简体   繁体   English

Protostuff写作列表为JSON

[英]Protostuff writing list as JSON

What is easiest way to have protostuff behave like standard Jackson serializer? 使protostuff表现得像标准Jackson序列化器的最简单方法是什么?

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... 这里- o为对象,可以是字符串,SOMETYPE,列表[T]等...

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

JSON is not the main serialization type, supported by protostuff. JSON不是主要的序列化类型,由protostuff支持。 It was originally created to support protobuf, with some extensions (object graphs). 它最初是为了支持protobuf而创建的,带有一些扩展(对象图)。 JSON serialization was added later, as a "supported" serialization format. 稍后添加了JSON序列化,作为“支持的”序列化格式。 That's why there are few limitations, that do not exist in generic JSON support libraries like Jackson JSON or GSON. 这就是为什么在Jackson JSON或GSON等通用JSON支持库中不存在的限制很少的原因。

Protostuff can serialize/deserialize "a message", which is an abstraction of a structure with a set of key-value pairs - fields. Protostuff可以序列化/反序列化“消息”,它是具有一组键值对的结构的抽象 - 字段。 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. 使用此包装类,您可以将“data”设置为任意类型,包括List / array。

UPDATE 2016-10-04: 更新 2016-10-04:

JSON serialization format in protostuff does not support circular references. protostuff中的JSON序列化格式不支持循环引用。 For serializing object graphs you have to use GraphIOUtil, which uses its own binary format. 对于序列化对象图,您必须使用GraphIOUtil,它使用自己的二进制格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM