Design proposal for improving JSON serialization/deserialization performance

This document describes some of the current thoughts for improving JSON serialization and deserialization in Project Zero. The primary objective of this effort is to improve performance by eliminating the intermediary objects of type JSONObject and JSONArray when converting JSON data to/from Java objects. This should also have the side effect of simplifying the JSON API in Project Zero. The complete list of changes include

  • Remove the JSONObject class. Any reference to JSONObject can now be replaced by a java.util.Map.
  • Remove the JSONArray class. Any reference to JSONArray can now be replaced by a java.util.List.
  • Replace the zero.json.JsonType class with zero.json.Json that has static utility methods for encoding (encode()) Objects as JSON encoded strings and for decoding (decode) JSON encoded strings as Java objects.
  • Encoding objects
    • To encode an object as a JSON string use - String jsonStr = Json.encode(obj);
    • Allow for a formatted (embeds and spaces) JSON string - String formattedJsonStr = Json.encode(obj, true);
    • Allow encoding an object directly to an OutputStream or Writer.
    • Encode Maps as JSON objects ("{ ... }").
    • Encode Lists or Arrays as JSON arrays ("[ ... ]").
    • Encode other complex objects as JSON objects using the object's public fields, preferring getters when available.
    • Eliminate circular references to objects using the $jref notatation.
  • Decoding JSON strings
    • To decode a JSON string use - Object obj = Json.decode(jsonStr);
    • Allow decoding directly from an InputStream or Reader
    • Simple types are converted automatically to the equivalent Java types (String, Integer ...)
    • A JSON Object ("{...}") is decoded to a Map. Permit preserving the order of elements in the object using a flag. For instance if an object {"foo":"bar", "baz":"bah"} is decoded and subsequently encoded, there is no guarantee that the order will be preserved, so you might get {'baz":"bah", "foo":"bar"}. While the JSON spec does not speak to preserving this order, it is not compatible with certain implementations (PHP.net json_encode()). For now, you can use an optional argument in Json.decode() to create an object that preserved the order of elements received from the wire. A better approach might be to allow a callback interface that allows the caller to populate a JSON object or array directly into a Java object of the caller's choice.
    • A JSON Array ("[...]") is decoded to a List.
  • Provide for custom serialization/encoding by allowing implementations of an (zero.json.converter.)Encoder interface. Encoders are required to participate in the encoding process to eliminate circular references.
  • The Converter interface extends the Encoder interface. Registration of Converters in the Global Context has not changed. The fromObject method is no longer applicable and has been removed.

-- madhu - 21 Jan 2008

r2 - 25 Jan 2008 - 21:00:16 - madhu
Syndicate this site RSS ATOM
Copyright 2007 © IBM Corporation | Privacy | Terms of Use | About this site