Skip to content

JrJackson does not support nested non JSON datatypes (apart from Symbol)

Guy Boertje edited this page Oct 10, 2013 · 3 revisions

When a non JSON datatype is passed into the dump method as the root object, a special case is made, the to_json method is tried.

However if a JSON datatype is passed into the dump method then JrJackson expects all elements in the object graph to be JSON datatypes, there is one exception to this - Ruby Symbol, because there is a standard Jackson ToString serializer class that is easy to register for the org.jruby.RubySymbol java class.

I don't think JrJackson will include the handling of non JSON datatyped objects in Hash keys or values or Array values anytime soon.

Calling to_json from the ruby side or the java side hands over control to the object, which will in all likelyhood invoke a new serializer session to serialize a Hash (which may include more non JSON datatype objects creating more serializer sessions). What Ruby really needs is a universal to_json_data method so the same serializer session can be used for the whole object graph.

On the Ruby side

  1. There many of builtin and std lib Classes that do not have a to_json method
  2. Some Ruby classes are not serializable, e.g. Thread, Proc
  3. There is no universal Class level creational method (e.g. from_json) that will build an instance from a json string
  4. For example, the Oj library has symmetrical load(dump(obj)) guarantees, and it does this in a proprietary way.
  5. Walking through the object graph to call the to_json method on the ruby side will wipe out any performance gains making JrJackson no more useful than the Json gem.

On the Java side

  1. Jackson has many mechanisms for a class author or Jackson library user to specify how Jackson finds the appropriate serializer to use for a given java instance.
  2. Jackson finds serializers for the RubyArray and RubyHash because they implement a standard interface, e.g. List or Map.
  3. Honestly, I don't know how Jackson finds serializers for the other JSON datatypes, e.g. RubyFloat, RubyInteger, RubyString, RubyBoolean
  4. To support the to_json method, defining a ToJson serializer is not hard, but what is hard is to find a way to register this serializer so that Jackson would use this serializer when handling some arbitary Ruby object that happens to have a to_json method defined.
  5. Even in JRuby, there is no common parameterless method to call that will return a JSON compatible java object.
  6. Performance would be severely affected by continually moving across the Java Ruby boundary.
  7. On deserialization there is no way to know what class upon which to invoke what constructor that accepts the JSON string.

And all of the above is just applicable to Hash or Array values. There is a new level of complexity when a serializing a Hash with non String keys!

JSON is a data representation and not an Object serialization/deserialization mechanism. In Ruby we have YAML and Marshal for that.

Clone this wiki locally