ko5tik edited this page Sep 14, 2010 · 5 revisions
Clone this wiki locally

JSON Serializer for android

Project goal

Android applications need to marshall (and umarshall) POJOs to various destinations,
but unfortunately adding a new dependency to project is quite uncool – apps have to be lightweight.
JSON serializer helps to achieve this goal with just 2 small classes without additional dependencies.

30 second introduction

Marshal your POJOs to JSON strings:

String result = JSONMarshaller.marshall(myCoolPojoBean).toString();

And unmarshall them with creating new object:

MyPOJO pojo = JSONUnmarshaller.unmarshall(jsonSorceString,MyPOJO.class);

Now you can forget about SQLLite databases and storing your stuff in app preferences
field by field

Marshalling conventions

At the moment there are no annotations or other means of configurations. All marshalling is done
via conventions:

  • process only public getters
    • starting with “get”
    • no params
    • returnung primitives or beans with public default constructor
  • JSON field names are created by stripping “get” prefix
  • marshall primitives and strings directly to JSON object ( see docs for org.json.JSONObject.put(...) )
  • marshall POJOs as nested JSON objects (if they got public default constructor)

Not implemented yet:

  • marshalling arrays

no plans to implement (at least for now, but ideas are welcome )

  • marshalling collections (it’s impossible to say, what objects reside inside)

Unmarshalling conventions

as with marshalling, we have no metadata and rely on conventions.

  • class of root bean shall be specified explicitely (usual restrictions apply – public default constructor)
  • setter name is created by prepending “set” to JSON field name (no capitalisation)
  • json values are used directly if suitable setter is found (single param of mathcing type, JSON provides numerics booleans and strings)
  • attempt is made to construct param type with JSON value (using single value constructor)

not implemented yet

  • unmarshalling JSON arrays (ideas welcome, whether to extend this to collections)
  • unmarshalling nested objects (work in progress)