A java .jar easing the use of the Gson library. The class GsonUtils offers static methods to quickly:
- load an object from a json file
- serialize an object into a string
- serialize an object into a file
- exclude fields from the serialization with the
@DoNotSerializeannotation.
Add the jar to the classpath.
To serialize an object into a file, you two methods, taking either a path to a file or the file itself. For example, if you have an object, you can do:
MyObject myObject = new MyObject();
GsonUtils.writeToFile("/path/to/a/file.json", myObject, true);The boolean parameter determines if the output should be prettyprinted or not.
If your object is a collection or another generic object, you can use the TypeToken class, like that:
Map<String,MyObject> map = new HashMap<>();
GsonUtils.writeToFile("/path/to/a/file.json", new TypeToken<HashMap<String,MyObject>>(){}, true);To deserialize a json file to an object, the principle is the same:
MyObject myObject =v (MyObject) GsonUtils.getFromFile("/path/to/a/file.json", new MyObject());
Map<String,MyObject> map = (HashMap<String,MyObject>)
GsonUtils.getFromFile("/path/to/a/file.json", new TypeToken<HashMap<String,MyObject>>(){});Finally, you can serialize an object into a string. The method dump serialize every field, the method toJson will
exclude any field annotated with @DoNotSerialize.