-
Notifications
You must be signed in to change notification settings - Fork 435
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
Update JSON unknown field support #771
Changes from 1 commit
dd141c5
03eb4de
f24881e
877cb28
b0b4a38
9c42e1f
5781bb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,8 @@ public enum JSONDecodingError: Error { | |
case conflictingOneOf | ||
/// Reached the nesting limit for messages within messages while decoding. | ||
case messageDepthLimit | ||
/// Encountered a unknown field with the given name. When parsing JSON, you | ||
/// can instead instructe the library to ignore this via | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/instructe/instruct/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
/// JSONDecodingOptions.ignoreUnknownFields. | ||
case unknownField(String) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,9 @@ public struct JSONDecodingOptions { | |
/// while parsing. | ||
public var messageDepthLimit: Int = 100 | ||
|
||
/// If unknown fields in the JSON should be ignored. If they aren't | ||
/// ignored, and error will be raised if one is encountered. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/and error/an error/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
public var ignoreUnknownFields: Bool = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just out of curiosity, why is the default value for this option false? I ask because it seems more natural to set this to true, to me, as someone who uses protobuf to help manage version compatibility across various components. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also had the pointer in the second commit on the PR, but it is in the spec – Spec: https://developers.google.com/protocol-buffers/docs/proto3#json_options
With the binary format, the unknown fields can be parsed and preserved to be reflected back out. But for the JSON format, there is no way to know if something was an int32, int64, uint32, etc., so while it could be reflected back out in JSON, you can't convert it or even expose it via a reflection (if you language has proto reflection). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, since the "ignore" is destructive, it really shouldn't be the default since it means by default we lose things we can't deal with. If we tried to capture the text and reflect it out for JSON, maybe it would make sense, but the fact that a binary conversion would loose things is also bad. There is a similar issue with enums, if you get a value string you don't know, there is no way to keep it. They also offer up that folks could provide an encoding option to use number, but we haven't added that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, that makes a lot of sense. I am not accustomed to using JSON with proto aside from debugging. |
||
|
||
public init() {} | ||
} |
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.
s/Encountered a/Encountered an/
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.
done