Skip to content
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

[WIP] Avoid intermediate values when serializing and deserializing proto3 JSON #670

Draft
wants to merge 38 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
84c6bc8
Avoid intermediate values when serializing proto3 JSON
osa1 Jun 2, 2022
662efa9
Fix formatting
osa1 Jun 2, 2022
81ddd0c
Fix reserved names test
osa1 Jun 2, 2022
3594c5d
Avoid intermediate JSON objects when merging proto3 JSON messages
osa1 Jun 2, 2022
a64b2f3
Update reserved names
osa1 Jun 3, 2022
a549b57
Update benchmarks to use new functions
osa1 Jun 7, 2022
b8ef37d
Implement well-known type conversions
osa1 Jun 7, 2022
fa4d2ea
Generalize JSON generator and parsers to generate and parse both stri…
osa1 Jun 7, 2022
f155f11
Move writer and reader code to separate files
osa1 Jun 7, 2022
24bffd5
Remove accidentally committed file
osa1 Jun 7, 2022
7c92089
Refactoring
osa1 Jun 7, 2022
6fa98c8
Stubs for well-known types
osa1 Jun 7, 2022
38079b0
Refactor function types, fix a jsontool use error
osa1 Jun 7, 2022
a737185
Rename jsonWriter -> jsonSink
osa1 Jun 7, 2022
9b62b5d
Start updating well-known types
osa1 Jun 8, 2022
d44a919
Update rest of the well-known type ser/deser code
osa1 Jun 8, 2022
78d495e
Update plugin for new BuilderInfo arg names
osa1 Jun 8, 2022
c8895c7
Update reserved names
osa1 Jun 8, 2022
e6bee93
Merge remote-tracking branch 'origin/master' into jsontool
osa1 Jun 8, 2022
add7cce
Fix control flow in a few places
osa1 Jun 8, 2022
513d01e
Fix a few bugs
osa1 Jun 9, 2022
951a2c9
Fix a bug
osa1 Jun 9, 2022
00003ce
Update two tests
osa1 Jun 9, 2022
48804f1
Fix a few tests
osa1 Jun 9, 2022
a5a3f98
More bugs
osa1 Jun 9, 2022
dd8cb85
Fix int bounds checks
osa1 Jun 9, 2022
4d43c95
A few fixups
osa1 Jun 9, 2022
6fe0731
Fix bugs, update tests
osa1 Jun 9, 2022
333f16c
Bump jsontool
osa1 Jun 9, 2022
a890105
Remove unused def
osa1 Jun 9, 2022
eaa0d35
Fix error messages
osa1 Jun 9, 2022
14bffa2
Disable a test for now
osa1 Jun 9, 2022
0e28eb3
Fix TODOs
osa1 Jun 9, 2022
02a9960
Handle unexpected input in parseFailure matcher
osa1 Jun 9, 2022
7899d1f
Renaming, docs
osa1 Jun 9, 2022
ee3cf27
More docs
osa1 Jun 9, 2022
617c30c
Bump jsontool
osa1 Jun 16, 2022
712cae7
Merge remote-tracking branch 'origin/master' into jsontool
osa1 Jun 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix a few tests
  • Loading branch information
osa1 committed Jun 9, 2022
commit 48804f1be2605e45683dc036b7413fabbaf193ce
34 changes: 23 additions & 11 deletions protobuf/lib/src/protobuf/proto3_json_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void _mergeFromProto3JsonReader(JsonReader jsonReader, _FieldSet fieldSet,

if (!jsonReader.tryObject()) {
// TODO: Check error message
throw context.parseException('Expected JSON object', jsonString);
throw context.parseException('Expected JSON object', 1);
}

final Map<String, FieldInfo> fieldsByName = meta.byName;
Expand All @@ -69,7 +69,10 @@ void _mergeFromProto3JsonReader(JsonReader jsonReader, _FieldSet fieldSet,

if (fieldInfo == null) {
if (context.ignoreUnknownFields) {
return;
jsonReader.skipAnyValue();
context.popIndex();
key = jsonReader.nextKey();
continue;
} else {
throw context.parseException('Unknown field name \'$key\'', key);
}
Expand All @@ -82,21 +85,17 @@ void _mergeFromProto3JsonReader(JsonReader jsonReader, _FieldSet fieldSet,
// TODO: We don't have the JSON string to show in error messages
throw context.parseException('Expected a map', 1);
}
String? key = jsonReader.nextKey();
while (key != null) {
String? keyStr = jsonReader.tryString();
if (keyStr == null) {
throw context.parseException('Expected a String key', keyStr);
}
context.addMapIndex(keyStr);
String? mapKeyStr = jsonReader.nextKey();
while (mapKeyStr != null) {
context.addMapIndex(mapKeyStr);
Object mapKey =
_decodeMapKey(keyStr, mapFieldInfo.keyFieldType, context);
_decodeMapKey(mapKeyStr, mapFieldInfo.keyFieldType, context);
Object? value = _parseProto3JsonValue(
jsonReader, mapFieldInfo.valueFieldInfo, typeRegistry, context);
fieldValues[mapKey] = value;
context.popIndex();

key = jsonReader.nextKey();
mapKeyStr = jsonReader.nextKey();
}
} else if (_isRepeated(fieldInfo.type)) {
if (jsonReader.tryNull()) {
Expand All @@ -118,6 +117,7 @@ void _mergeFromProto3JsonReader(JsonReader jsonReader, _FieldSet fieldSet,
}
}
} else if (_isGroupOrMessage(fieldInfo.type)) {
// TODO: Avoid allocating a message here
var parsedSubMessage =
_parseProto3JsonValue(jsonReader, fieldInfo, typeRegistry, context)
as GeneratedMessage;
Expand Down Expand Up @@ -342,6 +342,18 @@ Object _decodeMapKey(String key, int fieldType, JsonParsingContext context) {
}
}

Object? _nextJsonObject(JsonReader reader) {
final jsonReader = JsonReader.fromObject(jsonObject);

Object? json;
final sink = jsonObjectWriter((result) {
json = result;
});
jsonReader.expectAnyValue(sink);

return json;
}

int _tryParse32BitProto3(String s, JsonParsingContext context) {
return int.tryParse(s) ??
(throw context.parseException('expected integer', s));
Expand Down