During a recent job interview, i was asked the steps taken to convert Java object to JSON using Jackson. Although, I havent done it before, I have seen examples where Jackson was used and by using object mapper the java object is converted to JSON. However, the interviewer asked "how and where is the JSON schema set up" I thought this was done automatically. Does one have to specify how the JSON has to be returned? I have seen examples where the toString is overriden - is that where the schema is specified. Thanks
1 Answer
I think the interviewer doesn't mean to ask you how to use Jackson, but how JSON is constructed from a Java Object.
Here's what I thought:
- Jackson will detect every field's name as
keys; - Iterate the
keysto get their values and put it into the JSON. - If a field has child object, it will do step 1 and 2 recursively.
Note: The Java class should implementation the serialization interface.
2 Comments
Luiggi Mendoza
Not necessarily. Jackson can serialize classes that aren't marked as
Serializable.Diego C Nascimento
As said, theres others ways of doing the serialization. And, the way you said is so simple to the real implementation, that needs to detect the type of the object, how it should be codec/decoded, etc