本文整理汇总了Java中com.google.gson.InstanceCreator类的典型用法代码示例。如果您正苦于以下问题:Java InstanceCreator类的具体用法?Java InstanceCreator怎么用?Java InstanceCreator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InstanceCreator类属于com.google.gson包,在下文中一共展示了InstanceCreator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConstructor
import com.google.gson.InstanceCreator; //导入依赖的package包/类
public <T> ObjectConstructor<T> getConstructor(TypeToken<T> typeToken) {
final Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
final InstanceCreator<T> creator = (InstanceCreator) this.instanceCreators.get(type);
if (creator != null) {
return new ObjectConstructor<T>() {
public T construct() {
return creator.createInstance(type);
}
};
}
ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType);
if (defaultConstructor != null) {
return defaultConstructor;
}
ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(rawType);
if (defaultImplementation != null) {
return defaultImplementation;
}
return newUnsafeAllocator(type, rawType);
}
示例2: testDeserializeAnyBeanWithCreateInstance
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test
public void testDeserializeAnyBeanWithCreateInstance() {
AnyBean expected = new AnyBean("Any Name", 35);
expected.setType("Java");
String json = oson.serialize(expected);
String jsonExpected = "{\"name\":\"Any Name\",\"age\":35,\"type\":\"Java\"}";
assertEquals(jsonExpected, json);
AnyBean result = oson.setClassMappers(AnyBean.class, new ClassMapper()
.setConstructor(new InstanceCreator(){
@Override
public Object createInstance(Type type) {
return new AnyBean(null, 0);
}
})).deserialize(json, AnyBean.class);
assertEquals(expected.toString(), result.toString());
}
示例3: loadResultsFromFile
import com.google.gson.InstanceCreator; //导入依赖的package包/类
private DriftResults loadResultsFromFile(String path) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(path));
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(
UnivariateFunction.class,
new InstanceCreator<PolynomialSplineFunction>() {
@Override
public PolynomialSplineFunction createInstance(Type type) {
return new PolynomialSplineFunction(new double[]{1, 2}, new PolynomialFunction[]{new PolynomialFunction(new double[1])});
}
}).create();
return gson.fromJson(reader, DriftResults.class);
} finally {
if(reader != null) {
reader.close();
}
}
}
示例4: testInstanceCreatorReturnsSubTypeForTopLevelObject_add1163
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorReturnsSubTypeForTopLevelObject_add1163() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject_add1163");
Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() {
public TestTypes.Base createInstance(Type type) {
return new TestTypes.Sub();
}
}).create();
String json = "{baseName:\'Base\',subName:\'SubRevised\'}";
TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub));
TestTypes.Sub sub = ((TestTypes.Sub)(base));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例5: testInstanceCreatorReturnsSubTypeForTopLevelObject
import com.google.gson.InstanceCreator; //导入依赖的package包/类
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject");
Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() {
public TestTypes.Base createInstance(Type type) {
return new TestTypes.Sub();
}
}).create();
String json = "foo";
TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub));
TestTypes.Sub sub = ((TestTypes.Sub)(base));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例6: testInstanceCreatorReturnsSubTypeForTopLevelObject_remove980
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorReturnsSubTypeForTopLevelObject_remove980() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorReturnsSubTypeForTopLevelObject_remove980");
Gson gson = new GsonBuilder().registerTypeAdapter(TestTypes.Base.class, new InstanceCreator<com.google.gson.common.TestTypes.Base>() {
public TestTypes.Base createInstance(Type type) {
return new TestTypes.Sub();
}
}).create();
String json = "{baseName:\'Base\',subName:\'SubRevised\'}";
TestTypes.Base base = gson.fromJson(json, TestTypes.Base.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1647,(base instanceof com.google.gson.common.TestTypes.Sub));
TestTypes.Sub sub = ((TestTypes.Sub)(base));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1649,"SubRevised",1648,"SubRevised".equals(sub.subName));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1650,com.google.gson.common.TestTypes.Sub.SUB_NAME);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1651,sub.subName);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例7: testInstanceCreatorForCollectionType_add1155
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_add1155() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1155");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例8: testInstanceCreatorForCollectionType_add1156
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_add1156() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1156");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例9: testInstanceCreatorForCollectionType_add1157
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_add1157() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_add1157");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例10: testInstanceCreatorForCollectionType
import com.google.gson.InstanceCreator; //导入依赖的package包/类
public void testInstanceCreatorForCollectionType() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例11: testInstanceCreatorForCollectionType_literalMutation1301
import com.google.gson.InstanceCreator; //导入依赖的package包/类
public void testInstanceCreatorForCollectionType_literalMutation1301() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_literalMutation1301");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("foo", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例12: testInstanceCreatorForCollectionType_remove972
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_remove972() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove972");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例13: testInstanceCreatorForCollectionType_remove973
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_remove973() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove973");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例14: testInstanceCreatorForCollectionType_remove974
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@Test(timeout = 1000)
public void testInstanceCreatorForCollectionType_remove974() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForCollectionType_remove974");
@SuppressWarnings(value = "serial")
class SubArrayList<T> extends ArrayList<T> { }
InstanceCreator<java.util.List<java.lang.String>> listCreator = new InstanceCreator<java.util.List<java.lang.String>>() {
public List<java.lang.String> createInstance(Type type) {
return new SubArrayList<java.lang.String>();
}
};
Type listOfStringType = new TypeToken<java.util.List<java.lang.String>>() { }.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listOfStringType, listCreator).create();
List<java.lang.String> list = gson.fromJson("[\"a\"]", listOfStringType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1631,SubArrayList.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1633,list,1632,list.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例15: testInstanceCreatorForParametrizedType_add1158
import com.google.gson.InstanceCreator; //导入依赖的package包/类
@SuppressWarnings(value = { "unchecked" , "rawtypes" })
@Test(timeout = 1000)
public void testInstanceCreatorForParametrizedType_add1158() throws Exception {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testInstanceCreatorForParametrizedType_add1158");
@SuppressWarnings(value = "serial")
class SubTreeSet<T> extends TreeSet<T> { }
InstanceCreator<java.util.SortedSet> sortedSetCreator = new InstanceCreator<java.util.SortedSet>() {
public SortedSet createInstance(Type type) {
return new SubTreeSet();
}
};
Gson gson = new GsonBuilder().registerTypeAdapter(SortedSet.class, sortedSetCreator).create();
Type sortedSetType = new TypeToken<java.util.SortedSet<java.lang.String>>() { }.getType();
SortedSet<java.lang.String> set = gson.fromJson("[\"a\"]", sortedSetType);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1635,set,1634,set.first());
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1636,SubTreeSet.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1638,set,1637,set.getClass());
set = gson.fromJson("[\"b\"]", SortedSet.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1640,set,1639,set.first());
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1641,SubTreeSet.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1643,set,1642,set.getClass());
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}