Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder create.

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:br.com.ateliens.service.CadastroEnderecos.java

public Endereco buscarEnderecoPor(String urlJson) {
    System.out.println("CHAMOU O SERVIO....");

    final GsonBuilder gsonBuilder = new GsonBuilder();
    final Gson gson = gsonBuilder.create();
    Gson g = new Gson();
    Endereco retornoEndereco = gson.fromJson(urlJson, Endereco.class);
    return retornoEndereco;

}

From source file:br.com.davimonteiro.lotus_runtime.config.ConfigurationUtil.java

License:Open Source License

public static Configuration load(Path configPath) {
    Configuration config = null;/* w w w. jav  a 2s .  c  o  m*/

    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();

    try {
        config = gson.fromJson(new FileReader(configPath.toFile()), Configuration.class);
    } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
        e.printStackTrace();
    }

    return config;
}

From source file:br.com.davimonteiro.lotus_runtime.config.ConfigurationUtil.java

License:Open Source License

public static Configuration load(String jsonConfig) {
    Configuration config = null;// www .ja  v a  2  s.com

    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();

    config = gson.fromJson(jsonConfig, Configuration.class);

    return config;
}

From source file:br.com.financemate.bean.wsCep.ServicoEndereco.java

public EnderecoBean buscarEnderecoPor(String urlJson) {
    final GsonBuilder gsonBuilder = new GsonBuilder();
    final Gson gson = gsonBuilder.create();
    EnderecoBean retornoEndereco = gson.fromJson(urlJson, EnderecoBean.class);
    return retornoEndereco;
}

From source file:br.com.gdgtresrios.sicomerciows.resource.GsonMessageBodyHandler.java

private Gson getGson() {
    if (gson == null) {
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.create();
    }// w ww .ja  v  a 2 s.c  om
    return gson;
}

From source file:br.ufg.inf.es.saep.sandbox.dominio.infraestrutura.Serializador.java

License:Creative Commons License

/**
 * Cria instncia de serializar preparada
 * para realizar converses entre objetos e
 * sequncias de caracters./*from w w  w. j  a  v a 2s  .c  om*/
 */
public Serializador() {
    GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(Valor.class, new ValorSerializer());
    gb.registerTypeAdapter(Valor.class, new ValorDeserializer());
    gson = gb.create();

    valorType = new TypeToken<Valor>() {
    }.getType();
    pontuacaoType = new TypeToken<Pontuacao>() {
    }.getType();
}

From source file:br.ufmg.hc.telessaude.webservices.mobile.utils.GsonUtils.java

public static Gson getInstanceWithStringDateAdapter() {
    GsonBuilder builder = new GsonBuilder();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    //        final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a");
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override/*from w ww.  ja  v a  2s.c o m*/
        public Date deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {
            String data_str = "";
            Date data = null;
            try {
                data_str = je.getAsJsonPrimitive().getAsString();
                if (data_str.replaceAll("[\\-\\d]", "").isEmpty()) {
                    data = new Date(Long.parseLong(data_str));

                } else {
                    if (data_str.length() == 12) {
                        data = new SimpleDateFormat("MMM dd, yyyy").parse(data_str);
                    } else if (data_str.length() == 11) {
                        data = new SimpleDateFormat("MMM d, yyyy").parse(data_str);
                    } else if (data_str.length() == 10) {
                        data = new SimpleDateFormat("dd/MM/yyyy").parse(data_str);
                    } else {

                        data = sdf.parse(data_str);
                    }
                }
            } catch (ParseException ex) {
                Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
            return data;
        }
    });
    builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date t, Type type, JsonSerializationContext jsc) {
            if (t == null) {
                return new JsonPrimitive((String) null);
            }
            return new JsonPrimitive(sdf.format(t));
        }
    });

    builder.registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {
        @Override
        public JsonElement serialize(java.sql.Date t, Type type, JsonSerializationContext jsc) {
            if (t == null) {
                return new JsonPrimitive((String) null);
            }
            return new JsonPrimitive(sdf.format(t));
        }
    });
    return builder.create();
}

From source file:buri.ddmsence.AbstractBaseComponent.java

License:Open Source License

/**
 * @see IDDMSComponent#toJSON()//from   w ww.  jav a2  s  . c  om
 */
public String toJSON() {
    GsonBuilder builder = new GsonBuilder();
    if (Boolean.valueOf(PropertyReader.getProperty("output.json.prettyPrint"))) {
        builder.setPrettyPrinting();
    }
    return (builder.create().toJson(getJSONObject()));
}

From source file:ca.cmput301w14t09.elasticSearch.ElasticSearchOperations.java

License:GNU General Public License

/**
 * This method creates is what makes the bitmap into a json serialzable format
 *//*from ww  w.j  a v  a 2s . co  m*/
private static void constructGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Bitmap.class, new JsonBitmapConverter());
    GSON = builder.create();
}

From source file:ca.cs.ualberta.localpost.controller.ElasticSearchOperations.java

License:Open Source License

/**
 * Constructs a Gson with a custom serializer / desserializer registered for
 * Bitmaps.//from   w  w w . jav  a  2  s. c o  m
 */
private static void constructGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Bitmap.class, new BitmapJsonConverter());
    gson = builder.create();
}