Exploring basic features of google's GSON library.
Reference: https://github.com/google/gson
We serialize / deserialize objects from / to json using GSON
.
- gson
- to json
def customerJson = new Gson().toJson(customer)
- from json
JsonReader reader = new JsonReader(new FileReader("src/test/resources/customerFull.json")) def customer = new Gson().fromJson(reader, Customer.class)
- with serializing nulls
Gson gson = new GsonBuilder().serializeNulls().create() def customerJson = gson.toJson(customer)
- pretty (well formatted) json
Gson gson = new GsonBuilder().setPrettyPrinting().create() println gson.toJson(customer)
- to json