Rev 182 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 178 | espaco | 1 | package br.com.ec.ws.util; |
| 2 | |||
| 3 | import java.lang.reflect.Type; |
||
| 4 | import java.util.ArrayList; |
||
| 5 | import java.util.List; |
||
| 6 | |||
| 7 | import com.google.gson.Gson; |
||
| 8 | import com.google.gson.GsonBuilder; |
||
| 9 | import com.google.gson.internal.StringMap; |
||
| 10 | import com.sun.jersey.api.client.Client; |
||
| 11 | import com.sun.jersey.api.client.config.ClientConfig; |
||
| 12 | import com.sun.jersey.api.client.config.DefaultClientConfig; |
||
| 13 | import com.sun.jersey.api.json.JSONConfiguration; |
||
| 14 | |||
| 15 | public class JsonUtils { |
||
| 16 | |||
| 17 | private static Gson gsonComDataHora = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create(); |
||
| 18 | private static Gson gsonComData = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); |
||
| 19 | |||
| 20 | public static String gerarObjetoJsonComDataHora(Object objeto) { |
||
| 21 | return gsonComDataHora.toJson(objeto); |
||
| 22 | } |
||
| 23 | |||
| 24 | public static <T> T gerarObjetoInformadoComDataHora(String json, Class<T> objetoInformado){ |
||
| 25 | return gsonComDataHora.fromJson(json, objetoInformado); |
||
| 26 | } |
||
| 27 | |||
| 28 | public static String gerarObjetoJsonComData(Object objeto) { |
||
| 29 | return gsonComData.toJson(objeto); |
||
| 30 | } |
||
| 31 | |||
| 32 | public static <T> T gerarObjetoInformadoComData(String json, Class<T> objetoInformado){ |
||
| 33 | return gsonComData.fromJson(json, objetoInformado); |
||
| 34 | } |
||
| 35 | |||
| 36 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
||
| 37 | public static <T> List<T> gerarListaObjetoInformadoComDataHora(String json, Class<T> classeEsperada){ |
||
| 38 | List<T> lista = new ArrayList<T>(); |
||
| 39 | List<StringMap> listaObjetos = gsonComDataHora.fromJson(json, List.class); |
||
| 40 | for(StringMap objeto : listaObjetos){ |
||
| 41 | T objetoGerado = gsonComDataHora.fromJson(objeto.toString(), classeEsperada); |
||
| 42 | lista.add(objetoGerado); |
||
| 43 | } |
||
| 44 | return lista; |
||
| 45 | } |
||
| 46 | |||
| 47 | public static <T> List<T> gerarListaObjetoInformadoComData(String json, Type type){ |
||
| 48 | List<T> lista = gsonComData.fromJson(json, type); |
||
| 49 | return lista; |
||
| 50 | } |
||
| 51 | |||
| 52 | public static Client gerarClientJsonWs() { |
||
| 53 | ClientConfig clientConfig = new DefaultClientConfig(); |
||
| 54 | clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); |
||
| 55 | Client client = Client.create(clientConfig); |
||
| 56 | return client; |
||
| 57 | } |
||
| 58 | |||
| 59 | } |