Json-lib & Hibernate: tips and hints - No Fluff Just Stuff

Json-lib & Hibernate: tips and hints

Posted by: Andres Almiray on May 1, 2008

One of the driving goals of Json-lib since the very beginning has been simplifying Java-to-JSON conversions, no matter how complex the data may be. Many users have reported that they are developing applications that expose hibernated domains, but due to Hibernate using CGLib to do its magic there were some unexpected gotchas, which prompted some tweaks into Json-lib. The following is a list of tips and hints that may come in handy when using both libraries.

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
  1. Have your domain class implement JSONString
  2. Register a set of properties to be excluded with JsonConfig
  3. Use a PropertyFilter
  4. 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.

Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »