Example usage for com.google.gson GsonBuilder setLongSerializationPolicy

List of usage examples for com.google.gson GsonBuilder setLongSerializationPolicy

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setLongSerializationPolicy.

Prototype

public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) 

Source Link

Document

Configures Gson to apply a specific serialization policy for Long and long objects.

Usage

From source file:io.weba.processor.flink.event.gson.factory.EventFactory.java

License:Open Source License

public Gson create() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer());
    gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer());
    gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer());
    gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer());
    gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer());
    gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer());
    gsonBuilder.serializeNulls();//from  w w w  .  j  a v a 2 s  . c  om
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    gsonBuilder.enableComplexMapKeySerialization();

    return gsonBuilder.create();
}

From source file:org.apache.camel.component.gson.GsonDataFormat.java

License:Apache License

@Override
protected void doStart() throws Exception {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        if (exclusionStrategies != null && !exclusionStrategies.isEmpty()) {
            ExclusionStrategy[] strategies = exclusionStrategies
                    .toArray(new ExclusionStrategy[exclusionStrategies.size()]);
            builder.setExclusionStrategies(strategies);
        }//from   www . jav  a  2  s .co m
        if (longSerializationPolicy != null) {
            builder.setLongSerializationPolicy(longSerializationPolicy);
        }
        if (fieldNamingPolicy != null) {
            builder.setFieldNamingPolicy(fieldNamingPolicy);
        }
        if (fieldNamingStrategy != null) {
            builder.setFieldNamingStrategy(fieldNamingStrategy);
        }
        if (serializeNulls != null && serializeNulls) {
            builder.serializeNulls();
        }
        if (prettyPrinting != null && prettyPrinting) {
            builder.setPrettyPrinting();
        }
        if (dateFormatPattern != null) {
            builder.setDateFormat(dateFormatPattern);
        }
        gson = builder.create();
    }
}

From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelModelProvider.java

License:Open Source License

static Gson createGson() {
    final GsonBuilder builder = new GsonBuilder();

    builder.setPrettyPrinting();/*from  ww  w. j a  va 2  s  . co  m*/
    builder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    builder.setDateFormat(DATE_FORMAT);
    builder.registerTypeAdapter(MetaKey.class, new JsonDeserializer<MetaKey>() {

        @Override
        public MetaKey deserialize(final JsonElement json, final Type type,
                final JsonDeserializationContext ctx) throws JsonParseException {
            return MetaKey.fromString(json.getAsString());
        }
    });

    return builder.create();
}

From source file:org.jc.avrotokudu.consumer.AvroConsumer.java

public AvroConsumer(String consumerGroup, String topic, int partitionToConsume, String[] seedBrokers,
        int soTimeout, int port, int maxEmptyPartitionReadTries, String tableName, String[] masterAddresses) {
    super(consumerGroup, topic, partitionToConsume, seedBrokers, soTimeout, port, maxEmptyPartitionReadTries);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);

    this.gson = gsonBuilder.create();
    try {/*  ww  w .ja v a  2  s . c  o m*/
        AvroConsumer.kuduHelper = KuduHelperFactory.buildKuduHelper(tableName, masterAddresses);
        AvroConsumer.kuduHelper.init(KuduHelper.KUDU_SESSION_ASYNC,
                SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND);
    } catch (Exception ex) {
        Logger.getLogger(AvroConsumer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.jc.kududbhelper.lib.KuduHelper.java

/**
 * Method to process data in a json array. Usually invoked when invoking
 * from cli commands. Notice that when KuduHelper is included as an external
 * jar inside a given project, this is not the preferred method.
 *
 * @param jsonArray an array of json objects with the format
 * Map<String, String>/*from  ww  w . j  a  v a  2  s  .  c  o m*/
 * @throws InterruptedException This exception indicates that an async kudu
 * table did not successfully joined within timeout millis.
 * @throws Exception An exception when inserting data, parsing values to be
 * inserted or invalid data types.
 */
public void processJson(String jsonArray) throws InterruptedException, IllegalStateException, Exception {
    GsonBuilder gsonBuilder = new GsonBuilder();
    java.lang.reflect.Type type = new TypeToken<List<Map<String, String>>>() {
    }.getType();
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    Gson gson = gsonBuilder.create();
    List<Map<String, String>> data = gson.fromJson(jsonArray, type);
    if (data.isEmpty()) {
        throw new Exception("Cannot process an empty data list.");
    }

    this.processObjectsAsMap(data);
}