Example usage for com.google.gson GsonBuilder setDateFormat

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

Introduction

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

Prototype

public GsonBuilder setDateFormat(int style) 

Source Link

Document

Configures Gson to to serialize Date objects according to the style value provided.

Usage

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   w  ww  . 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.eclipse.packagedrone.repo.channel.impl.ChannelInstanceModelProvider.java

License:Open Source License

private static Gson createGson() {
    final GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();/*  w w w  . jav  a2s.c  o m*/
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return builder.create();
}

From source file:org.eclipse.packagedrone.repo.channel.impl.ChannelServiceModelProvider.java

License:Open Source License

protected Gson createGson() {
    final GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();//  ww  w.  ja  va 2 s  .c  om
    builder.serializeNulls();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    builder.registerTypeAdapter(DeployGroup.class, new DeployGroupTypeAdapter());
    builder.registerTypeAdapter(MetaKey.class, MetaKeyTypeAdapter.INSTANCE);
    return builder.create();
}

From source file:org.foxbpm.web.common.util.JSONUtil.java

License:Apache License

private static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
        String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null)
        return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls)
        builder.serializeNulls();//w  w w . ja  va2  s .c  om
    if (version != null)
        builder.setVersion(version.doubleValue());
    // if (StringUtil.isEmpty(datePattern))
    // datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose)
        builder.excludeFieldsWithoutExposeAnnotation();
    String result = null;
    Gson gson = builder.create();
    try {
        if (targetType != null) {
            result = gson.toJson(target, targetType);
        } else {
            result = gson.toJson(target);
        }
    } catch (Exception ex) {
        if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration
                || target.getClass().isArray()) {
            result = EMPTY_JSON_ARRAY;
        } else
            result = EMPTY_JSON;
    }
    return result;
}

From source file:org.headsupdev.agile.api.rest.Api.java

License:Open Source License

private void setupBuilder() {
    GsonBuilder builder = getBuilder();
    builder.setDateFormat("yyyy-MM-dd'T'HH:mmZ"); // ISO 8601 date/time format
    if (respectPublishAnnotation()) {
        // Prefer to only expose the fields I have annotated
        builder.setExclusionStrategies(new MissingPublishExclusionStrategy());
    }/*from  w w w  . ja  v  a  2  s .  c o m*/

    setupJson(builder);
}

From source file:org.jeeventstore.serialization.gson.EventSerializerGson.java

License:Open Source License

/**
 * Create the underlying GsonBuilder./*  w  w w  . jav  a2  s.  c om*/
 * Added here to test proper serialization in integration tests.
 */
public static GsonBuilder createBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(EventList.class, new EventListTypeConverter());
    builder.serializeSpecialFloatingPointValues(); // required to serialize Double.POSITIVE_INFINITY and others
    builder.enableComplexMapKeySerialization(); // required to properly serialize maps
    builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // granularity of 1 ms
    return builder;
}

From source file:org.jenkinsci.plugins.fod.FoDAPI.java

protected Gson getGson() {

    if (null == gson) {
        GsonBuilder builder = new GsonBuilder();

        //2013-11-13T04:35:48.28
        //FIXME Gson creates dates assuming local time, but dates are probably UTC
        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");

        gson = builder.create();/*  w ww. j a v a2s  .co m*/
    }
    return gson;
}

From source file:org.lastaflute.core.json.engine.GsonJsonEngine.java

License:Apache License

protected void registerUtilDateFormat(GsonBuilder builder) {
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // same as local date-time
}

From source file:org.ndogen.util.FileUtils.java

License:Open Source License

public static String objectToJson(Object o) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Gson gson = gsonBuilder.create();/* w w w  .j  a v a 2s. co  m*/
    String json = gson.toJson(o);

    return json;
}

From source file:org.openlaszlo.remote.json.LZGsonMarshaller.java

License:Open Source License

@Override
public byte[] createObject(Object object, String objectReturnType) {
    try {//from  w  w w .jav  a2 s .c  om

        GsonBuilder _gsonBuilder = getGsonBuilderInstance();

        if (applyLzDefaultGsonProperties) {
            _gsonBuilder.setDateFormat("dd/MM/yyyy HH:mm:ss");
            _gsonBuilder.serializeNulls();
            _gsonBuilder.setPrettyPrinting();
        }

        Gson gson = _gsonBuilder.create();

        //        String buf2 = new LZReturnObject(objectReturnType)
        //            .createObjectProgram(object);
        //        
        //        mLogger.error("LZReturnObject1:"+buf);
        //        mLogger.error("LZReturnObject2:"+buf2);

        String jsonOutput = gson.toJson(object);

        return jsonOutput.getBytes("UTF-8");

    } catch (Exception err) {
        mLogger.error("[createObject]", err);
    }
    return null;
}