Example usage for com.google.gson JsonSerializer JsonSerializer

List of usage examples for com.google.gson JsonSerializer JsonSerializer

Introduction

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

Prototype

JsonSerializer

Source Link

Usage

From source file:org.withinsea.izayoi.core.serialize.GsonJsonSerializer.java

License:Mozilla Public License

protected Gson createGson() {
    return new GsonBuilder().serializeNulls()
            .excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
            .registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override/*from   w  w  w  .  j  a  v  a2  s.c  om*/
                public JsonElement serialize(Date date, Type type,
                        JsonSerializationContext jsonSerializationContext) {
                    return new JsonPrimitive(date.getTime());
                }
            }).registerTypeAdapter(Calendar.class, new JsonSerializer<Calendar>() {
                @Override
                public JsonElement serialize(Calendar calendar, Type type,
                        JsonSerializationContext jsonSerializationContext) {
                    return new JsonPrimitive(calendar.getTime().getTime());
                }
            }).create();
}

From source file:xyz.yhsj.easynet.factory.ConverterFactoryPro.java

License:Apache License

public static ConverterFactoryPro create() {

    Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
        @Override/*w w w . jav  a 2 s  .  c o m*/
        public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == src.longValue())
                return new JsonPrimitive(src.longValue());
            return new JsonPrimitive(src);
        }
    }).create();

    return create(gson);
}