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:se.sperber.cryson.serialization.CrysonSerializer.java

License:Apache License

@PostConstruct
public void setupGson() {
    jsonParser = new JsonParser();
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.serializeNulls();//w  w  w  .  j ava2 s  .com
    gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss Z");

    HibernateProxyTypeAdapter hibernateProxyTypeAdapter = new HibernateProxyTypeAdapter();
    gsonBuilder.registerTypeHierarchyAdapter(HibernateProxy.class, hibernateProxyTypeAdapter);

    gsonAllInclusive = gsonBuilder.create();

    gsonBuilder.setExclusionStrategies(lazyAssociationExclusionStrategy, userTypeExclusionStrategy,
            crysonExcludeExclusionStrategy);
    gson = gsonBuilder.create();

    hibernateProxyTypeAdapter.setGson(gson);
}

From source file:travel.izi.sdk.util.GsonHelper.java

License:Apache License

/**
 * Create a {@link com.google.gson.GsonBuilder} and register all of the custom types needed in order to properly
 * deserialize complex IZITravel-specific type.
 *
 * @return Assembled GSON builder instance.
 *//*from   ww  w  .  j  av a  2  s. com*/
public static GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    //enum types
    builder.registerTypeAdapter(Category.class, new JsonDeserializer<Category>() {
        @Override
        public Category deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Category.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(MediaType.class, new JsonDeserializer<MediaType>() {
        @Override
        public MediaType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return MediaType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(MtgObjectType.class, new JsonDeserializer<MtgObjectType>() {
        @Override
        public MtgObjectType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return MtgObjectType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(Placement.class, new JsonDeserializer<Placement>() {
        @Override
        public Placement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Placement.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(PlaybackType.class, new JsonDeserializer<PlaybackType>() {
        @Override
        public PlaybackType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return PlaybackType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(Status.class, new JsonDeserializer<Status>() {
        @Override
        public Status deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Status.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(TriggerZoneType.class, new JsonDeserializer<TriggerZoneType>() {
        @Override
        public TriggerZoneType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return TriggerZoneType.fromValue(json.getAsString());
        }
    });

    return builder;
}

From source file:view.GsonView.java

public static Gson CreateGson(boolean serializeNuls, String dateformat) {
    GsonBuilder gb = new GsonBuilder();
    if (serializeNuls) {
        gb = gb.serializeNulls();//  w ww  .j a  v a 2  s. c om
    }

    if ((dateformat != null) && (!dateformat.isEmpty())) {
        gb = gb.setDateFormat(dateformat);
    }

    return gb.create();
}

From source file:ws.logv.trainmonitor.api.JsonHelper.java

License:Apache License

public <T> T fromJson(String json, Type typeOfT) {
    GsonBuilder b = new GsonBuilder();
    b.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    b.registerTypeAdapter(TrainStatus.class, new TrainStatusDeserializer());
    Gson gson = b.create();/*from ww w  .j a  va  2  s  .  co m*/

    return gson.fromJson(json, typeOfT);
}

From source file:ws.logv.trainmonitor.api.JsonHelper.java

License:Apache License

public String toJson(Object data) {
    GsonBuilder b = new GsonBuilder();
    b.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Gson gson = b.create();/*from  www .ja v  a2 s .c  o  m*/

    return gson.toJson(data);
}