Example usage for com.google.gson GsonBuilder GsonBuilder

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

Introduction

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

Prototype

public GsonBuilder() 

Source Link

Document

Creates a GsonBuilder instance that can be used to build Gson with various configuration settings.

Usage

From source file:com.actimem.blog.gson.dates.SerializedFeatureDateDemo.java

License:Apache License

public static void main(String[] args) throws IOException {
    Company company = new Company();
    company.setName("Actimem");
    company.setFounded(new GregorianCalendar(2015, 5, 20).getTime());
    company.setUpdatedTs(new Date());

    // DateFormat.FULL
    // DateFormat.LONG
    // DateFormat.MEDIUM
    // DateFormat.SHORT
    Gson gson = new GsonBuilder().setDateFormat(DateFormat.MEDIUM).create();
    String json = gson.toJson(company);
    System.out.println(json);/*from w w  w  .  java2  s  . c o m*/

    Company company2 = gson.fromJson(json, Company.class);
    System.out.println(company2.toString());
}

From source file:com.actimem.blog.gson.dependencies.DependencyDemo.java

License:Apache License

public static void main(String[] args) throws IOException {
    Company company = new Company();
    company.setName("Actimem");
    company.setAddress(new Address("10 Street Name", "London"));
    company.add(new Employee("John", 10));
    company.add(new Employee("Michael", 10));

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(company);
    System.out.println(json);//from  w ww . ja v  a 2s.c om

    Company company2 = gson.fromJson(json, Company.class);
    System.out.println(company2);
}

From source file:com.actimem.blog.gson.emptyproperties.IgnoreNullsDemo.java

License:Apache License

public static void main(String[] args) throws IOException {
    Company company = new Company();
    Gson gson = new GsonBuilder().serializeNulls().create();
    String json = gson.toJson(company);
    System.out.println(json);//from   ww  w .j a v  a  2s.  c  om
}

From source file:com.actimem.blog.gson.immutable.ImmutableDemoWithSerializer.java

License:Apache License

public static void main(String[] args) throws IOException {
    Company company = new Company("Actimem", 1999, 3.5);

    Gson gson = new GsonBuilder().registerTypeAdapter(Company.class, new CompanyDeserializer()).create();
    String json = gson.toJson(company);
    System.out.println(json);/* w ww.  ja v  a  2s .c om*/

    Company company2 = gson.fromJson(json, Company.class);
    System.out.println(company2.toString());
}

From source file:com.actimem.blog.gson.map.MapDemo.java

License:Apache License

public static void main(String[] args) throws IOException {
    Company company = new Company();
    company.setName("Actimem");
    company.addEmployeeDepartment("John", "IT");
    company.addEmployeeDepartment("William", "Accounts");
    company.addEmployeeDepartment("Alfred", "Sales");

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(company);
    System.out.println(json);/*from w w w.j  a va2s.co  m*/

    Company company2 = gson.fromJson(json, Company.class);
    System.out.println(company2);
}

From source file:com.actionml.entity.Event.java

License:Apache License

@Override
public String toString() {
    // handle DateTime separately
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeAdapter());
    Gson gson = gsonBuilder.create();/*from   w  w w  .ja v  a  2s.  c  o  m*/
    return gson.toJson(this); // works when there are no generic types
}

From source file:com.addhen.voto.sdk.BaseApiBuilder.java

License:Apache License

private void initializeGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.registerTypeAdapter(Date.class, new DateDeserializer());
    mGson = builder.create();/*www .  j a  v  a 2  s . co  m*/
}

From source file:com.addhen.voto.sdk.test.BaseTestCase.java

License:Apache License

@Before
public void setUp() throws Exception {
    mGson = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer())
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    mGsonDeserializer = new GsonDeserializer(mGson);
}

From source file:com.adnanbal.fxdedektifi.forex.data.entity.mapper.NewSignalEntityJsonMapper.java

License:Apache License

@Inject
public NewSignalEntityJsonMapper() {
    this.gson = new GsonBuilder()
            .registerTypeAdapter(Date.class,
                    (JsonDeserializer<Date>) (jsonElement, type,
                            context) -> new Date(jsonElement.getAsJsonPrimitive().getAsLong()))
            .registerTypeAdapter(Date.class,
                    (JsonSerializer<Date>) (date, type,
                            jsonSerializationContext) -> new JsonPrimitive(date.getTime()))

            .create();/*w w  w .  j  a  va2s .  c o  m*/

}

From source file:com.adnanbal.fxdedektifi.forex.data.entity.mapper.SignalEntityJsonMapper.java

License:Apache License

@Inject
public SignalEntityJsonMapper() {
    this.gson = new GsonBuilder()
            .registerTypeAdapter(Date.class,
                    (JsonDeserializer<Date>) (jsonElement, type,
                            context) -> new Date(jsonElement.getAsJsonPrimitive().getAsLong()))
            .registerTypeAdapter(Date.class,
                    (JsonSerializer<Date>) (date, type,
                            jsonSerializationContext) -> new JsonPrimitive(date.getTime()))

            .create();/*from  w  ww .jav a 2s. c  om*/

}