forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphicInstanceTest.cs
More file actions
70 lines (56 loc) · 1.48 KB
/
PolymorphicInstanceTest.cs
File metadata and controls
70 lines (56 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using ServiceStack.Text.Common;
namespace ServiceStack.Text.Tests.JsonTests
{
[TestFixture]
public class PolymorphicInstanceTest
{
[SetUp]
public void SetUp()
{
JsConfig.Reset();
JsConfig<ICat>.ExcludeTypeInfo = false;
}
[Test]
public void Can_deserialise_polymorphic_dog_exact()
{
var dog =
JsonSerializer.DeserializeFromString<Dog>(
@"{""__type"":"""
+ typeof(Dog).ToTypeString()
+ @""",""Name"":""Fido""}");
Assert.That(dog.Name, Is.EqualTo(@"Fido"));
}
[Test]
public void Can_deserialise_polymorphic_list_exact_with_no_side_effect_for_bad_type_position()
{
var dog =
JsonSerializer.DeserializeFromString<Dog>(
@"{""Name"":""Fido"",""__type"":"""
+ typeof(Dog).ToTypeString() + @"""}");
Assert.That(dog.Name, Is.EqualTo(@"Fido"));
}
[Test]
public void Should_not_even_instantiate_incorrect_type()
{
var json = @"{""__type"":"""
+ typeof(TestClass).ToTypeString() + @""", ""Name"":""Fido""}";
var dog = JsonSerializer.DeserializeFromString<Dog>(
json);
Assert.IsFalse(TestClass.Called);
}
public class TestClass
{
public static bool Called { get; set; }
public TestClass()
{
Called = true;
}
}
}
}