-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataEx.cs
More file actions
97 lines (85 loc) · 2.88 KB
/
DataEx.cs
File metadata and controls
97 lines (85 loc) · 2.88 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.IO;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Formatting = System.Xml.Formatting;
namespace Inversion {
/// <summary>
/// Extension methods for <see cref="IData"/>
/// largely concerned with supporting both
/// `.ToXml(...)` and `.ToJson(...)`
/// </summary>
public static class DataEx {
/// <summary>
/// Generates an XML representation of the specified <see cref="IData"/> object.
/// </summary>
/// <param name="self">The data model to produce XML for.</param>
/// <returns>
/// Returns the XML representation as a `string`.
/// </returns>
/// <remarks>
/// This is implemented by creating a `StringWriter` and
/// calling `.ToXml(IData, StringWriter)`
/// </remarks>
public static string ToXml(this IData self) {
using (StringWriter str = new StringWriter()) {
self.ToXml(str);
return str.ToString();
}
}
/// <summary>
/// Produces an xml representation of the subject
/// `IData` object.
/// </summary>
/// <param name="self">The `IData` object to act upon.</param>
/// <param name="writer">The xml writer to write the representation to.</param>
public static void ToXml(this IData self, TextWriter writer) {
using (XmlTextWriter xml = new XmlTextWriter(writer)) {
xml.Formatting = Formatting.Indented;
self.ToXml(xml);
}
}
/// <summary>
/// Produces a json representation of the subject `IData` object.
/// </summary>
/// <param name="self">The `IData` object to act upon.</param>
/// <returns>Return the json representation of the `IData` object as a string.</returns>
public static string ToJson(this IData self) {
using (StringWriter str = new StringWriter()) {
self.ToJson(str);
return str.ToString();
}
}
/// <summary>
/// Produces a json representation of the subject `IData` object.
/// </summary>
/// <param name="self">The `IData` object to act upon.</param>
/// <param name="writer">The text writer the representation should be writtern to.</param>
public static void ToJson(this IData self, TextWriter writer) {
using (JsonTextWriter json = new JsonTextWriter(writer)) {
json.Formatting = Newtonsoft.Json.Formatting.Indented;
self.ToJson(json);
}
}
/// <summary>
/// Provides a JSON Object view of the objects data.
/// </summary>
/// <param name="self">The `IData` object to act upon.</param>
/// <returns>
/// Returns a `JObject` representation of this objects data.
/// </returns>
public static JObject ToJsonObject(this IData self) {
return JObject.Parse(self.ToJson());
}
/// <summary>
/// Provides a data view snapshot of the data object.
/// </summary>
/// <param name="self">The `IData` object to act upon.</param>
/// <returns>
/// Returns a `DataView` snapshot of the data object.
/// </returns>
public static DataView ToDataView(this IData self) {
return new DataView(self);
}
}
}