com.google.gson.InstanceCreator Java Examples
The following examples show how to use
com.google.gson.InstanceCreator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: letv Author: JackChan1999 File: ConstructorConstructor.java License: Apache License 2.0 | 6 votes |
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); }
Example #2
Source Project: thunderstorm Author: zitmen File: ResultsDriftCorrection.java License: GNU General Public License v3.0 | 6 votes |
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(); } } }
Example #3
Source Project: gson Author: google File: InstanceCreatorTest.java License: Apache License 2.0 | 6 votes |
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); }
Example #4
Source Project: gson Author: google File: InstanceCreatorTest.java License: Apache License 2.0 | 6 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) public void testInstanceCreatorForParametrizedType() throws Exception { @SuppressWarnings("serial") class SubTreeSet<T> extends TreeSet<T> {} InstanceCreator<SortedSet> sortedSetCreator = new InstanceCreator<SortedSet>() { @Override public SortedSet createInstance(Type type) { return new SubTreeSet(); } }; Gson gson = new GsonBuilder() .registerTypeAdapter(SortedSet.class, sortedSetCreator) .create(); Type sortedSetType = new TypeToken<SortedSet<String>>() {}.getType(); SortedSet<String> set = gson.fromJson("[\"a\"]", sortedSetType); assertEquals(set.first(), "a"); assertEquals(SubTreeSet.class, set.getClass()); set = gson.fromJson("[\"b\"]", SortedSet.class); assertEquals(set.first(), "b"); assertEquals(SubTreeSet.class, set.getClass()); }
Example #5
Source Project: extentreports-java Author: extent-framework File: JsonConfigLoader.java License: Apache License 2.0 | 5 votes |
private void init(T instance) { this.instance = instance; creator = new InstanceCreator<T>() { @Override public T createInstance(Type type) { return instance; } }; }
Example #6
Source Project: typescript.java Author: angelozerr File: CompletionsRequest.java License: MIT License | 5 votes |
@Override public Response<List<CompletionEntry>> parseResponse(JsonObject json) { String fileName = super.getArguments().getFile(); int line = super.getArguments().getLine(); int offset = super.getArguments().getOffset(); Gson gson = new GsonBuilder() .registerTypeAdapter(CompletionEntry.class, new InstanceCreator<CompletionEntry>() { @Override public CompletionEntry createInstance(Type type) { return factory.create(matcherProvider.getMatcher(), fileName, line, offset, client); } }).create(); return gson.fromJson(json, CompletionsResponse.class); }
Example #7
Source Project: typescript.java Author: angelozerr File: NavBarRequest.java License: MIT License | 5 votes |
@Override public Response<List<NavigationBarItem>> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavBarResponse.class); }
Example #8
Source Project: typescript.java Author: angelozerr File: NavTreeRequest.java License: MIT License | 5 votes |
@Override public Response<NavigationBarItem> parseResponse(JsonObject json) { Gson gson = GsonHelper.DEFAULT_GSON; if (positionProvider != null) { gson = new GsonBuilder().registerTypeAdapter(Location.class, new InstanceCreator<Location>() { @Override public Location createInstance(Type type) { return new Location(positionProvider); } }).create(); } return gson.fromJson(json, NavTreeResponse.class); }
Example #9
Source Project: MiBandDecompiled Author: vishnudevk File: h.java License: Apache License 2.0 | 5 votes |
h(ConstructorConstructor constructorconstructor, InstanceCreator instancecreator, Type type) { c = constructorconstructor; a = instancecreator; b = type; super(); }
Example #10
Source Project: MiBandDecompiled Author: vishnudevk File: ConstructorConstructor.java License: Apache License 2.0 | 5 votes |
public ObjectConstructor get(TypeToken typetoken) { Type type = typetoken.getType(); Class class1 = typetoken.getRawType(); InstanceCreator instancecreator = (InstanceCreator)a.get(type); Object obj; if (instancecreator != null) { obj = new d(this, instancecreator, type); } else { InstanceCreator instancecreator1 = (InstanceCreator)a.get(class1); if (instancecreator1 != null) { return new h(this, instancecreator1, type); } obj = a(class1); if (obj == null) { obj = a(type, class1); if (obj == null) { return b(type, class1); } } } return ((ObjectConstructor) (obj)); }
Example #11
Source Project: gson Author: google File: MapTest.java License: Apache License 2.0 | 5 votes |
public void testMapSubclassDeserialization() { Gson gson = new GsonBuilder().registerTypeAdapter(MyMap.class, new InstanceCreator<MyMap>() { public MyMap createInstance(Type type) { return new MyMap(); } }).create(); String json = "{\"a\":1,\"b\":2}"; MyMap map = gson.fromJson(json, MyMap.class); assertEquals("1", map.get("a")); assertEquals("2", map.get("b")); }
Example #12
Source Project: gson Author: google File: InstanceCreatorTest.java License: Apache License 2.0 | 5 votes |
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); }
Example #13
Source Project: gson Author: google File: InstanceCreatorTest.java License: Apache License 2.0 | 5 votes |
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); }
Example #14
Source Project: gson Author: google File: InstanceCreatorTest.java License: Apache License 2.0 | 5 votes |
public void testInstanceCreatorForCollectionType() { @SuppressWarnings("serial") class SubArrayList<T> extends ArrayList<T> {} InstanceCreator<List<String>> listCreator = new InstanceCreator<List<String>>() { @Override public List<String> createInstance(Type type) { return new SubArrayList<String>(); } }; Type listOfStringType = new TypeToken<List<String>>() {}.getType(); Gson gson = new GsonBuilder() .registerTypeAdapter(listOfStringType, listCreator) .create(); List<String> list = gson.fromJson("[\"a\"]", listOfStringType); assertEquals(SubArrayList.class, list.getClass()); }
Example #15
Source Project: gson Author: google File: ObjectTest.java License: Apache License 2.0 | 5 votes |
public void testInnerClassDeserialization() { final Parent p = new Parent(); Gson gson = new GsonBuilder().registerTypeAdapter( Parent.Child.class, new InstanceCreator<Parent.Child>() { public Parent.Child createInstance(Type type) { return p.new Child(); } }).create(); String json = "{'value2':3}"; Parent.Child c = gson.fromJson(json, Parent.Child.class); assertEquals(3, c.value2); }
Example #16
Source Project: gson Author: google File: GraphAdapterBuilder.java License: Apache License 2.0 | 5 votes |
public GraphAdapterBuilder addType(Type type, InstanceCreator<?> instanceCreator) { if (type == null || instanceCreator == null) { throw new NullPointerException(); } instanceCreators.put(type, instanceCreator); return this; }
Example #17
Source Project: gson Author: google File: GraphAdapterBuilder.java License: Apache License 2.0 | 5 votes |
public void registerOn(GsonBuilder gsonBuilder) { Factory factory = new Factory(instanceCreators); gsonBuilder.registerTypeAdapterFactory(factory); for (Map.Entry<Type, InstanceCreator<?>> entry : instanceCreators.entrySet()) { gsonBuilder.registerTypeAdapter(entry.getKey(), factory); } }
Example #18
Source Project: gson Author: google File: GraphAdapterBuilder.java License: Apache License 2.0 | 5 votes |
/** * Hook for the graph adapter to get a reference to a deserialized value * before that value is fully populated. This is useful to deserialize * values that directly or indirectly reference themselves: we can hand * out an instance before read() returns. * * <p>Gson should only ever call this method when we're expecting it to; * that is only when we've called back into Gson to deserialize a tree. */ @SuppressWarnings("unchecked") public Object createInstance(Type type) { Graph graph = graphThreadLocal.get(); if (graph == null || graph.nextCreate == null) { throw new IllegalStateException("Unexpected call to createInstance() for " + type); } InstanceCreator<?> creator = instanceCreators.get(type); Object result = creator.createInstance(type); graph.nextCreate.value = result; graph.nextCreate = null; return result; }
Example #19
Source Project: streamsx.topology Author: IBMStreams File: Element.java License: Apache License 2.0 | 5 votes |
private Gson refresher() { if (refreshJson == null) { refreshJson = refreshBuilder.registerTypeAdapter(getClass(), new InstanceCreator<Object>() { @Override public Object createInstance(Type arg0) { return Element.this; }}).create(); } return refreshJson; }
Example #20
Source Project: smarthome Author: eclipse-archived File: PropertiesTypeAdapter.java License: Eclipse Public License 2.0 | 5 votes |
public PropertiesTypeAdapter(Gson gson) { // obtain the default type adapters for String and Object classes keyAdapter = gson.getAdapter(String.class); valueAdapter = gson.getAdapter(Object.class); // obtain default gson objects constructor = new ConstructorConstructor(Collections.<Type, InstanceCreator<?>> emptyMap()); delegate = new MapTypeAdapterFactory(constructor, false).create(new Gson(), TOKEN); }
Example #21
Source Project: letv Author: JackChan1999 File: ConstructorConstructor.java License: Apache License 2.0 | 4 votes |
public ConstructorConstructor(Map<Type, InstanceCreator<?>> instanceCreators) { this.instanceCreators = instanceCreators; }
Example #22
Source Project: lastaflute Author: lastaflute File: CollectionGsonAdaptable.java License: Apache License 2.0 | 4 votes |
protected CollectionTypeAdapterFactory createEmbeddedFactory() { final Map<Type, InstanceCreator<?>> instanceCreators = prepareInstanceCreators(); final ConstructorConstructor constructor = createConstructorConstructor(instanceCreators); return newCollectionTypeAdapterFactory(constructor); }
Example #23
Source Project: lastaflute Author: lastaflute File: CollectionGsonAdaptable.java License: Apache License 2.0 | 4 votes |
protected Map<Type, InstanceCreator<?>> prepareInstanceCreators() { return Collections.emptyMap(); }
Example #24
Source Project: lastaflute Author: lastaflute File: CollectionGsonAdaptable.java License: Apache License 2.0 | 4 votes |
protected ConstructorConstructor createConstructorConstructor(Map<Type, InstanceCreator<?>> instanceCreators) { return new ConstructorConstructor(instanceCreators); }
Example #25
Source Project: gson Author: google File: ConstructorConstructor.java License: Apache License 2.0 | 4 votes |
public ConstructorConstructor(Map<Type, InstanceCreator<?>> instanceCreators) { this.instanceCreators = instanceCreators; }
Example #26
Source Project: gson Author: google File: ConstructorConstructor.java License: Apache License 2.0 | 4 votes |
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { final Type type = typeToken.getType(); final Class<? super T> rawType = typeToken.getRawType(); // first try an instance creator @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type); if (typeCreator != null) { return new ObjectConstructor<T>() { @Override public T construct() { return typeCreator.createInstance(type); } }; } // Next try raw type match for instance creators @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType); if (rawTypeCreator != null) { return new ObjectConstructor<T>() { @Override public T construct() { return rawTypeCreator.createInstance(type); } }; } ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType); if (defaultImplementation != null) { return defaultImplementation; } // finally try unsafe return newUnsafeAllocator(type, rawType); }
Example #27
Source Project: gson Author: google File: GraphAdapterBuilder.java License: Apache License 2.0 | 4 votes |
public GraphAdapterBuilder() { this.instanceCreators = new HashMap<Type, InstanceCreator<?>>(); this.constructorConstructor = new ConstructorConstructor(instanceCreators); }
Example #28
Source Project: gson Author: google File: GraphAdapterBuilder.java License: Apache License 2.0 | 4 votes |
Factory(Map<Type, InstanceCreator<?>> instanceCreators) { this.instanceCreators = instanceCreators; }
Example #29
Source Project: framework Author: Odoo-mobile File: ConstructorConstructor.java License: GNU Affero General Public License v3.0 | 4 votes |
public ConstructorConstructor(Map<Type, InstanceCreator<?>> instanceCreators) { this.instanceCreators = instanceCreators; }
Example #30
Source Project: framework Author: Odoo-mobile File: ConstructorConstructor.java License: GNU Affero General Public License v3.0 | 4 votes |
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { final Type type = typeToken.getType(); final Class<? super T> rawType = typeToken.getRawType(); // first try an instance creator @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type); if (typeCreator != null) { return new ObjectConstructor<T>() { public T construct() { return typeCreator.createInstance(type); } }; } // Next try raw type match for instance creators @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType); if (rawTypeCreator != null) { return new ObjectConstructor<T>() { public T construct() { return rawTypeCreator.createInstance(type); } }; } ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType); if (defaultImplementation != null) { return defaultImplementation; } // finally try unsafe return newUnsafeAllocator(type, rawType); }