Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9476e0e

Browse files
committed
Have Env.StrictMode on array with missing comma
1 parent f71d083 commit 9476e0e

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ public bool EatItemSeperatorOrMapEndChar(ReadOnlySpan<char> value, ref int i)
718718

719719
for (; i < value.Length; i++) { var c = value[i]; if (!JsonUtils.IsWhiteSpace(c)) break; } //Whitespace inline
720720
}
721+
else if (Env.StrictMode) throw new Exception(
722+
$"Expected '{JsWriter.ItemSeperator}' or '{JsWriter.MapEndChar}'");
721723

722724
return success;
723725
}

src/ServiceStack.Text/Jsv/JsvTypeSerializer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,12 @@ public bool EatItemSeperatorOrMapEndChar(string value, ref int i)
379379

380380
var success = value[i] == JsWriter.ItemSeperator
381381
|| value[i] == JsWriter.MapEndChar;
382-
i++;
382+
383+
if (success)
384+
i++;
385+
else if (Env.StrictMode) throw new Exception(
386+
$"Expected '{JsWriter.ItemSeperator}' or '{JsWriter.MapEndChar}'");
387+
383388
return success;
384389
}
385390

@@ -391,9 +396,9 @@ public bool EatItemSeperatorOrMapEndChar(ReadOnlySpan<char> value, ref int i)
391396
|| value[i] == JsWriter.MapEndChar;
392397

393398
if (success)
394-
{
395399
i++;
396-
}
400+
else if (Env.StrictMode) throw new Exception(
401+
$"Expected '{JsWriter.ItemSeperator}' or '{JsWriter.MapEndChar}'");
397402

398403
return success;
399404
}
@@ -411,7 +416,7 @@ public ReadOnlySpan<char> EatValue(ReadOnlySpan<char> value, ref int i)
411416
{
412417
var tokenStartPos = i;
413418
var valueLength = value.Length;
414-
if (i == valueLength) return default(ReadOnlySpan<char>);
419+
if (i == valueLength) return default;
415420

416421
var valueChar = value[i];
417422
var withinQuotes = false;
@@ -422,7 +427,7 @@ public ReadOnlySpan<char> EatValue(ReadOnlySpan<char> value, ref int i)
422427
//If we are at the end, return.
423428
case JsWriter.ItemSeperator:
424429
case JsWriter.MapEndChar:
425-
return default(ReadOnlySpan<char>);
430+
return default;
426431

427432
//Is Within Quotes, i.e. "..."
428433
case JsWriter.QuoteChar:

tests/ServiceStack.Text.Tests/JsonTests/OnDeserializationErrorTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ public void TestReset()
5757
}
5858

5959
[Test]
60-
public void Invokes_callback_deserialization_of_array_with_missing_comma()
60+
public void StrictMode_throws_Exception_on_array_with_missing_comma()
6161
{
62+
Env.StrictMode = true;
6263
string json = @"{""Values"": [ { ""Val"": ""a""} { ""Val"": ""b""}] }";
64+
65+
Assert.Throws<SerializationException>(() => json.FromJson<TestDtoWithArray>());
6366

64-
AssertThatInvalidJsonInvokesExpectedCallback<TestDtoWithArray>(json, "Values", @"[ { ""Val"": ""a""} { ""Val"": ""b""}]", typeof(TestChildDto[]), null);
67+
Env.StrictMode = false;
6568
}
6669

6770
[Test]

0 commit comments

Comments
 (0)