-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJDataObject.cs
57 lines (50 loc) · 1.67 KB
/
JDataObject.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
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Inversion {
/// <summary>
/// Implements a `JObject` as an `IData` type.
/// </summary>
/// <remarks>
/// This is addressing a concern not disimilar to that being addressed by
/// `DataView` which is the presentation of data in abstract terms
/// especially for views or ad-hoc data.
/// </remarks>
public class JDataObject: JObject, IData {
/// <summary>
/// Instantiates a new `JDataObject` from an other `JObject`.
/// </summary>
/// <param name="other">
/// The `JObject` to copy data from.
/// </param>
public JDataObject(JObject other): base(other) {}
/// <summary>
/// Instantiates a new `JDataObject` from another `IDataObject`.
/// </summary>
/// <param name="other">
/// The `IData` object to copy data from.
/// </param>
public JDataObject(IData other) : base(other.ToJson()) { }
/// <summary>
/// Provides an abstract representation
/// of the objects data expressed as a JSON object.
/// </summary>
public JObject Data { get { return this; } }
/// <summary>
/// Produces an xml representation of the model.
/// </summary>
/// <param name="writer">The writer to used to write the xml to. </param>
public void ToXml(XmlWriter writer) {
string rootName = this["_type"].Value<string>() ?? "record";
XmlDocument xml = JsonConvert.DeserializeXmlNode(this.ToString(), rootName);
xml.WriteTo(writer);
}
/// <summary>
/// Produces a json respresentation of the model.
/// </summary>
/// <param name="writer">The writer to use for producing json.</param>
public void ToJson(JsonWriter writer) {
writer.WriteRaw(this.ToString());
}
}
}