public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationTarget = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationTarget == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } // @AutoValue is SOURCE retention, which means it cannot be looked up at runtime. // Assume abstract types built by static methods are AutoValue. if (Modifier.isAbstract(type.getRawType().getModifiers()) && deserializationTarget.isStatic()) { // Lookup the generated AutoValue class, whose fields must be read for serialization. String packageName = type.getRawType().getPackage().getName(); String autoClassName = type.getRawType().getName().replace('$', '_') .replace(packageName + ".", packageName + ".AutoValue_"); try { type = (TypeToken<T>) TypeToken.get(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
public void testRejectsIfNoConstuctorMarked() throws IOException { TypeAdapter<DefaultConstructor> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(DefaultConstructor.class)); assertNull(adapter); }
static DeserializationConstructorAndReflectiveTypeAdapterFactory parameterizedCtorFactory() { FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(ImmutableSet.of( new ExtractSerializedName(), new ExtractNamed())); AnnotationConstructorNamingStrategy deserializationPolicy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class, SerializedNames.class, Inject.class), ImmutableSet.of(new ExtractNamed())); return new DeserializationConstructorAndReflectiveTypeAdapterFactory(new ConstructorConstructor(ImmutableMap.<Type, InstanceCreator<?>>of()), serializationPolicy, Excluder.DEFAULT, deserializationPolicy); }
static DeserializationConstructorAndReflectiveTypeAdapterFactory parameterizedCtorFactory() { FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(ImmutableSet.of( new ExtractSerializedName(), new ExtractNamed())); NamingStrategies.AnnotationConstructorNamingStrategy deserializationPolicy = new NamingStrategies.AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class, Inject.class), ImmutableSet.of(new ExtractNamed())); return new DeserializationConstructorAndReflectiveTypeAdapterFactory(new ConstructorConstructor(), serializationPolicy, Excluder.DEFAULT, deserializationPolicy); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationTarget = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationTarget == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } // @AutoValue is SOURCE retention, which means it cannot be looked up at runtime. // Assume abstract types built by static methods are AutoValue. if (Modifier.isAbstract(type.getRawType().getModifiers()) && deserializationTarget.isStatic()) { // Lookup the generated AutoValue class, whose fields must be read for serialization. String packageName = type.getRawType().getPackage().getName(); String autoClassName = type.getRawType().getName().replace('$', '_') .replace(packageName + ".", packageName + ".AutoValue_"); try { type = (TypeToken<T>) TypeToken.get(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "duplicate key: foo") public void testNoDuplicateSerializedNamesRequiredOnAllParameters() { parameterizedCtorFactory.create(gson, TypeToken.get(DuplicateSerializedNames.class)); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationTarget = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationTarget == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } // @AutoValue is SOURCE retention, which means it cannot be looked up at runtime. // Assume abstract types built by static methods are AutoValue. if (Modifier.isAbstract(type.getRawType().getModifiers()) && deserializationTarget.isStatic()) { // Lookup the generated AutoValue class, whose fields must be read for serialization. String packageName = type.getRawType().getPackage().getName(); String autoClassName = type.getRawType().getName().replace('$', '_') .replace(packageName + ".", packageName + ".AutoValue_"); try { type = (TypeToken<T>) TypeToken.get(type.getRawType().getClassLoader().loadClass(autoClassName)); } catch (ClassNotFoundException ignored) { } } return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationTarget, getParameterReaders(gson, deserializationTarget)); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* parameter 0 failed to be named by AnnotationBasedNamingStrategy requiring one of javax.inject.Named") public void testSerializedNameRequiredOnAllParameters() { parameterizedCtorFactory .create(gson, TypeToken.get(WithDeserializationConstructorButWithoutSerializedName.class)); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }
public void testRejectsIfNoConstuctorMarked() throws IOException { TypeAdapter<DefaultConstructor> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(DefaultConstructor.class)); assertNull(adapter); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* parameter 0 failed to be named by AnnotationBasedNamingStrategy requiring one of javax.inject.Named") public void testSerializedNameRequiredOnAllParameters() { parameterizedCtorFactory .create(gson, TypeToken.get(WithDeserializationConstructorButWithoutSerializedName.class)); }
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { com.google.common.reflect.TypeToken<T> token = typeToken(type.getType()); Invokable<T, T> deserializationCtor = constructorFieldNamingPolicy.getDeserializer(token); if (deserializationCtor == null) { return null; // allow GSON to choose the correct Adapter (can't simply return delegateFactory.create()) } else { return new DeserializeIntoParameterizedConstructor<T>(delegateFactory.create(gson, type), deserializationCtor, getParameterReaders(gson, deserializationCtor)); } }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Multiple entries with same key: foo.*") public void testNoDuplicateSerializedNamesRequiredOnAllParameters() { parameterizedCtorFactory.create(gson, TypeToken.get(DuplicateSerializedNames.class)); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Incorrect count of names on annotation of .*") public void testSerializedNamesMustHaveCorrectCountOfNames() { parameterizedCtorFactory.create(gson, TypeToken.get(ValueTypeWithFactoryMissingSerializedNames.class)); }
public void testNullWhenPrimitive() { assertNull(parameterizedCtorFactory.create(gson, TypeToken.get(int.class))); }
public void testNullWhenPrimitive() { assertNull(parameterizedCtorFactory.create(gson, TypeToken.get(int.class))); }
public void testGenericParamsCopiedIn() throws IOException { TypeAdapter<GenericParamsCopiedIn> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(GenericParamsCopiedIn.class)); List<String> inputFoo = Lists.newArrayList(); inputFoo.add("one"); Map<String, String> inputBar = Maps.newHashMap(); inputBar.put("2", "two"); GenericParamsCopiedIn toTest = adapter.fromJson("{ \"foo\":[\"one\"], \"bar\":{ \"2\":\"two\"}}"); assertEquals(inputFoo, toTest.foo); assertNotSame(inputFoo, toTest.foo); assertEquals(inputBar, toTest.bar); }
@Test(expectedExceptions = NullPointerException.class) public void testPartialObjectStillThrows() throws IOException { TypeAdapter<ComposedObjects> adapter = parameterizedCtorFactory .create(gson, TypeToken.get(ComposedObjects.class)); assertNull(adapter.fromJson("{\"x\":{\"foo\":0,\"bar\":1}}")); }
public void testGenericParamsCopiedIn() throws IOException { TypeAdapter<GenericParamsCopiedIn> adapter = parameterizedCtorFactory.create(gson, TypeToken.get(GenericParamsCopiedIn.class)); List<String> inputFoo = Lists.newArrayList(); inputFoo.add("one"); Map<String, String> inputBar = Maps.newHashMap(); inputBar.put("2", "two"); GenericParamsCopiedIn toTest = adapter.fromJson("{ \"foo\":[\"one\"], \"bar\":{ \"2\":\"two\"}}"); assertEquals(inputFoo, toTest.foo); assertNotSame(inputFoo, toTest.foo); assertEquals(inputBar, toTest.bar); }