ruby-protobuf / protobuf

A pure ruby implementation of Google's Protocol Buffers
https://github.com/ruby-protobuf
MIT License
462 stars 101 forks source link

Add Message.from_json #411

Open aslakhellesoy opened 4 years ago

aslakhellesoy commented 4 years ago

This PR adds a new Message.from_json class method that turns a JSON string into a message object.

It addresses three shortcomings:

aslakhellesoy commented 4 years ago

Please hold off on merging this while I fix the failing tests. I'm also adding a bugfixes for enum fields and JSON.

aslakhellesoy commented 4 years ago

With this change, bytes values have to be encoded as Encoding::ASCII_8BIT, which in Ruby means "binary" encoding. Any other encoding will cause the value to be Base64 decoded.

This is because the JSON representation of bytes is always Base64. When deserialising JSON we don't have the protobuf field info, so the decoding is (unfortunately) happening inside Protobuf::Field::BytesField, based on whether the value is a "binary" string or a "base64" string.

jackorp commented 3 years ago

Ping... Hi, what's the status on this? I am packaging this library for Fedora as a part of an effort to update cucumber and it would be nice if this PR and PR#415 were merged.