Protostuff writing list as JSON

973 Views Asked by At

What is easiest way to have protostuff behave like standard Jackson serializer?

I wanted to be able to serialize object graphs, lists or arrays as root objects but seems there is not even a workaround for this?

Here — o is Object that can be of String, SomeType, List[T] etc...

JsonIOUtil.writeTo(stream,
                   o,
                   RuntimeSchema.getSchema((Class<Object>) o.getClass()),
                   false,
                   LinkedBuffer.allocate());
1

There are 1 best solutions below

1
Kostiantyn On

JSON is not the main serialization type, supported by protostuff. It was originally created to support protobuf, with some extensions (object graphs). JSON serialization was added later, as a "supported" serialization format. That's why there are few limitations, that do not exist in generic JSON support libraries like Jackson JSON or GSON.

Protostuff can serialize/deserialize "a message", which is an abstraction of a structure with a set of key-value pairs - fields. Field can be primitive (integer, string, etc), other message or an array. But there is no way to serialize array directly - you always need "a message".

You can define a wrapper class like this:

class Event {
    public Object data;
}

With this wrapper class, you can set "data" to any arbitrary type, including List/array.

UPDATE 2016-10-04:

JSON serialization format in protostuff does not support circular references. For serializing object graphs you have to use GraphIOUtil, which uses its own binary format.