By default, Jackson (the library Play JSON uses) will try to map every public field to a json field with the same name. If the object has getters/setters, it will infer the name from them. So, if you have a Book
class with a private field to store the ISBN and have get/set methods named getISBN/setISBN
, Jackson will
setISBN
method to define the isbn field in the Java object (if the JSON object has a "ISBN" field).public class Person {
String id, name;
}
JsonNode node = Json.parse("{\"id\": \"3S2F\", \"name\", \"Salem\"}");
Person person = Json.fromJson(node, Person.class);
System.out.println("Hi " + person.name); // Hi Salem
// "person" is the object from the previous example
JsonNode personNode = Json.toJson(person)
// personNode comes from the previous example
String json = personNode.toString();
// or
String json = Json.stringify(json);
System.out.println(personNode.toString());
/* Prints:
{"id":"3S2F","name":"Salem"}
*/
System.out.println(Json.prettyPrint(personNode));
/* Prints:
{
"id" : "3S2F",
"name" : "Salem"
}
*/