-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONSerExample.cs
More file actions
73 lines (64 loc) · 2.15 KB
/
JSONSerExample.cs
File metadata and controls
73 lines (64 loc) · 2.15 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
71
72
73
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace JSONIronPythonExample
{
// indicates that this class can be serialized
[DataContract]
public class ParentDataClass
{
// indicates that this member will be serialized.
// fields are optional by default
// It's not a bad idea to provide defaults for them in the constructor.
[DataMember]
public int AnInteger;
[DataMember]
public List<object> AList;
[DataMember]
public ChildDataClass ACustomType;
// not all members need to be part of the data contract
private double aDouble;
public ParentDataClass()
{
// let's provide some defaults in the constructor
// objects start off as null unless you 'new' them
// ints and doubles and stuff should default to zero.
AList = new List<object>();
ACustomType = new ChildDataClass();
}
public string ToJSONString()
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ParentDataClass));
MemoryStream stream = new MemoryStream();
ser.WriteObject(stream, this);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
public static ParentDataClass FromJSONString(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ParentDataClass));
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(jsonString);
writer.Flush();
stream.Position = 0;
return (ParentDataClass)ser.ReadObject(stream);
}
}
[DataContract]
public class ChildDataClass
{
[DataMember]
public string AString;
public ChildDataClass()
{
AString = @"";
}
}
}