A friendly way to deal with JSON in Java
A friendly way to deal with JSON in Java

Just pushed a improved libFriendlyJSON. As far as I can tell it's pretty flexible and robust.

An example of usage:

    1: class SmallClass extends JSONEntity {
    2:         String name;
    3: 
    4:         /* You must always have this constructor in JSONEntities. */
    5:         public SmallClass (JSONObject json) {
    6:                 super (json);
    7:         }
    8: }
    9: 
   10: class Entity extends JSONEntity {
   11:         boolean aBoolean;
   12:         byte aByte;
   13:         char aChar;
   14:         short aShort;
   15:         int aInt;
   16:         long aLong;
   17:         float aFloat;
   18:         double aDouble;
   19: 
   20:         String aString;
   21:         Map<String, String> aMap;
   22:         Map<String, List<String>> aMapOfLists;
   23: 
   24:         SmallClass[] arrayOfSmallClass;
   25: 
   26:         transient int transientValue;
   27: 
   28:         /* You must always have this constructor in JSONEntities. */
   29:         public Entity (JSONObject json) throws JSONMappingException {
   30:                 super (json);
   31:                 init ();
   32:         }
   33: 
   34:         private init () {
   35:                 this.transientValue = this.aInt;
   36:                 if (this.aMap == null)
   37:                         this.aMap = new HashMap<String, String> ();
   38:                 if (this.aMapOfLists == null)
   39:                         this.aMapOfLists = new HashMap<String, List<String>> ();
   40:         }
   41: }

And then, somewhere on the code:

    1: Entity entity = new Entity ();
    2: /* ...
    3:  * set the content of entity
    4:  * ... */
    5: JSONObject jsonObj = entity.toJson ();
    6: String jsonString = entity.toString (4);
    7: String jsonObjString = jsonObj.toString (4);
    8: /* jsonString and jsonObjString will be equals. */
    9: 
   10: Entity fromJson = new Entity (jsonObj);
   11: Entity fromString = new Entity (jsonString);
   12: /* entity equals fromJson equals froString */

It supports things like fields declared with inner classes (static or not) as type, enumerations, arrays, lists, etc. It feels usable to me.

A particularity of Java makes it impossible to use initialization of fields when declaring them, as the fields are set by the super class and (when super (json) is called) and then the class initialization takes places, overwriting any value already set. The possible solution is to use a init method that set the fields (or we could do the initialization at the constructor but I guess it's more usual to other constructors so sharing the initialization code might be a good idea.

Inside this very small library is a great deal of reflection usage. In old JVM that would take a big toll on performance, nowadays it seams to be not so bad.

Feel free to have a look around, use the library and specially do share any idea of improvement or bug fix with me on github.