Tabnine Logo
DeserializationConstructorAndReflectiveTypeAdapterFactory
Code IndexAdd Tabnine to your IDE (free)

How to use
DeserializationConstructorAndReflectiveTypeAdapterFactory
in
org.jclouds.json.internal

Best Java code snippets using org.jclouds.json.internal.DeserializationConstructorAndReflectiveTypeAdapterFactory (Showing top 20 results out of 315)

origin: com.amysta.jclouds/jclouds-core

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));
}
origin: jclouds/legacy-jclouds

public void testRejectsIfNoConstuctorMarked() throws IOException {
 TypeAdapter<DefaultConstructor> adapter = parameterizedCtorFactory.create(gson,
    TypeToken.get(DefaultConstructor.class));
 assertNull(adapter);
}
origin: apache/jclouds

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);
}
origin: jclouds/legacy-jclouds

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);
}
origin: Nextdoor/bender

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));
}
origin: jclouds/legacy-jclouds

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "duplicate key: foo")
public void testNoDuplicateSerializedNamesRequiredOnAllParameters() {
 parameterizedCtorFactory.create(gson, TypeToken.get(DuplicateSerializedNames.class));
}
origin: apache/jclouds

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));
}
origin: apache/jclouds

@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));
}
origin: jclouds/legacy-jclouds

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));
 }
}
origin: apache/jclouds

public void testRejectsIfNoConstuctorMarked() throws IOException {
 TypeAdapter<DefaultConstructor> adapter = parameterizedCtorFactory.create(gson,
    TypeToken.get(DefaultConstructor.class));
 assertNull(adapter);
}
origin: io.cloudsoft.jclouds/jclouds-core

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));
 }
}
origin: jclouds/legacy-jclouds

@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));
}
origin: org.jclouds/jclouds-core

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));
 }
}
origin: apache/jclouds

@Test(expectedExceptions = IllegalArgumentException.class,
   expectedExceptionsMessageRegExp = "Multiple entries with same key: foo.*")
public void testNoDuplicateSerializedNamesRequiredOnAllParameters() {
 parameterizedCtorFactory.create(gson, TypeToken.get(DuplicateSerializedNames.class));
}
origin: apache/jclouds

@Test(expectedExceptions = IllegalArgumentException.class,
   expectedExceptionsMessageRegExp = "Incorrect count of names on annotation of .*")
public void testSerializedNamesMustHaveCorrectCountOfNames() {
 parameterizedCtorFactory.create(gson, TypeToken.get(ValueTypeWithFactoryMissingSerializedNames.class));
}
origin: jclouds/legacy-jclouds

public void testNullWhenPrimitive() {
 assertNull(parameterizedCtorFactory.create(gson, TypeToken.get(int.class)));
}
origin: apache/jclouds

public void testNullWhenPrimitive() {
 assertNull(parameterizedCtorFactory.create(gson, TypeToken.get(int.class)));
}
origin: jclouds/legacy-jclouds

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);
}
origin: jclouds/legacy-jclouds

@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}}"));
}
origin: apache/jclouds

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);
}
org.jclouds.json.internalDeserializationConstructorAndReflectiveTypeAdapterFactory

Javadoc

Creates type adapters for types handled in the following ways:

  • Deserialization
  • If there's an annotation designating a parameterized constructor, invoke that for fields correlating to named parameter annotations. Otherwise, use ConstructorConstructor, and set fields via reflection.

    Notes: primitive constructor params are set to the Java defaults (0 or false) if not present; and the empty object ({}) is treated as a null if the constructor for the object throws an NPE.

  • Serialization
  • Serialize based on reflective access to fields, delegating to ReflectiveTypeAdaptor.

Example: Using javax inject to select a constructor and corresponding named parameters

 
import NamingStrategies.*; 
serializationStrategy = new AnnotationOrNameFieldNamingStrategy( 
new ExtractSerializedName(), new ExtractNamed()); 
deserializationStrategy = new AnnotationConstructorNamingStrategy( 
ImmutableSet.of(javax.inject.Inject.class), 
ImmutableSet.of(new ExtractNamed())); 
factory = new DeserializationConstructorAndReflectiveTypeAdapterFactory(new ConstructorConstructor(), 
serializationStrategy, Excluder.DEFAULT, deserializationStrategy); 
gson = new GsonBuilder(serializationStrategy).registerTypeAdapterFactory(factory).create(); 

The above would work fine on the following class, which has no gson-specific annotations:

 
private static class ImmutableAndVerifiedInCtor { 
final int foo; 
@Named("_bar") 
final int bar; 
@Inject 
ImmutableAndVerifiedInCtor(@Named("foo") int foo, @Named("_bar") int bar) { 
if (foo < 0) 
throw new IllegalArgumentException("negative!"); 
this.foo = foo; 
this.bar = bar; 
} 
} 


Most used methods

  • getParameterReaders
  • <init>
  • create

Popular in Java

  • Making http post requests using okhttp
  • compareTo (BigDecimal)
  • getContentResolver (Context)
  • addToBackStack (FragmentTransaction)
  • Locale (java.util)
    Locale represents a language/country/variant combination. Locales are used to alter the presentatio
  • Filter (javax.servlet)
    A filter is an object that performs filtering tasks on either the request to a resource (a servlet o
  • JCheckBox (javax.swing)
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
  • BasicDataSource (org.apache.commons.dbcp)
    Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is no
  • LogFactory (org.apache.commons.logging)
    Factory for creating Log instances, with discovery and configuration features similar to that employ
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now