aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf/JsonTokenizer.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2017-10-24 11:44:44 +0100
committerJon Skeet <skeet@pobox.com>2017-10-28 07:47:43 +0100
commita9854512532a42c174e1aa496d4bfe05b04645d0 (patch)
tree43523c645c4256fb37a5d86f4e41aa2672ac0ee9 /csharp/src/Google.Protobuf/JsonTokenizer.cs
parent4526d8baa0fe1c866adab8066e59d15996c25d02 (diff)
downloadprotobuf-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/JsonTokenizer.cs')
-rw-r--r--csharp/src/Google.Protobuf/JsonTokenizer.cs28
1 files changed, 28 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/JsonTokenizer.cs b/csharp/src/Google.Protobuf/JsonTokenizer.cs
index 09a6d43b..2fc1e7c3 100644
--- a/csharp/src/Google.Protobuf/JsonTokenizer.cs
+++ b/csharp/src/Google.Protobuf/JsonTokenizer.cs
@@ -138,6 +138,34 @@ namespace Google.Protobuf
protected abstract JsonToken NextImpl();
/// <summary>
+ /// Skips the value we're about to read. This must only be called immediately after reading a property name.
+ /// If the value is an object or an array, the complete object/array is skipped.
+ /// </summary>
+ internal void SkipValue()
+ {
+ // We'll assume that Next() makes sure that the end objects and end arrays are all valid.
+ // All we care about is the total nesting depth we need to close.
+ int depth = 0;
+
+ // do/while rather than while loop so that we read at least one token.
+ do
+ {
+ var token = Next();
+ switch (token.Type)
+ {
+ case JsonToken.TokenType.EndArray:
+ case JsonToken.TokenType.EndObject:
+ depth--;
+ break;
+ case JsonToken.TokenType.StartArray:
+ case JsonToken.TokenType.StartObject:
+ depth++;
+ break;
+ }
+ } while (depth != 0);
+ }
+
+ /// <summary>
/// Tokenizer which first exhausts a list of tokens, then consults another tokenizer.
/// </summary>
private class JsonReplayTokenizer : JsonTokenizer