Example usage for com.google.gson.internal Excluder DEFAULT

List of usage examples for com.google.gson.internal Excluder DEFAULT

Introduction

In this page you can find the example usage for com.google.gson.internal Excluder DEFAULT.

Prototype

Excluder DEFAULT

To view the source code for com.google.gson.internal Excluder DEFAULT.

Click Source Link

Usage

From source file:com.gilecode.yagson.YaGson.java

License:Apache License

/**
 * Constructs a Gson object with the default configuration. The default configuration has the
 * following settings://from   www  . j a va2s  .  c  om
 * <ul>
 *   <li>By default, YaGson's detects all self-references and the duplicate objects in the whole references graph
 *   of the serialized object. All such objects except the first one are emitted as link-like references, see
 *   {@link ReferencesPolicy#DUPLICATE_OBJECTS}. Although this policy may be changed to the alternative ones by
 *   {@link YaGsonBuilder#setReferencesPolicy(ReferencesPolicy)}, it is not recommended in case of arbitrary objects,
 *   as the functionality of the deserialized objects may be broken.</li>
 *   <li>By default, the type information is emitted as {@literal @type/@value} wrappers when the actual types
 *   would be lost otherwise, i.e. when the known de-serialization type is less specific than the actual class
 *   of an object or its part being serialized. Use
 *   {@link YaGsonBuilder#setTypeInfoPolicy(TypeInfoPolicy)} to change it if necessary.</li>
 *   <li>The JSON generated by <code>toJson</code> methods is in compact representation. This
 *   means that all the unneeded white-space is removed. You can change this behavior with
 *   {@link GsonBuilder#setPrettyPrinting()}. </li>
 *   <li>The generated JSON omits all the fields that are null. Note that nulls in arrays are
 *   kept as is since an array is an ordered list. Moreover, if a field is not null, but its
 *   generated JSON is empty, the field is kept. You can configure Gson to serialize null values
 *   by setting {@link GsonBuilder#serializeNulls()}.</li>
 *   <li>The default Date format is same as {@link java.text.DateFormat#DEFAULT}. This format
 *   ignores the millisecond portion of the date during serialization. You can change
 *   this by invoking {@link GsonBuilder#setDateFormat(int)} or
 *   {@link GsonBuilder#setDateFormat(String)}. </li>
 *   <li>By default, Gson ignores the {@link com.google.gson.annotations.Expose} annotation.
 *   You can enable Gson to serialize/deserialize only those fields marked with this annotation
 *   through {@link GsonBuilder#excludeFieldsWithoutExposeAnnotation()}. </li>
 *   <li>By default, Gson ignores the {@link com.google.gson.annotations.Since} annotation. You
 *   can enable Gson to use this annotation through {@link GsonBuilder#setVersion(double)}.</li>
 *   <li>The default field naming policy for the output Json is same as in Java. So, a Java class
 *   field <code>versionNumber</code> will be output as <code>&quot;versionNumber&quot;</code> in
 *   Json. The same rules are applied for mapping incoming Json to the Java classes. You can
 *   change this policy through {@link GsonBuilder#setFieldNamingPolicy(FieldNamingPolicy)}.</li>
 *   <li>By default, YaGson excludes <code>static</code> fields from
 *   consideration for serialization and deserialization, but serializes most of <code>transient</code>
 *   fields.
 *   You can change this behavior through
 *   {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.</li>
 * </ul>
 */
public YaGson() {
    super(Excluder.DEFAULT.forReferencesPolicy(References.defaultPolicy()), FieldNamingPolicy.IDENTITY,
            Collections.<Type, InstanceCreator<?>>emptyMap(), false, TypeInfoPolicy.defaultPolicy().isEnabled(),
            DEFAULT_JSON_NON_EXECUTABLE, !TypeInfoPolicy.defaultPolicy().isEnabled(), // disable htmlSafe if types are printed
            false, false, LongSerializationPolicy.DEFAULT, Collections.<TypeAdapterFactory>emptyList(),
            References.defaultPolicy(), TypeInfoPolicy.defaultPolicy());
}

From source file:edu.mit.media.funf.config.DefaultRuntimeTypeAdapterFactory.java

License:Open Source License

/**
 * @param context/*from  ww w .j av  a2s  .  com*/
 * @param baseClass  
 * @param defaultClass  Setting this to null will cause a ParseException if the runtime type information is incorrect or unavailable.
 */
public DefaultRuntimeTypeAdapterFactory(Context context, Class<E> baseClass, Class<? extends E> defaultClass,
        TypeAdapterFactory delegateFactory) {
    assert context != null && baseClass != null;
    if (defaultClass != null && !isInstantiable(defaultClass)) {
        throw new RuntimeException("Default class does not have a default contructor.");
    }
    this.context = context;
    this.baseClass = baseClass;
    this.defaultClass = defaultClass;
    if (delegateFactory == null) {
        this.delegateFactory = new ReflectiveTypeAdapterFactory(
                new ConstructorConstructor(Collections.<Type, InstanceCreator<?>>emptyMap()),
                FieldNamingPolicy.IDENTITY, Excluder.DEFAULT);
    } else {
        this.delegateFactory = delegateFactory;
    }
}

From source file:org.jclouds.json.config.GsonModule.java

License:Apache License

@SuppressWarnings("rawtypes")
@Provides//from   w w w  .  j av a 2  s.  com
@Singleton
Gson provideGson(TypeAdapter<JsonBall> jsonAdapter, DateAdapter adapter, ByteListAdapter byteListAdapter,
        ByteArrayAdapter byteArrayAdapter, PropertiesAdapter propertiesAdapter, JsonAdapterBindings bindings,
        CredentialsAdapterFactory credentialsAdapterFactory, OptionalTypeAdapterFactory optional,
        SetTypeAdapterFactory set, ImmutableSetTypeAdapterFactory immutableSet, MapTypeAdapterFactory map,
        MultimapTypeAdapterFactory multimap, IterableTypeAdapterFactory iterable,
        CollectionTypeAdapterFactory collection, ListTypeAdapterFactory list,
        ImmutableListTypeAdapterFactory immutableList, FluentIterableTypeAdapterFactory fluentIterable,
        ImmutableMapTypeAdapterFactory immutableMap, DefaultExclusionStrategy exclusionStrategy) {

    FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(
            ImmutableSet.of(new ExtractSerializedName(), new ExtractNamed()));

    GsonBuilder builder = new GsonBuilder().setFieldNamingStrategy(serializationPolicy)
            .setExclusionStrategies(exclusionStrategy);

    // simple (type adapters)
    builder.registerTypeAdapter(Properties.class, propertiesAdapter.nullSafe());
    builder.registerTypeAdapter(Date.class, adapter.nullSafe());
    builder.registerTypeAdapter(byte[].class, byteArrayAdapter.nullSafe());
    builder.registerTypeAdapter(JsonBall.class, jsonAdapter.nullSafe());
    builder.registerTypeAdapterFactory(credentialsAdapterFactory);
    builder.registerTypeAdapterFactory(optional);
    builder.registerTypeAdapterFactory(iterable);
    builder.registerTypeAdapterFactory(collection);
    builder.registerTypeAdapterFactory(list);
    builder.registerTypeAdapter(new TypeToken<List<Byte>>() {
    }.getType(), byteListAdapter.nullSafe());
    builder.registerTypeAdapterFactory(immutableList);
    builder.registerTypeAdapterFactory(set);
    builder.registerTypeAdapterFactory(immutableSet);
    builder.registerTypeAdapterFactory(map);
    builder.registerTypeAdapterFactory(multimap);
    builder.registerTypeAdapterFactory(fluentIterable);
    builder.registerTypeAdapterFactory(immutableMap);

    AnnotationConstructorNamingStrategy deserializationPolicy = new AnnotationConstructorNamingStrategy(
            ImmutableSet.of(ConstructorProperties.class, SerializedNames.class, Inject.class),
            ImmutableSet.of(new ExtractNamed()));

    builder.registerTypeAdapterFactory(new DeserializationConstructorAndReflectiveTypeAdapterFactory(
            new ConstructorConstructor(ImmutableMap.<Type, InstanceCreator<?>>of()), serializationPolicy,
            Excluder.DEFAULT, deserializationPolicy));

    // complicated (serializers/deserializers as they need context to operate)
    builder.registerTypeHierarchyAdapter(Enum.class, new EnumTypeAdapterThatReturnsFromValue());

    for (Map.Entry<Type, Object> binding : bindings.getBindings().entrySet()) {
        builder.registerTypeAdapter(binding.getKey(), binding.getValue());
    }

    for (TypeAdapterFactory factory : bindings.getFactories()) {
        builder.registerTypeAdapterFactory(factory);
    }

    return builder.create();
}