1

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());
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Marko Kraljevic
  • 401
  • 4
  • 19

1 Answers1

0

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.

Kostiantyn
  • 935
  • 8
  • 13
  • So even if I use GraphIOUtil I have to have wrapper class and do the packing/unpacking step? Also this is only valid for protostuff-graph format that is native to the lib? I wanted to use protostuff for json & graph serialization since I saw that it has best speed+size ratio on benchmarks (https://github.com/eishay/jvm-serializers/wiki) but still seems that I'll have to use different (slower) libraries for the json part... – Marko Kraljevic Oct 03 '16 at 08:00