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

Commit 2a8b9e3

Browse files
committed
Add InvalidJsonTests
1 parent 2513a28 commit 2a8b9e3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Runtime.Serialization;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Text.Tests.JsonTests
5+
{
6+
[TestFixture]
7+
public class InvalidJsonTests
8+
{
9+
public class Invalid1
10+
{
11+
public string ExtId { get; set; }
12+
public string Name { get; set; }
13+
public string[] Mobiles { get; set; }
14+
}
15+
16+
[Test]
17+
public void Does_parse_invalid_JSON()
18+
{
19+
var json = "[{\"ExtId\":\"2\",\"Name\":\"VIP sj�lland\",\"Mobiles\":[\"4533333333\",\"4544444444\"]";
20+
21+
var dto = json.FromJson<Invalid1[]>();
22+
23+
Assert.That(dto[0].ExtId, Is.EqualTo("2"));
24+
Assert.That(dto[0].Name, Is.EqualTo("VIP sj�lland"));
25+
Assert.That(dto[0].Mobiles, Is.Null);
26+
}
27+
28+
[Test]
29+
public void Does_throw_on_invalid_JSON()
30+
{
31+
JsConfig.ThrowOnDeserializationError = true;
32+
33+
var json = "[{\"ExtId\":\"2\",\"Name\":\"VIP sj�lland\",\"Mobiles\":[\"4533333333\",\"4544444444\"]";
34+
35+
Assert.Throws<SerializationException>(() =>
36+
json.FromJson<Invalid1[]>());
37+
38+
JsConfig.Reset();
39+
}
40+
}
41+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
</ItemGroup>
178178
<ItemGroup>
179179
<Compile Include="AttributeTests.cs" />
180+
<Compile Include="JsonTests\InvalidJsonTests.cs" />
180181
<Compile Include="SerializationDelegatePerformanceTests.cs" />
181182
<Compile Include="SerializationHookTests.cs" />
182183
<Compile Include="StaticAccessorTests.cs" />

0 commit comments

Comments
 (0)