-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSerializer.dib
204 lines (153 loc) · 3.68 KB
/
JsonSerializer.dib
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!markdown
# Remove old files
#!pwsh
Remove-Item "jsonser_*.json"
#!markdown
# Data Classes
#!csharp
#r "ExampleDataLib.dll"
public class Employee
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
}
public class Company
{
public string CompanyName { get; set; }
public string CompanyAddress { get; set; }
public List<Employee> Employees { get; set;}
}
#!markdown
# Data
#!csharp
#r "ExampleDataLib.dll"
using Data=ExampleDataLib;
var data = new[] {
new Data.Company
{
CompanyName = "Company One",
CompanyAddress = "In the middle of nowhere st. 1",
Employees = new List<Data.Employee>()
{
new Data.Employee
{
Name = "John",
LastName = "Smith",
Age = 30,
BirthDate = new DateTime(1991, 10, 7)
},
new Data.Employee
{
Name = "Alex",
LastName = "Max",
Age = 25,
BirthDate = new DateTime(1996, 1, 7)
}
}
},
new Data.Company
{
CompanyName = "Company Two",
CompanyAddress = "Somewhere in Mars",
Employees = new List<Data.Employee>()
{
new Data.Employee
{
Name = "Torii",
LastName = "Marel",
Age = 130,
BirthDate = new DateTime(1891, 7, 5)
}
}
}
};
#!csharp
data
#!markdown
# Serialization
#!csharp
#r "nuget:Newtonsoft.Json"
#r "ExampleDataLib.dll"
using System.IO;
using Newtonsoft.Json;
using (var streamWriter = new StreamWriter("jsonser_test.json"))
{
var jsonString = JsonConvert.SerializeObject(data);
streamWriter.Write(jsonString);
}
#!pwsh
Get-Content ".\jsonser_test.json"
#!markdown
# Deserialization
#!csharp
#r "nuget:Newtonsoft.Json"
#r "ExampleDataLib.dll"
using System.IO;
using Newtonsoft.Json;
using Data=ExampleDataLib;
Data.Company[] dataFromFile;
using (var stream = new StreamReader("jsonser_test.json"))
{
var jsonString = stream.ReadToEnd();
dataFromFile = JsonConvert.DeserializeObject<Data.Company[]>(jsonString);
}
display(dataFromFile);
#!markdown
# Serialization special exceptions
#!csharp
#r "nuget:Newtonsoft.Json"
#r "ExampleDataLib.dll"
using Newtonsoft.Json;
public class Employee
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
[JsonIgnore]
public int SystemId { get; set; }
}
public class Company
{
[JsonProperty("compName")]
public string CompanyName { get; set; }
public string CompanyAddress { get; set; }
public List<Employee> Employees { get; set;}
}
#!csharp
#r "ExampleDataLib.dll"
using Data=ExampleDataLib.CustomNames;
var data2 = new[] {
new Data.Company
{
CompanyName = "Company One",
CompanyAddress = "In the middle of nowhere st. 1",
Employees = new List<Data.Employee>()
{
new Data.Employee
{
Name = "John",
LastName = "Smith",
Age = 30,
BirthDate = new DateTime(1991, 10, 7),
SystemId = 5
}
}
}
};
#!csharp
data2
#!csharp
#r "nuget:Newtonsoft.Json"
#r "ExampleDataLib.dll"
using System.IO;
using Newtonsoft.Json;
using (var streamWriter = new StreamWriter("jsonser_test2.json"))
{
var jsonString = JsonConvert.SerializeObject(data2);
streamWriter.Write(jsonString);
}
#!pwsh
Get-Content ".\jsonser_test2.json"