-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Message.from_json #411
base: 3-10-stable
Are you sure you want to change the base?
Add Message.from_json #411
Conversation
Please hold off on merging this while I fix the failing tests. I'm also adding a bugfixes for enum fields and JSON. |
With this change, This is because the JSON representation of |
when Array | ||
ob.map { |value| normalize_json(value) } | ||
when Hash | ||
Hash[*ob.flat_map { |key, value| [key.underscore, normalize_json(value)] }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The underscore thing seems like we'd want to make that optional?
@@ -48,7 +48,18 @@ def wire_type | |||
|
|||
def coerce!(value) | |||
case value | |||
when String, Symbol | |||
when String | |||
if value.encoding == Encoding::ASCII_8BIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to make this configurable at the from_json
method? You can specify if your uses Base64 encoded binary blobs? Maybe true by default but allow the caller to turn it off? That way we don't have to guess.
This PR adds a new
Message.from_json
class method that turns a JSON string into a message object.It addresses three shortcomings:
fooBarZap
) in the JSON are converted to underscore (fooBarZap
)bytes
fields are Base64 decoded unless they are already in "binary" format. This is because Protobuf's JSON representation uses Base64 encoding for bytes fields.