encoding/jsonpb: add unmarshal option to ignore unknown fields
This feature seems to be used quite a bit, and the conformance tests
treat this as required, perhaps as a "required option" since the
developer guide states:
"Proto3 JSON parser should reject unknown fields by default but may
provide an option to ignore unknown fields in parsing."
Also, all invalid UTF-8 errors in skipped values are also returned as it
is similar to a parse error, except it is a non-fatal one.
Change-Id: Ia26e9a355daecdbf99af23f3061353fffa32d47d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174017
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/cmd/conformance/main.go b/internal/cmd/conformance/main.go
index a779929..9f1abb7 100644
--- a/internal/cmd/conformance/main.go
+++ b/internal/cmd/conformance/main.go
@@ -69,7 +69,9 @@
case *pb.ConformanceRequest_ProtobufPayload:
err = proto.Unmarshal(p.ProtobufPayload, msg)
case *pb.ConformanceRequest_JsonPayload:
- err = jsonpb.Unmarshal(msg, []byte(p.JsonPayload))
+ err = jsonpb.UnmarshalOptions{
+ DiscardUnknown: req.TestCategory == pb.TestCategory_JSON_IGNORE_UNKNOWN_PARSING_TEST,
+ }.Unmarshal(msg, []byte(p.JsonPayload))
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{