/
CouchJsonDocument.cs
91 lines (79 loc) · 2.08 KB
/
CouchJsonDocument.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
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Divan
{
/// <summary>
/// A CouchDocument that holds its contents as a parsed JObject DOM which can be used
/// as a "light weight" base document instead of CouchDocument.
/// The _id and _rev are held inside the JObject.
/// </summary>
public class CouchJsonDocument : ICouchDocument
{
public CouchJsonDocument(string json, string id, string rev)
{
Obj = JObject.Parse(json);
Id = id;
Rev = rev;
}
public CouchJsonDocument(string json, string id)
{
Obj = JObject.Parse(json);
Id = id;
}
public CouchJsonDocument(string json)
{
Obj = JObject.Parse(json);
}
public CouchJsonDocument(JObject doc)
{
Obj = doc;
}
public CouchJsonDocument()
{
Obj = new JObject();
}
public override string ToString()
{
return Obj.ToString();
}
public JObject Obj { get; set; }
#region ICouchDocument Members
public virtual void WriteJson(JsonWriter writer)
{
foreach (JToken token in Obj.Children())
{
token.WriteTo(writer);
}
}
// Presume that Obj has _id and _rev
public void ReadJson(JObject obj)
{
Obj = obj;
}
public string Rev
{
get
{
if (Obj["_rev"] == null)
{
return null;
}
return Obj["_rev"].Value<string>();
}
set { Obj["_rev"] = JToken.FromObject(value); }
}
public string Id
{
get
{
if (Obj["_id"] == null)
{
return null;
}
return Obj["_id"].Value<string>();
}
set { Obj["_id"] = JToken.FromObject(value); }
}
#endregion
}
}