aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf.Test
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
-rw-r--r--csharp/src/Google.Protobuf.Test/JsonParserTest.cs21
-rw-r--r--csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs16
2 files changed, 37 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).
diff --git a/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs b/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs
index 527ab336..33d35036 100644
--- a/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs
+++ b/csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs
@@ -349,6 +349,22 @@ namespace Google.Protobuf
Assert.AreEqual(JsonToken.EndDocument, tokenizer.Next());
Assert.Throws<InvalidOperationException>(() => tokenizer.Next());
}
+
+ [Test]
+ [TestCase("{ 'skip': 0, 'next': 1")]
+ [TestCase("{ 'skip': [0, 1, 2], 'next': 1")]
+ [TestCase("{ 'skip': 'x', 'next': 1")]
+ [TestCase("{ 'skip': ['x', 'y'], 'next': 1")]
+ [TestCase("{ 'skip': {'a': 0}, 'next': 1")]
+ [TestCase("{ 'skip': {'a': [0, {'b':[]}]}, 'next': 1")]
+ public void SkipValue(string json)
+ {
+ var tokenizer = JsonTokenizer.FromTextReader(new StringReader(json.Replace('\'', '"')));
+ Assert.AreEqual(JsonToken.StartObject, tokenizer.Next());
+ Assert.AreEqual("skip", tokenizer.Next().StringValue);
+ tokenizer.SkipValue();
+ Assert.AreEqual("next", tokenizer.Next().StringValue);
+ }
/// <summary>
/// Asserts that the specified JSON is tokenized into the given sequence of tokens.