Google Json Java

Gson is a Google Open Source library to convert Java Objects into its JSON representation. Very clean and simple. 🙂

It’s my two cent for this project. I add locale option for Date format.

public GsonBuilder setDateFormat(int style,Locale locale);
public GsonBuilder setDateFormat(int dateStyle, int timeStyle, Locale locale);

Code exemple:
Gson gson = new GsonBuilder().setDateFormat(DateFormat.MEDIUM, DateFormat.SHORT,Locale.US).create();
String json = gson.toJson(Object);

Patch file:

Index: DefaultTypeAdapters.java
===================================================================
— DefaultTypeAdapters.java (revision 557)
+++ DefaultTypeAdapters.java (working copy)
@@ -254,10 +254,18 @@
DefaultDateTypeAdapter(final int style) {
this.format = DateFormat.getDateInstance(style);
}
+
+ DefaultDateTypeAdapter(final int style, final Locale aLocale) {
+ this.format = DateFormat.getDateInstance(style,aLocale);
+ }

public DefaultDateTypeAdapter(final int dateStyle, final int timeStyle) {
this.format = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
}
+
+ public DefaultDateTypeAdapter(final int dateStyle, final int timeStyle, final Locale aLocale) {
+ this.format = DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale);
+ }

// These methods need to be synchronized since JDK DateFormat classes are not thread-safe
// See issue 162
Index: GsonBuilder.java
===================================================================
— GsonBuilder.java (revision 557)
+++ GsonBuilder.java (working copy)
@@ -23,6 +23,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
+import java.util.Locale;

import com.google.gson.DefaultTypeAdapters.DefaultDateTypeAdapter;

@@ -77,6 +78,7 @@
private String datePattern;
private int dateStyle;
private int timeStyle;
+ private Locale locale;
private boolean serializeSpecialFloatingPointValues;
private boolean escapeHtmlChars;
private boolean prettyPrinting;
@@ -108,6 +110,7 @@
serializeNulls = false;
dateStyle = DateFormat.DEFAULT;
timeStyle = DateFormat.DEFAULT;
+ locale = null;
serializeSpecialFloatingPointValues = false;
generateNonExecutableJson = false;
}
@@ -319,6 +322,28 @@
this.datePattern = null;
return this;
}
+
+ /**
+ * Configures Gson to to serialize {@code Date} objects according to the style value provided.
+ * You can call this method or {@link #setDateFormat(String)} multiple times, but only the last
+ * invocation will be used to decide the serialization format.
+ *
+ *

Note that this style value should be one of the predefined constants in the
+ * {@code DateFormat} class. See the documentation in {@link java.text.DateFormat} for more
+ * information on the valid style constants.

+ *
+ * @param style the predefined date style that date objects will be serialized/deserialized
+ * to/from
+ * @param locale the given locale
+ * @return a reference to this {@code GsonBuilder} object to fulfill the “Builder” pattern
+ * @since 1.2
+ */
+ public GsonBuilder setDateFormat(int style,Locale aLocale) {
+ this.dateStyle = style;
+ this.datePattern = null;
+ this.locale = aLocale;
+ return this;
+ }

/**
* Configures Gson to to serialize {@code Date} objects according to the style value provided.
@@ -341,8 +366,31 @@
this.datePattern = null;
return this;
}

+
/**
+ * Configures Gson to to serialize {@code Date} objects according to the style value provided.
+ * You can call this method or {@link #setDateFormat(String)} multiple times, but only the last
+ * invocation will be used to decide the serialization format.
+ *
+ *

Note that this style value should be one of the predefined constants in the
+ * {@code DateFormat} class. See the documentation in {@link java.text.DateFormat} for more
+ * information on the valid style constants.

+ *
+ * @param dateStyle the predefined date style that date objects will be serialized/deserialized
+ * to/from
+ * @param timeStyle the predefined style for the time portion of the date objects
+ * @param locale the given locale
+ * @return a reference to this {@code GsonBuilder} object to fulfill the “Builder” pattern
+ * @since
+ */
+ public GsonBuilder setDateFormat(int dateStyle, int timeStyle, Locale aLocale) {
+ this.dateStyle = dateStyle;
+ this.timeStyle = timeStyle;
+ this.datePattern = null;
+ this.locale = aLocale;
+ return this;
+ }
+ /**
* Configures Gson for custom serialization or deserialization. This method combines the
* registration of an {@link InstanceCreator}, {@link JsonSerializer}, and a
* {@link JsonDeserializer}. It is best used when a single object {@code typeAdapter} implements
@@ -527,7 +575,7 @@

ParameterizedTypeHandlerMap<JsonSerializer> customSerializers = serializers.copyOf();
ParameterizedTypeHandlerMap<JsonDeserializer> customDeserializers = deserializers.copyOf();
– addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, customSerializers,
+ addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, locale, customSerializers,
customDeserializers);

customSerializers.registerIfAbsent(DefaultTypeAdapters.getDefaultSerializers(
@@ -553,7 +601,7 @@
return gson;
}

– private static void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,
+ private static void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,Locale locale,
ParameterizedTypeHandlerMap<JsonSerializer> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer> deserializers) {
if (!serializers.hasSpecificHandlerFor(Date.class)
@@ -562,10 +610,12 @@
DefaultDateTypeAdapter dateTypeAdapter = null;
if (datePattern != null && !””.equals(datePattern.trim())) {
dateTypeAdapter = new DefaultDateTypeAdapter(datePattern);
+ } else if (locale != null) {
+ dateTypeAdapter = new DefaultDateTypeAdapter(dateStyle, timeStyle,locale);
} else if (dateStyle != DateFormat.DEFAULT && timeStyle != DateFormat.DEFAULT) {
dateTypeAdapter = new DefaultDateTypeAdapter(dateStyle, timeStyle);
}

+
if (dateTypeAdapter != null) {
serializers.register(Date.class, dateTypeAdapter);
deserializers.register(Date.class, dateTypeAdapter);

Advertisement

One thought on “Google Json Java

  1. Hey,

    just in case someone else is looking for a way to apply a certain locale:

    public String getJSON(final String dateFormat, final Locale locate){

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new JsonSerializer(){

    @Override
    public JsonElement serialize(Date arg0, Type arg1, JsonSerializationContext arg2){
    SimpleDateFormat format = new SimpleDateFormat(dateFormat, locate);
    return new JsonPrimitive(format.format(arg0).toLowerCase());
    }
    });

    Gson gson = builder.create();
    return gson.toJson(this);
    }

    Thanks,

    Kon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s