Google Protobuf – 在Typescript中从JSON反序列化

huangapple go评论85阅读模式
英文:

Google Protobuf - Deserialize from JSON in Typescript

问题

我在Google Protobuf中使用TypeScript有一些对象,并将它们序列化为JSON以持久化到AWS S3,因为我需要以这种格式处理它们。但我有一个要求,即从包含由objInstance.toObject()生成的JSON的字符串中反序列化它们。是否有办法使用为此生成的代码实现这一点?所有内部类(jspb.Message)都不包含与反序列化相关的方法,除了deserializeBinary,这不是我所需要的。关于Node/TypeScript中的Google Protobuf的文档目前非常有限,甚至查看了存储库中的测试(https://github.com/protocolbuffers/protobuf/tree/master/js)。以下是生成代码中的函数:

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): SearchResponse.AsObject;
  static toObject(includeInstance: boolean, msg: SearchResponse): SearchResponse.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: SearchResponse, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): SearchResponse;
  static deserializeBinaryFromReader(message: SearchResponse, reader: jspb.BinaryReader): SearchResponse;

我找到了一个与此相关的问题:toObject -> Protobuf

英文:

I have some objects in Google Protobuf using Typecript and I'm serializing then to JSON to persist to AWS s3, as I need to work with then in this format.

But one of the requirements I have is to deserialize (from a string containing the JSON generated by objInstance.toObject()).

Is there any way to make it possible using the code generated for it? All the internal classes (jspb.Message) does not contain any method related to deserialization EXCEPT deserializeBinary, that is not what I need.

The documentation regarding google protobuf in Node/Typescript is very poor right now, even looked at the tests in the repo ( https://github.com/protocolbuffers/protobuf/tree/master/js )

Here's the functions in the generated code:

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): SearchResponse.AsObject;
  static toObject(includeInstance: boolean, msg: SearchResponse): SearchResponse.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo&lt;jspb.Message&gt;};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo&lt;jspb.Message&gt;};
  static serializeBinaryToWriter(message: SearchResponse, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): SearchResponse;
  static deserializeBinaryFromReader(message: SearchResponse, reader: jspb.BinaryReader): SearchResponse;

I found one issue referencing this: toObject -> Protobuf

答案1

得分: 3

很不幸,官方的JavaScript实现不支持JSON。

你可以在符合性测试中看到这一点:https://github.com/protocolbuffers/protobuf/blob/0110ff565951ea52af37d6f155197048c378abe3/conformance/conformance_nodejs.js#L76

这一行跳过了所有关于JSON格式的测试,因为它没有被实现。

但是在TypeScript中有替代的protobuf实现。例如,protobuf-ts(这是一个厚颜无耻的自荐,我是作者)。它支持所有特性的官方proto3 JSON格式。它通过了所有符合性测试。而且它的体积要小得多,与google-protobuf相比,大约只有40kb vs 400kb。

英文:

Unfortunately, the official JavaScript implementation does not support JSON.

You can see it in the conformance tests: https://github.com/protocolbuffers/protobuf/blob/0110ff565951ea52af37d6f155197048c378abe3/conformance/conformance_nodejs.js#L76

This line skips all the tests for the JSON format because it is not implemented.

But there are alternative protobuf implementations in TypeScript. For example, protobuf-ts (this is a shameless plug, I am the author). It supports all features of the canonical proto3 JSON format. It passes all conformance tests. And it has a seriously smaller footprint than google-protobuf ~40kb vs ~400kb.

huangapple
  • 本文由 发表于 2020年1月3日 22:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定