|
Java example source code file (RuntimeTypeAdapterFactoryFunctionalTest.java)
The RuntimeTypeAdapterFactoryFunctionalTest.java Java example source code
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.functional;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Functional tests for the RuntimeTypeAdapterFactory feature in extras.
*/
public final class RuntimeTypeAdapterFactoryFunctionalTest extends TestCase {
private final Gson gson = new Gson();
/**
* This test also ensures that {@link TypeAdapterFactory} registered through {@link JsonAdapter}
* work correctly for {@link Gson#getDelegateAdapter(TypeAdapterFactory, TypeToken)}.
*/
public void testSubclassesAutomaticallySerialized() throws Exception {
Shape shape = new Circle(25);
String json = gson.toJson(shape);
shape = gson.fromJson(json, Shape.class);
assertEquals(25, ((Circle)shape).radius);
shape = new Square(15);
json = gson.toJson(shape);
shape = gson.fromJson(json, Shape.class);
assertEquals(15, ((Square)shape).side);
assertEquals(ShapeType.SQUARE, shape.type);
}
@JsonAdapter(Shape.JsonAdapterFactory.class)
static class Shape {
final ShapeType type;
Shape(ShapeType type) { this.type = type; }
private static final class JsonAdapterFactory extends RuntimeTypeAdapterFactory<Shape> {
public JsonAdapterFactory() {
super(Shape.class, "type");
registerSubtype(Circle.class, ShapeType.CIRCLE.toString());
registerSubtype(Square.class, ShapeType.SQUARE.toString());
}
}
}
public enum ShapeType {
SQUARE, CIRCLE
}
private static final class Circle extends Shape {
final int radius;
Circle(int radius) { super(ShapeType.CIRCLE); this.radius = radius; }
}
private static final class Square extends Shape {
final int side;
Square(int side) { super(ShapeType.SQUARE); this.side = side; }
}
// Copied from the extras package
static class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
private final Class<?> baseType;
private final String typeFieldName;
private final Map<String, Class>> labelToSubtype = new LinkedHashMap
Other Java examples (source code examples)Here is a short list of links related to this Java RuntimeTypeAdapterFactoryFunctionalTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.