Java Json Create getJson(T t)

Here you can find the source of getJson(T t)

Description

get Json

License

Apache License

Declaration

public static <T> String getJson(T t) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class Main {
    private static final Gson G = new GsonBuilder().setDateFormat(DateFormat.LONG)
            .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }/*from  ww w. jav  a2s .  c  o m*/
            }).create();

    public static <T> String getJson(T t) {
        return G.toJson(t);
    }
}

Related

  1. createArrayBuilder()
  2. createJsonFrom(JsonObject user, String... ignoreKeys)
  3. createJsonValue(String string)
  4. createPrettyWriterFactory()
  5. emptyArray()
  6. object2JsonDateSerializer(Object obj, final String dateformat)
  7. toBean(Class clazz, String json)
  8. toJson(E e)
  9. toJSON(Map map)