Java Json getFromGson(String json, Class clazz)

Here you can find the source of getFromGson(String json, Class clazz)

Description

get From Gson

License

Apache License

Declaration

public static <T> T getFromGson(String json, Class<T> clazz) 

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   w w w. j  a va 2  s .c  o  m*/
            }).create();

    public static <T> T getFromGson(String json, Class<T> clazz) {
        return G.fromJson(json, clazz);
    }
}

Related

  1. fill_dictionary(Map dictionary, JsonObject jsonObject)
  2. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
  3. fill_list(JsonObjectBuilder jsonObject, String key, List list)
  4. fromDictionary(Map dictionary)
  5. getAsJSONArray(Object obj)
  6. getJSONDate(long l)
  7. getJsonMapper()
  8. hasKey(JsonObject object, String name)
  9. jsonEquals(JsonArray expected, JsonArray actual, boolean strict)

  10. HOME | Copyright © www.java2s.com 2016