Common
A library of common classes.
JsonSerialization.cs
1using System.IO;
2using System.Runtime.Serialization.Json;
3using System.Text;
4
6{
10 public static class JsonSerialization
11 {
17 public static string GetJsonString<T>(T graph, JsonWriterSettings settings = null)
18 {
19 var serializer = new DataContractJsonSerializer(typeof(T));
20 settings = settings ?? JsonWriterSettings.PrettyPrintSettings();
21
22 using (var stream = new MemoryStream())
23 {
24 using (var writer = JsonReaderWriterFactory.CreateJsonWriter(stream,
25 settings.Encoding, false, settings.Indent, settings.IndentCharacters))
26 {
27 serializer.WriteObject(writer, graph);
28 }
29
30 stream.Seek(0, SeekOrigin.Begin);
31
32 using (var reader = new StreamReader(stream))
33 {
34 return reader.ReadToEnd();
35 }
36 }
37 }
38
44 public static void SaveJson<T>(string path, T graph)
45 {
46 var serializer = new DataContractJsonSerializer(typeof(T));
47
48 using (var stream = File.Create(path))
49 {
50 serializer.WriteObject(stream, graph);
51 }
52 }
53
58 public static T LoadJson<T>(string path)
59 {
60 var serializer = new DataContractJsonSerializer(typeof(T));
61
62 using (var stream = File.OpenRead(path))
63 {
64 return (T)serializer.ReadObject(stream);
65 }
66 }
67
72 public static T LoadJson<T>(byte[] bytes)
73 {
74 var serializer = new DataContractJsonSerializer(typeof(T));
75
76 using (var stream = new MemoryStream(bytes))
77 {
78 return (T)serializer.ReadObject(stream);
79 }
80 }
81
86 public static T LoadJsonString<T>(string json)
87 {
88 return LoadJson<T>(Encoding.UTF8.GetBytes(json));
89 }
90
97 public static void SaveEncryptedJson<T>(string path, T graph, byte[] key)
98 {
99 var serializer = new DataContractJsonSerializer(typeof(T));
100
101 using (var stream = File.Create(path))
102 {
103 Cryptography.EncryptToStream(stream, serializer, graph, key);
104 }
105 }
106
112 public static T LoadEncryptedJson<T>(string path, byte[] key)
113 {
114 var serializer = new DataContractJsonSerializer(typeof(T));
115
116 using (var stream = File.OpenRead(path))
117 {
118 return Cryptography.DecryptFromStream<T>(stream, serializer, key);
119 }
120 }
121 }
122}
Contains methods related to encryption and decryption.
Definition: Cryptography.cs:11
Contains methods for serializing objects to and from JSON.
static void SaveJson< T >(string path, T graph)
Serializes the object as JSON to the specified file path.
static T LoadJsonString< T >(string json)
Loads a JSON object from a JSON string.
static string GetJsonString< T >(T graph, JsonWriterSettings settings=null)
Returns the JSON string for the object graph.
static T LoadJson< T >(string path)
Loads a JSON object from the specified path.
static void SaveEncryptedJson< T >(string path, T graph, byte[] key)
Serializes the object as encrypted JSON to the specified path.
static T LoadEncryptedJson< T >(string path, byte[] key)
Loads the object from an encrypted JSON file.
Contains settings for JSON writer formatting.
static JsonWriterSettings PrettyPrintSettings()
Returns new settings for pretty printing.