JsonSerializerserializes objects into JSON strings. Create an instance of the serializer, configure it and you're done! You may re-use the
JsonSerializerinstances (i.e. configurations). Let's see more about how serialization works.
JsonSerializerand pass an object to serialize:
JsonSerializerprocess the target object according to its type. It recognizes arrays, lists, maps, collections, strings, numbers etc. and returns them in a correct JSON format.
JsonSerializerwill 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,
JsonSerializerdetects circular dependencies (by checking an object's identity).
JsonSerializerbrowse 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.
JsonSerializerdoes 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:
JsonSerializerknows to serializes primitives, collections, strings, integers, and various Java types that are commonly used as bean properties. For each type, there is a
TypeJsonSerializerinstance that defines how type is serialized to a JSON.