The first gotcha is that hibernated objects are really proxies, and as such they expose more properties than the ones you wrote, so let's make sure Json-lib only serializes those properties you are interested in. This task may be accomplished in 4 different ways, depending on your needs
- Have your domain class implement
JSONString
- Register a set of properties to be excluded with JsonConfig
- Use a
PropertyFilter
- Write a custom
JsonBeanProcessor
1. Implementing JSONString
is the most invasive option as it ties your code to Json-lib's APIs (don't worry it won't go away) and gives you full responsibility on how the class should be represented in a valid JSON string. Here is an example of how it may look:
2. The next option allows a higher level of configuration as you can add/update properties to be excluded at will by registering a String array with JsonConfig
Be aware that this option does not discriminate the source of the properties, which means that if you have 2 beans with an address property, that property will be omitted in both cases, this "inconvenience" leads to the netx option.
3. Use a PropertyFilter
, allowing you a finer detail on both the source and properties to be excluded. You must pay attention on how you register a PropertyFilter as the same mechanism is used when transforming from JSON to Java.
4. Lastly you may use a JsonBeanProcessor, which works like JSONString in the sense that it must return a valid JSONObject that represents the whole bean but it is not as invasive.
And it is here with JsonBeanProcessors where we encounter the second gotcha related to CGlib. As you noticed we registered the processor using Person as target class, but Hibernate returns a Proxy that is not exactly a Person but a subclass (if possible) of Person. The default matching algorithm used by Json-lib will try to match the exact class (using equals), meaning that your processor will not be called when you expect it to be,this is why JsonBeanProcessorMatcher was introduced, now you are be able to write the following code
I hope these examples help dispel some doubts, in any case please feel free to drop by Json-lib's forum and mailing lists.