Java Code Examples for com.google.gson.FieldNamingStrategy

The following examples show how to use com.google.gson.FieldNamingStrategy. These examples are extracted from open source projects. 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
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
  // Check if we can deal with the given type
  if (!IUnknownPropertiesConsumer.class.isAssignableFrom(typeToken.getRawType())) {
    return null;
  }
  // If we can, we should get the backing class to fetch its fields from
  @SuppressWarnings("unchecked")
  final Class<IUnknownPropertiesConsumer> rawType =
      (Class<IUnknownPropertiesConsumer>) typeToken.getRawType();
  @SuppressWarnings("unchecked")
  final TypeAdapter<IUnknownPropertiesConsumer> delegateTypeAdapter =
      (TypeAdapter<IUnknownPropertiesConsumer>) gson.getDelegateAdapter(this, typeToken);
  // Excluder is necessary to check if the field can be processed
  // Basically it's not required, but it makes the check more complete
  final Excluder excluder = gson.excluder();
  // This is crucial to map fields and JSON object properties since Gson supports name remapping
  final FieldNamingStrategy fieldNamingStrategy = gson.fieldNamingStrategy();
  final TypeAdapter<IUnknownPropertiesConsumer> unknownPropertiesTypeAdapter =
      UnknownPropertiesTypeAdapter.create(
          rawType, delegateTypeAdapter, excluder, fieldNamingStrategy);
  @SuppressWarnings("unchecked")
  final TypeAdapter<T> castTypeAdapter = (TypeAdapter<T>) unknownPropertiesTypeAdapter;
  return castTypeAdapter;
}
 
Example 2
private static Collection<String> getPropertyNames(
    final Class<?> clazz,
    final Excluder excluder,
    final FieldNamingStrategy fieldNamingStrategy) {
  final Collection<String> propertyNames = new ArrayList<>();
  // Class fields are declared per class so we have to traverse the whole hierarchy
  for (Class<?> i = clazz;
      i.getSuperclass() != null && i != Object.class;
      i = i.getSuperclass()) {
    for (final Field declaredField : i.getDeclaredFields()) {
      // If the class field is not excluded
      if (!excluder.excludeField(declaredField, false)) {
        // We can translate the field name to its property name counter-part
        final String propertyName = fieldNamingStrategy.translateName(declaredField);
        propertyNames.add(propertyName);
      }
    }
  }
  return propertyNames;
}
 
Example 3
Source Project: unitime   Source File: EventsExportEventsToJSON.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void print(ExportHelper helper, EventLookupRpcRequest request, List<EventInterface> events, int eventCookieFlags, EventMeetingSortBy sort, boolean asc) throws IOException {
	helper.setup("application/json", reference(), false);
	
   	Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
		@Override
		public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
			return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
		}
	})
	.setFieldNamingStrategy(new FieldNamingStrategy() {
		Pattern iPattern = Pattern.compile("i([A-Z])(.*)");
		@Override
		public String translateName(Field f) {
			Matcher matcher = iPattern.matcher(f.getName());
			if (matcher.matches())
				return matcher.group(1).toLowerCase() + matcher.group(2);
			else
				return f.getName();
		}
	})
	.setPrettyPrinting().create();
   	
   	helper.getWriter().write(gson.toJson(events));
}
 
Example 4
private static <T extends IUnknownPropertiesConsumer> TypeAdapter<T> create(
    final Class<? super T> clazz,
    final TypeAdapter<T> typeAdapter,
    final Excluder excluder,
    final FieldNamingStrategy fieldNamingStrategy) {
  final Collection<String> propertyNames =
      getPropertyNames(clazz, excluder, fieldNamingStrategy);
  return new UnknownPropertiesTypeAdapter<>(typeAdapter, propertyNames);
}
 
Example 5
Source Project: lastaflute   Source File: GsonJsonEngine.java    License: Apache License 2.0 5 votes vote down vote up
protected LaReflectiveTypeAdapterFactory createReflectiveTypeAdapterFactory(Gson newGson, Object factory) {
    final ConstructorConstructor constructorConstructor = getConstructorConstructor(factory);
    final JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory = getJsonAdapterFactory(factory);
    final FieldNamingStrategy fieldNamingStrategy = newGson.fieldNamingStrategy();
    final Excluder excluder = newGson.excluder();
    return new LaReflectiveTypeAdapterFactory(constructorConstructor, fieldNamingStrategy, excluder, jsonAdapterFactory);
}
 
Example 6
Source Project: lastaflute   Source File: LaReflectiveTypeAdapterFactory.java    License: Apache License 2.0 5 votes vote down vote up
public LaReflectiveTypeAdapterFactory(ConstructorConstructor constructorConstructor, FieldNamingStrategy fieldNamingPolicy,
        Excluder excluder, JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory) {
    this.constructorConstructor = constructorConstructor;
    this.fieldNamingPolicy = fieldNamingPolicy;
    this.excluder = excluder;
    this.jsonAdapterFactory = jsonAdapterFactory;
}
 
Example 7
Source Project: gson   Source File: ReflectiveTypeAdapterFactory.java    License: Apache License 2.0 5 votes vote down vote up
public ReflectiveTypeAdapterFactory(ConstructorConstructor constructorConstructor,
    FieldNamingStrategy fieldNamingPolicy, Excluder excluder,
    JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory) {
  this.constructorConstructor = constructorConstructor;
  this.fieldNamingPolicy = fieldNamingPolicy;
  this.excluder = excluder;
  this.jsonAdapterFactory = jsonAdapterFactory;
}
 
Example 8
Source Project: letv   Source File: ReflectiveTypeAdapterFactory.java    License: Apache License 2.0 4 votes vote down vote up
public ReflectiveTypeAdapterFactory(ConstructorConstructor constructorConstructor, FieldNamingStrategy fieldNamingPolicy, Excluder excluder) {
    this.constructorConstructor = constructorConstructor;
    this.fieldNamingPolicy = fieldNamingPolicy;
    this.excluder = excluder;
}
 
Example 9
Source Project: java   Source File: GsonCompatibilityMode.java    License: MIT License 4 votes vote down vote up
public Builder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
    this.fieldNamingStrategy = fieldNamingStrategy;
    return this;
}
 
Example 10
Source Project: graphql-spqr   Source File: GsonValueMapper.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isDeserializableInSubType(String fieldName, FieldNamingStrategy namingStrategy, List<Class<?>> concreteSubTypes) {
    return concreteSubTypes.stream().anyMatch(impl -> ClassUtils.findField(impl, field -> namingStrategy.translateName(field).equals(fieldName)).isPresent());
}
 
Example 11
Source Project: graphql-spqr   Source File: GsonFieldNamingStrategy.java    License: Apache License 2.0 4 votes vote down vote up
public GsonFieldNamingStrategy(FieldNamingStrategy fallback, MessageBundle messageBundle) {
    this.fallback = fallback;
    this.messageBundle = messageBundle;
}
 
Example 12
Source Project: graphql-spqr   Source File: GsonValueMapperFactory.java    License: Apache License 2.0 4 votes vote down vote up
private GsonValueMapperFactory(Gson prototype, TypeInfoGenerator typeInfoGenerator, FieldNamingStrategy fieldNamingStrategy, List<Configurer> configurers) {
    this.prototype = prototype;
    this.fieldNamingStrategy = fieldNamingStrategy;
    this.typeInfoGenerator = Objects.requireNonNull(typeInfoGenerator);
    this.configurers = Objects.requireNonNull(configurers);
}
 
Example 13
Source Project: graphql-spqr   Source File: GsonValueMapperFactory.java    License: Apache License 2.0 4 votes vote down vote up
public Builder withFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
    this.fieldNamingStrategy = fieldNamingStrategy;
    return this;
}
 
Example 14
public ReflectiveTypeAdapterFactory(ConstructorConstructor constructorconstructor, FieldNamingStrategy fieldnamingstrategy, Excluder excluder)
{
    a = constructorconstructor;
    b = fieldnamingstrategy;
    c = excluder;
}
 
Example 15
public ReflectiveTypeAdapterFactory(ConstructorConstructor constructorConstructor,
    FieldNamingStrategy fieldNamingPolicy, Excluder excluder) {
  this.constructorConstructor = constructorConstructor;
  this.fieldNamingPolicy = fieldNamingPolicy;
  this.excluder = excluder;
}
 
Example 16
static String getFieldName(FieldNamingStrategy fieldNamingPolicy, Field f) {
  SerializedName serializedName = f.getAnnotation(SerializedName.class);
  return serializedName == null ? fieldNamingPolicy.translateName(f) : serializedName.value();
}