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:fr.dudie.keolis.gson.LineAlertDataDeserializer.java

License:Open Source License

@Override
public LineAlertData deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {

    if (json instanceof JsonPrimitive) {
        return new LineAlertData();
    } else {/*from  w  w  w  .  j a  v  a2  s  .  com*/
        // TOBO is there a way to avoid creating a new Gson ?
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        // type adapter because list of only one element returned by keolis are not a JsonArray
        // but a JsonObject.
        final Type listOfLinesType = new TypeToken<List<LineAlert>>() {
        }.getType();
        gsonBuilder.registerTypeAdapter(listOfLinesType, new ListOfLineAlertDeserializer());

        return gsonBuilder.create().fromJson(json, typeOfT);

    }
}

From source file:fr.itinerennes.api.client.gson.ItineRennesApiGsonFactory.java

License:Open Source License

public static Gson newInstance(final boolean prettyJson) {
    final GsonBuilder gb = new GsonBuilder();
    gb.setDateFormat(DATE_FORMAT);
    gb.addSerializationExclusionStrategy(new ExcludeAnnotationStrategy());
    gb.registerTypeAdapter(Time.class, new TimeTypeAdapter());
    gb.registerTypeAdapter(Locale.class, new LocaleTypeAdapter());
    gb.registerTypeAdapter(TimeZone.class, new TimeZoneTypeAdapter());
    if (prettyJson) {
        gb.setPrettyPrinting();//from   www  . j av a2 s  .c  o m
    }
    return gb.create();
}

From source file:game.com.entity.BaseEntity.java

public String toJsonString() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat(AppConfig.dateFormat);
    Gson gson = gsonBuilder.create();//from   w ww . j av a2 s .  c  om
    return gson.toJson(this);
}

From source file:gov.nasa.arc.geocam.memo.service.DjangoMemoJsonConverterImplementation.java

License:Open Source License

@Override
public List<GeoCamMemoMessage> deserializeList(String jsonString) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");
    Gson gson = builder.create();//from   w  ww  . j a  va2  s  .  c o  m

    Type listType = new TypeToken<List<GeoCamMemoMessage>>() {
    }.getType();

    return gson.fromJson(jsonString, listType);
}

From source file:gov.nasa.arc.geocam.memo.service.DjangoMemoJsonConverterImplementation.java

License:Open Source License

public GeoCamMemoMessage deserialize(String jsonString) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");

    Gson gson = builder.create();//w w w.j a  v a  2s.c om
    return gson.fromJson(jsonString, GeoCamMemoMessage.class);
}

From source file:gov.nasa.arc.geocam.memo.service.DjangoMemoJsonConverterImplementation.java

License:Open Source License

@Override
public String serialize(GeoCamMemoMessage message) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");

    Gson gson = builder.create();/* ww  w .java  2  s  .c o  m*/
    String ret = gson.toJson(message);
    return ret;
}

From source file:gov.nasa.arc.geocam.talk.service.TalkJsonConverter.java

License:Open Source License

@Override
public List<GeoCamTalkMessage> deserializeList(String jsonString) {
    JsonParser parser = new JsonParser();
    JsonObject root = parser.parse(jsonString).getAsJsonObject();

    String jsonMessages = root.get("ms").toString();

    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");
    Gson gson = builder.create();/*  www .j  av a 2 s. c  o m*/

    Type listType = new TypeToken<List<GeoCamTalkMessage>>() {
    }.getType();

    return gson.fromJson(jsonMessages, listType);
}

From source file:gov.nasa.arc.geocam.talk.service.TalkJsonConverter.java

License:Open Source License

@Override
public GeoCamTalkMessage deserialize(String jsonString) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");

    Gson gson = builder.create();/*from  w  ww . j  a va  2  s.  c  o  m*/
    return gson.fromJson(jsonString, GeoCamTalkMessage.class);
}

From source file:gov.nasa.arc.geocam.talk.service.TalkJsonConverter.java

License:Open Source License

@Override
public String serialize(GeoCamTalkMessage message) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("MM/dd/yy HH:mm:ss");

    builder.setExclusionStrategies(new TalkMessageExclusionStrategy());
    Gson gson = builder.create();/* www  . j  a  v a 2 s. c o  m*/
    String ret = gson.toJson(message);
    return ret;
}

From source file:io.motown.ocpp.websocketjson.gson.GsonFactoryBean.java

License:Apache License

public Gson createGson() {
    GsonBuilder builder = new GsonBuilder();

    if (dateFormat != null && !dateFormat.isEmpty()) {
        builder.setDateFormat(dateFormat);
    }/* w  ww .  j  a  v a2 s .co  m*/

    for (TypeAdapterSerializer<?> typeAdapter : typeAdapterSerializers) {
        builder.registerTypeAdapter(typeAdapter.getAdaptedType(), typeAdapter);
    }

    for (TypeAdapterDeserializer<?> typeAdapter : typeAdapterDeserializers) {
        builder.registerTypeAdapter(typeAdapter.getAdaptedType(), typeAdapter);
    }

    return builder.create();
}