BigVertiser Ads

Wednesday, March 27, 2013

JSONer

JSONer, is a one of my ART(Annotated Reflective Tool) projects. It extends a basic generic infrastructure, to allow JAVA instance custom serialization of any sort.

This project supports the serialization, and deserialization of Java instances to and from JSON text respectively.


  • JSONer is very simple to implement, it requires the jar in your project build path, and to annotate your JSON bean class with a JSON_Class annotation. Furthermore any member which needs to be serialized, or deserialized, MUST be annotated with a JSON_Field annotation.
  • Adding a new parser type is simpler then any other parser... you simply extend the JSON_TypeParser<YourType> specify your type, implement your serialization and deserialization, and specify the parser class type in the annotation of the specific field.
  • Adding support for generic type is also simpler then any other parser any List<?> or Map<?,?> first generation generic types, if to be more specific List<List<Item>> is not supported, while List<AnyItem> is supported, and in order to solve this sort of issue, wrap your List<Item> and turn it into AnyItem. (and don't forget to annotate the class type and member...)
    (I must extend that unlike my ReXML, this implementation only works for the specified type. if inherited type are stored in the list, they would be down parsed(casted) to the parent class type defined in the annotation!!)
  • Don't worry about nested JSON_Class types, if the class type of a members are annotated correctly, the parser would handle it.
  • You may also configure whether a member is optional, and use the same object for deserializing multiple different texts.
The only thing I didn't (want to get into) implement is a recursive handling... it will be a pain in the S!


I've tested it with many configuration on my end, and it did not fail me so far. I did have a failure/bug every now and again, and have fixed the ones I've encountered.

I must warn you, there might still be a few bugs hiding somewhere in a more complex setup.

I give this to you for a couple of reasons:

  • I would like to get an honest opinion of someone else.
  • I would to be informed of any bugs.
  • I would like to know if the documentation is OK for you.
  • I would like to know if the example supplied is good enough.
  • I would like to know what you don't like about it..
  • And anything else you can think about which can bring me a step closer to perfection... :)

WITH OUT ANY WARRANTIES:
YOU MAY USE THE JARS AS IS FOR ANY PURPOSE.

You can get JSONer here... 

UPDATE:

Here is how to read an object from a file as requested:

File file = new File(pathToFile);
FileInputStream fis = null;
YourBeanClass yourBeanInstance = null;
try {
 fis = new FileInputStream(file);
 yourBeanInstance = deserialize(YourBeanClass.class, fis);
} catch (FileNotFoundException e) {
 e.printStackTrace();
 // Your error handling...
} finally { if (fis != null) fis.close(); }

2 comments:

  1. please give a code example how can I parse a JSON file in java.

    ReplyDelete