~ubuntuone-client-engineering/droidcouch/trunk

« back to all changes in this revision

Viewing changes to src/se/msc/android/droidcouch/CouchJsonDocument.java

  • Committer: Igor
  • Date: 2010-01-07 12:40:44 UTC
  • Revision ID: git-v1:f59a39b97a8ebc0d17fafee44c96bc95e953ba91
First port commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package se.msc.android.droidcouch;
 
2
 
 
3
import java.util.Iterator;
 
4
 
 
5
import org.json.JSONException;
 
6
import org.json.JSONObject;
 
7
import org.json.JSONStringer;
 
8
 
 
9
public class CouchJsonDocument implements ICouchDocument {
 
10
    protected String Id;
 
11
    protected String Rev;
 
12
    public JSONObject Obj;
 
13
    
 
14
        public CouchJsonDocument(String json, String id, String rev) throws JSONException
 
15
    {
 
16
        Obj = new JSONObject(json);
 
17
        Id = id;
 
18
        Rev = rev;
 
19
    }
 
20
 
 
21
    public CouchJsonDocument(String json, String id) throws JSONException
 
22
    {
 
23
        Obj = new JSONObject(json);
 
24
        Id = id;
 
25
    }
 
26
 
 
27
    public CouchJsonDocument(String json) throws JSONException
 
28
    {
 
29
        Obj = new JSONObject(json);
 
30
    }
 
31
 
 
32
    public CouchJsonDocument(JSONObject doc)
 
33
    {
 
34
        Obj = doc;
 
35
    }
 
36
 
 
37
    public CouchJsonDocument()
 
38
    {
 
39
        Obj = new JSONObject();
 
40
    }
 
41
 
 
42
    public String toString()
 
43
    {
 
44
        return Obj.toString();
 
45
    }
 
46
 
 
47
 
 
48
 
 
49
    public void WriteJson(JSONStringer writer) 
 
50
    {
 
51
        Iterator i = Obj.keys();
 
52
        while (i.hasNext()) {
 
53
                String key = (String) i.next();
 
54
                try {
 
55
                                writer.key(key).value(Obj.get(key));
 
56
                        } catch (JSONException e) {
 
57
                                // TODO Auto-generated catch block
 
58
                                e.printStackTrace();
 
59
                        }
 
60
        }
 
61
    }
 
62
 
 
63
    // Presume that Obj has _id and _rev
 
64
    public void ReadJson(JSONObject obj)
 
65
    {
 
66
        Obj = obj;
 
67
    }
 
68
 
 
69
    public String Rev()
 
70
    {
 
71
        return Obj.optString("_rev");
 
72
    }
 
73
    public void Rev(String newValue)
 
74
    {
 
75
        try {
 
76
                        Obj.put("_rev", newValue);
 
77
                } catch (JSONException e) {
 
78
                        // TODO Auto-generated catch block
 
79
                        e.printStackTrace();
 
80
                }
 
81
    }
 
82
 
 
83
    public String Id()
 
84
    {
 
85
        return Obj.optString("_id");
 
86
    }
 
87
    public void Id(String newValue)
 
88
    {
 
89
        try {
 
90
                        Obj.put("_id", newValue);
 
91
                } catch (JSONException e) {
 
92
                        // TODO Auto-generated catch block
 
93
                        e.printStackTrace();
 
94
                }
 
95
    }
 
96
 
 
97
}