forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnonymousTypes.cs
More file actions
50 lines (39 loc) · 1.3 KB
/
AnonymousTypes.cs
File metadata and controls
50 lines (39 loc) · 1.3 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
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class AnonymousTypes
: TestBase
{
[Test]
public void Can_serialize_anonymous_types()
{
Serialize(new { Id = 1, Name = "Name", IntList = new[] { 1, 2, 3 } }, includeXml: false); // xmlserializer cannot serialize anonymous types.
}
[Test]
public void Can_serialize_anonymous_type_and_read_as_string_Dictionary()
{
var json = JsonSerializer.SerializeToString(
new { Id = 1, Name = "Name", IntList = new[] { 1, 2, 3 } });
Console.WriteLine("JSON: " + json);
var map = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(json);
Console.WriteLine("MAP: " + map.Dump());
}
public class TestObj
{
public string Title1 { get; set; }
public object Title2 { get; set; }
}
[Test]
public void Escapes_string_in_object_correctly()
{
const string expectedValue = @"a\nb";
string json = string.Format(@"{{""Title1"":""{0}"",""Title2"":""{0}""}}", expectedValue);
var value = JsonSerializer.DeserializeFromString<TestObj>(json);
value.PrintDump();
Assert.That(value.Title1, Is.EqualTo(value.Title2.ToString()));
}
}
}