Example usage for com.google.gson GsonBuilder enableComplexMapKeySerialization

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

Introduction

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

Prototype

public GsonBuilder enableComplexMapKeySerialization() 

Source Link

Document

Enabling this feature will only change the serialized form if the map key is a complex type (i.e.

Usage

From source file:org.jeeventstore.serialization.gson.EventSerializerGson.java

License:Open Source License

/**
 * Create the underlying GsonBuilder./*from w  w  w  .  j a  v  a 2 s.  com*/
 * Added here to test proper serialization in integration tests.
 */
public static GsonBuilder createBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(EventList.class, new EventListTypeConverter());
    builder.serializeSpecialFloatingPointValues(); // required to serialize Double.POSITIVE_INFINITY and others
    builder.enableComplexMapKeySerialization(); // required to properly serialize maps
    builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // granularity of 1 ms
    return builder;
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

/**
 * @return the gson//from   w w  w .  ja v  a 2s .c  o m
 */
public Gson getGson() {
    if (gson == null) {
        GsonBuilder gbuilder = new GsonBuilder();
        if (serializeNulls) {
            gbuilder.serializeNulls();
        }
        gbuilder.setDateFormat(datePattern);
        if (prettyPrinting) {
            gbuilder.setPrettyPrinting();
        }
        if (excludeFieldsWithoutExposeAnnotation) {
            gbuilder.excludeFieldsWithoutExposeAnnotation();
        }
        if (enableComplexMapKey) {
            gbuilder.enableComplexMapKeySerialization();
        }
        if (version != null) {
            gbuilder.setVersion(version);
        }
        gson = gbuilder.create();
    }
    return gson;
}

From source file:org.yccheok.jstock.portfolio.PortfolioRealTimeInfo.java

License:Open Source License

public boolean save(File file) {
    GsonBuilder builder = new GsonBuilder();
    builder.enableComplexMapKeySerialization();
    Gson gson = builder.create();//from ww w  .  ja  va2  s.c o  m
    String string = gson.toJson(this);

    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        try {
            writer.write(string);
        } finally {
            writer.close();
        }
    } catch (IOException ex) {
        log.error(null, ex);
        return false;
    }

    return true;
}

From source file:src.MainApp.java

/**
 * Constructor./*from w w w  .j  a v a 2 s .  co m*/
 */
public MainApp() {

    initUser();

    GsonBuilder builder = new GsonBuilder();
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.enableComplexMapKeySerialization();
    gson = FxGson.addFxSupport(builder).create();

    //setCurrUser(new Profile(new User("", "", "", 123, "", 0, 0, new DriversLicense("",0)),"test"));
    rideContainer = new RideOverview();
    //If file exists, load
    /**File ride = new File("src/main/resources/ridedata.json");
    File user = new File("src/main/resources/userdata.json");
    if (ride.exists() && user.exists())
    {
       try {
    currUser = load();
       } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
       }
    } else {
       currUser = new Profile(new User("", ""));
       rideContainer = new RideOverview();
    }
    if (currUser == null){
       System.out.print(":(");
    } else {
       System.out.println(currUser.getUserRoutes());
    } */
}