-
Notifications
You must be signed in to change notification settings - Fork 4
/
Snippets.cs
96 lines (80 loc) · 1.95 KB
/
Snippets.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2007 James Newton-King. All rights reserved.
// Use of this source code is governed by The MIT License,
// as found in the license.md file.
// ReSharper disable UnusedVariable
public class Snippets
{
[Fact]
public void LinqToJson()
{
#region LinqToJson
var jArray = new JArray
{
"Manual text",
new DateTime(2000, 5, 23)
};
var jObject = new JObject
{
["MyArray"] = jArray
};
var json = jObject.ToString();
// {
// "MyArray": [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// }
#endregion
}
[Fact]
public void SerializeJson()
{
#region SerializeJson
var product = new Product
{
Name = "Apple",
Expiry = new(2008, 12, 28),
Sizes = ["Small"]
};
var json = JsonConvert.SerializeObject(product);
// {
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
// }
#endregion
}
public class Product
{
public string Name { get; set; }
public DateTime Expiry { get; set; }
public string[] Sizes { get; set; }
}
[Fact]
public void DeserializeJson()
{
#region DeserializeJson
var json = """
{
'Name': 'Bad Boys',
'ReleaseDate': '1995-4-7T00:00:00',
'Genres': [
'Action',
'Comedy'
]
}
""";
var movie = JsonConvert.DeserializeObject<Movie>(json);
var name = movie.Name;
// Bad Boys
#endregion
}
class Movie
{
public string Name { get; set; }
public DateTime? ReleaseDate { get; set; }
public List<string> Genres { get; set; }
}
}