JsonSerializer
serializes objects into JSON strings. Create an instance of the serializer, configure it and you're done! You may re-use the JsonSerializer
instances (i.e. configurations). Let's see more about how serialization works.JsonSerializer
and pass an object to serialize:JsonSerializer
process the target object according to its type. It recognizes arrays, lists, maps, collections, strings, numbers etc. and returns them in a correct JSON format.JsonSerializer
will scan all its properties (defined by getters) and create JSON map from it. Each property of a bean will be also serialized according to its type and so on. Of course, JsonSerializer
detects circular dependencies (by checking an object's identity).JsonSerializer
:JsonSerializer
browse the 'object graph' of given object. Current serialization position is determined by the path: a string that consist of dot-separated property names up to the current position.user.work.prefix
. This means that we can get its value in Java by calling: getUser().getWork().getPrefix()
on target object.deep
flag to true
:JsonSerializer
does not outputs object's class name. However, sometimes we want to preserve the type of serialized object (especially if we want to parse the JSON string back into object). To do this, just enable this feature:JsonSerializer
knows to serializes primitives, collections, strings, integers, and various Java types that are commonly used as bean properties. For each type, there is a TypeJsonSerializer
instance that defines how type is serialized to a JSON.