-
Notifications
You must be signed in to change notification settings - Fork 197
[WIP] Avoid intermediate values when serializing and deserializing proto3 JSON #670
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
base: master
Are you sure you want to change the base?
Changes from all commits
84c6bc8
662efa9
81ddd0c
3594c5d
a64b2f3
a549b57
b8ef37d
fa4d2ea
f155f11
24bffd5
7c92089
6fa98c8
38079b0
a737185
9b62b5d
d44a919
78d495e
c8895c7
e6bee93
add7cce
513d01e
951a2c9
00003ce
48804f1
a5a3f98
dd8cb85
4d43c95
6fe0731
333f16c
a890105
eaa0d35
14bffa2
0e28eb3
02a9960
7899d1f
ee3cf27
617c30c
712cae7
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 |
|---|---|---|
|
|
@@ -208,24 +208,92 @@ abstract class GeneratedMessage { | |
| /// For the proto3 JSON format use: [toProto3Json]. | ||
| String writeToJson() => jsonEncode(writeToJsonMap()); | ||
|
|
||
| /// Returns an Object representing Proto3 JSON serialization of `this`. | ||
| /// Returns Dart JSON object encoding this message following proto3 JSON | ||
| /// format. | ||
| /// | ||
| /// The returned object will have the same format as objects returned by | ||
| /// [jsonEncode]. | ||
| /// | ||
| /// See [toProto3JsonSink] for details. | ||
| Object? toProto3Json( | ||
| {TypeRegistry typeRegistry = const TypeRegistry.empty()}) { | ||
| Object? object; | ||
| final objectSink = jsonObjectWriter((newObject) { | ||
| object = newObject; | ||
| }); | ||
| _writeToProto3JsonSink(_fieldSet, typeRegistry, objectSink); | ||
| return object; | ||
| } | ||
|
|
||
| /// Returns a proto3 JSON string encoding this message. | ||
| /// | ||
| /// See [toProto3JsonSink] for details. | ||
| String toProto3JsonString( | ||
|
Collaborator
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. Should this really be called
Member
Author
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. Additions in this PR will be inconsistent unless we want to do breaking changes. Currently in master branch (and also in the latest release) we have these generators:
So the proto3 method is already inconsistent. Without changing it, we can name the new methods Should we rename
Collaborator
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. Argh - I see - I introduced that inconsistency - not sure what is the better path forwards. If we prefer to avoid churn over consistency we keep it as I guess it's up to your judgement. |
||
| {TypeRegistry typeRegistry = const TypeRegistry.empty()}) { | ||
| final buf = StringBuffer(); | ||
| final stringSink = jsonStringWriter(buf); | ||
| toProto3JsonSink(typeRegistry, stringSink); | ||
| return buf.toString(); | ||
| } | ||
|
|
||
| /// Writes proto3 JSON serialization of this message to the given [JsonSink]. | ||
| /// | ||
| /// The key for each field is be the camel-cased name of the field. | ||
| /// | ||
| /// Well-known types and their special JSON encoding are supported. | ||
| /// If a well-known type cannot be encoded (eg. a `google.protobuf.Timestamp` | ||
| /// with negative `nanoseconds`) an error is thrown. | ||
| /// Well-known types and their special JSON encoding are supported. If a | ||
| /// well-known type cannot be encoded (eg. a `google.protobuf.Timestamp` with | ||
| /// negative `nanoseconds`) an error is thrown. | ||
| /// | ||
| /// Extensions and unknown fields are not encoded. | ||
| /// | ||
| /// The [typeRegistry] is be used for encoding `Any` messages. If an `Any` | ||
| /// message encoding a type not in [typeRegistry] is encountered, an | ||
| /// error is thrown. | ||
| Object? toProto3Json( | ||
| {TypeRegistry typeRegistry = const TypeRegistry.empty()}) => | ||
| _writeToProto3Json(_fieldSet, typeRegistry); | ||
| /// message encoding a type not in [typeRegistry] is encountered, an error is | ||
| /// thrown. | ||
| /// | ||
| /// The [newMessage] argument is for use in generated code, do not use. | ||
| void toProto3JsonSink(TypeRegistry typeRegistry, JsonSink jsonSink, | ||
| {bool newMessage = true}) { | ||
| _writeToProto3JsonSink(_fieldSet, typeRegistry, jsonSink, | ||
| newMessage: newMessage); | ||
| } | ||
|
|
||
| /// Merges field values from Dart representation of JSON following proto3 | ||
| /// JSON format. | ||
| /// | ||
| /// [jsonObject] should be generated by [jsonDecode] or have the same format. | ||
| /// | ||
| /// See [mergeFromProto3JsonReader] documentation for details. | ||
| void mergeFromProto3Json(Object? jsonObject, | ||
| {TypeRegistry typeRegistry = const TypeRegistry.empty(), | ||
| bool ignoreUnknownFields = false, | ||
| bool supportNamesWithUnderscores = true, | ||
| bool permissiveEnums = false}) => | ||
| mergeFromProto3JsonReader( | ||
| JsonReader.fromObject(jsonObject), | ||
| typeRegistry: typeRegistry, | ||
| ignoreUnknownFields: ignoreUnknownFields, | ||
| supportNamesWithUnderscores: supportNamesWithUnderscores, | ||
| permissiveEnums: permissiveEnums, | ||
| ); | ||
|
|
||
| /// Marges field values from a [String] following proto3 JSON format. | ||
| /// | ||
| /// See [mergeFromProto3JsonReader] documentation for details. | ||
| void mergeFromProto3JsonString(String jsonString, | ||
| {TypeRegistry typeRegistry = const TypeRegistry.empty(), | ||
| bool ignoreUnknownFields = false, | ||
| bool supportNamesWithUnderscores = true, | ||
| bool permissiveEnums = false}) => | ||
| mergeFromProto3JsonReader( | ||
| JsonReader.fromString(jsonString), | ||
| typeRegistry: typeRegistry, | ||
| ignoreUnknownFields: ignoreUnknownFields, | ||
| supportNamesWithUnderscores: supportNamesWithUnderscores, | ||
| permissiveEnums: permissiveEnums, | ||
| ); | ||
|
|
||
| /// Merges field values from [json], a JSON object using proto3 encoding. | ||
| /// Merges field values from a [JsonReader] that reads from a JSON in proto3 | ||
| /// JSON format. | ||
| /// | ||
| /// Well-known types and their special JSON encodings are supported. | ||
| /// | ||
|
|
@@ -252,13 +320,13 @@ abstract class GeneratedMessage { | |
| /// | ||
| /// Throws [FormatException] if the JSON not formatted correctly (a String | ||
| /// where a number was expected etc.). | ||
| void mergeFromProto3Json(Object? json, | ||
| void mergeFromProto3JsonReader(JsonReader jsonReader, | ||
| {TypeRegistry typeRegistry = const TypeRegistry.empty(), | ||
| bool ignoreUnknownFields = false, | ||
| bool supportNamesWithUnderscores = true, | ||
| bool permissiveEnums = false}) => | ||
| _mergeFromProto3Json(json, _fieldSet, typeRegistry, ignoreUnknownFields, | ||
| supportNamesWithUnderscores, permissiveEnums); | ||
| _mergeFromProto3JsonReader(jsonReader, _fieldSet, typeRegistry, | ||
| ignoreUnknownFields, supportNamesWithUnderscores, permissiveEnums); | ||
|
|
||
| /// Merges field values from [data], a JSON object, encoded as described by | ||
| /// [GeneratedMessage.writeToJson]. | ||
|
|
||
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.
Consider passing an "indent" value