|
Java example source code file (InstanceCreatorTest.java)
The InstanceCreatorTest.java Java example source code
/*
* Copyright (C) 2009 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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.common.TestTypes.Base;
import com.google.gson.common.TestTypes.ClassWithBaseField;
import com.google.gson.common.TestTypes.Sub;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import java.lang.reflect.Type;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Functional Test exercising custom serialization only. When test applies to both
* serialization and deserialization then add it to CustomTypeAdapterTest.
*
* @author Inderjeet Singh
*/
public class InstanceCreatorTest extends TestCase {
public void testInstanceCreatorReturnsBaseType() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
@Override public Base createInstance(Type type) {
return new Base();
}
})
.create();
String json = "{baseName:'BaseRevised',subName:'Sub'}";
Base base = gson.fromJson(json, Base.class);
assertEquals("BaseRevised", base.baseName);
}
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
@Override public Base createInstance(Type type) {
return new Sub();
}
})
.create();
String json = "{baseName:'Base',subName:'SubRevised'}";
Base base = gson.fromJson(json, Base.class);
assertTrue(base instanceof Sub);
Sub sub = (Sub) base;
assertFalse("SubRevised".equals(sub.subName));
assertEquals(Sub.SUB_NAME, sub.subName);
}
public void testInstanceCreatorReturnsSubTypeForField() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
@Override public Base createInstance(Type type) {
return new Sub();
}
})
.create();
String json = "{base:{baseName:'Base',subName:'SubRevised'}}";
ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class);
assertTrue(target.base instanceof Sub);
assertEquals(Sub.SUB_NAME, ((Sub)target.base).subName);
}
// This regressed in Gson 2.0 and 2.1
public void testInstanceCreatorForCollectionType() {
@SuppressWarnings("serial")
class SubArrayList<T> extends ArrayList
Other Java examples (source code examples)Here is a short list of links related to this Java InstanceCreatorTest.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.