diff options
author | Jon Skeet <jonskeet@google.com> | 2017-10-24 11:44:44 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2017-10-28 07:47:43 +0100 |
commit | a9854512532a42c174e1aa496d4bfe05b04645d0 (patch) | |
tree | 43523c645c4256fb37a5d86f4e41aa2672ac0ee9 /csharp/src/Google.Protobuf.Test/JsonParserTest.cs | |
parent | 4526d8baa0fe1c866adab8066e59d15996c25d02 (diff) | |
download | protobuf-a9854512532a42c174e1aa496d4bfe05b04645d0.tar.gz protobuf-a9854512532a42c174e1aa496d4bfe05b04645d0.tar.bz2 protobuf-a9854512532a42c174e1aa496d4bfe05b04645d0.zip |
Add JsonParser setting to ignore unknown field values
Note that the default behavior is still to throw an exception; you
need to opt into ignoring unknown fields.
Fixes #2838.
Diffstat (limited to 'csharp/src/Google.Protobuf.Test/JsonParserTest.cs')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index f595455a..2c515433 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -926,6 +926,27 @@ namespace Google.Protobuf Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json)); } + [Test] + public void UnknownField_NotIgnored() + { + string json = "{ \"unknownField\": 10, \"singleString\": \"x\" }"; + Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json)); + } + + [Test] + [TestCase("5")] + [TestCase("\"text\"")] + [TestCase("[0, 1, 2]")] + [TestCase("{ \"a\": { \"b\": 10 } }")] + public void UnknownField_Ignored(string value) + { + var parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true)); + string json = "{ \"unknownField\": " + value + ", \"singleString\": \"x\" }"; + var actual = parser.Parse<TestAllTypes>(json); + var expected = new TestAllTypes { SingleString = "x" }; + Assert.AreEqual(expected, actual); + } + /// <summary> /// Various tests use strings which have quotes round them for parsing or as the result /// of formatting, but without those quotes being specified in the tests (for the sake of readability). |