Json-lib: more custom features - No Fluff Just Stuff

Json-lib: more custom features

Posted by: Andres Almiray on October 19, 2007

Its just been a couple of days after Json-lib 2.1 was released and feature requests are already pouring in. The first one ask for custom handling of default values when a null reference is encountered. Per the JSON spec if a value is null Json-lib applies the following rules
  • 0 - if it is a Number or primitive number
  • '' - if it is a String/Character or char
  • false - if it is a Boolean
  • null - all other cases
But what if you would like to return 'null' if an Integer reference is null? (yes I know that would against the spec), or if a MyCoolBean reference is null you'd like to return '{ empty: true }' instead of simply 'null'? that's the reason why DefaultValueProcessor is now available, along with DefaultValueProcessorMatcher (for the same reasons shown here).

The second feature caught me by surprise as I didn't understand the usefulness of it at first sight, the thing is that a user has a bean which uses a composed Map for its property representation, but that bean does not implements Map nor it is a DynaBean, so he would like to call internalMap.put() instead of going to trough the usual PropertyUtils.setProperty(), which depending on the client code can be sometimes a performance bottleneck. After a quick message exchange and a prompt patch PropertySetStrategy is ready.

These features have a default implementation already plugged into JsonConfig, which is the place where you would also plug-in your custom implementations.

The third one is related to JSON <-> serialization. As you may probably know, XMLSerializer will output type hints (unless configured otherwise) to an XML string, using those hints again when transforming back to JSON. The hints use reserved keywords as attributes [type,class,params,null] but it may be the case for some users that some of those keywords must have business meaning instead of just 'framework' meaning, how do we cope with that? by adding a 'json_' prefix to the reserved keywords. In order to maintain backward compatibility there is a flag on XMLSerializer (typeHintsCompatibility) that when set to false will enable the prefix, when true it will disregard it.

The code is already on the cvs along with a couple of quick bug fixes. Keep posted for more Json-lib updates.
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 »