feat: Add spec-compliant protojson extension#2297
Conversation
0a4e11f to
8e945cd
Compare
|
That's great! One thing I noticed in my tests that if an protobuf object is created with message Test {
int32 int_value = 1;
}the code const Test = root.lookup('Test');
console.log(protojson.toJson(Test, Test.create({intValue: 0})));will emit Otherwise, passes my tests :) |
| WKT_FROM[".google.protobuf.Timestamp"] = function (type, value) { | ||
| if (typeof value !== "string") | ||
| throw invalid(type.fullName, value, "expected timestamp string"); | ||
| var match = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(?:\.(\d{1,9}))?(Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/.exec(value); |
There was a problem hiding this comment.
Not sure what the :[0-5]\d means at the end of the regex; I think it blocks valid timestamps like '2026-06-03T09:19:00.000-0700' from being parsed properly (returning "invalid timestamp").
There was a problem hiding this comment.
Here :[0-5]\d is the minutes portion of a timezone offset, like in -07:59, whereas -0700 is not valid RFC 3339 iiuc, and hence not ProtoJSON that protoc accepts. Is asserted by the conformance harness in TimestampWithMissingColonInOffset.
Adds a spec-compliant ProtoJSON extension derived from proto3-json-serializer. Cc @alexander-fenster as the original author.
ProtoJSON is exposed via
ext/protojsonwith explicitfromJson,fromJsonString,toJson, andtoJsonStringAPIs, plus an optionalinstall()helper for reflectedTypeconvenience methods.Example usage:
Also updates conformance reporting and README documentation to show binary, ProtoJSON, and Text Format coverage.
A future major release may move some of this functionality into the main library, but keeping it as an extension is the cleanest way to support ProtoJSON for now.
Fixes #677
Fixes #839
Fixes #916
Fixes #1108
Fixes #1304
Fixes #1312
Fixes #1922
Supersedes #1265
Also covers the reflection/runtime ProtoJSON parts of #2201. Static-module integration may become relevant again if we decide to move ProtoJSON support closer to the main library.