Browse code

added copyright notice for jsonxx

git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@1922 8eb893ce-cfd4-0310-b710-fb5ebe64c474

Stefan Sayer authored on 19/05/2010 09:33:50
Showing 1 changed files
... ...
@@ -1,4 +1,31 @@
1 1
 // Author: Hong Jiang <hong@hjiang.net>
2
+/* 
3
+source: http://github.com/hjiang/jsonxx/
4
+
5
+Copyright (c) 2010 Hong Jiang
6
+
7
+Permission is hereby granted, free of charge, to any person
8
+obtaining a copy of this software and associated documentation
9
+files (the "Software"), to deal in the Software without
10
+restriction, including without limitation the rights to use,
11
+copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+copies of the Software, and to permit persons to whom the
13
+Software is furnished to do so, subject to the following
14
+conditions:
15
+
16
+The above copyright notice and this permission notice shall be
17
+included in all copies or substantial portions of the Software.
18
+
19
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
+OTHER DEALINGS IN THE SOFTWARE.
27
+
28
+*/
2 29
 
3 30
 #include <cassert>
4 31
 #include <iostream>
Browse code

moved json<->AmArg into core

git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@1920 8eb893ce-cfd4-0310-b710-fb5ebe64c474

Stefan Sayer authored on 19/05/2010 09:20:35
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,185 @@
1
+// Author: Hong Jiang <hong@hjiang.net>
2
+
3
+#include <cassert>
4
+#include <iostream>
5
+#include <map>
6
+#include <vector>
7
+
8
+namespace jsonxx {
9
+  bool match(const std::string& pattern, std::istream& input, 
10
+	     bool ignore_ws = true);
11
+  bool parse_string(std::istream& input, std::string* value);
12
+  bool parse_bool(std::istream& input, bool* value);
13
+  bool parse_null(std::istream& input);
14
+  bool parse_float(std::istream& input, double* value);
15
+  bool parse_number(std::istream& input, long* value);
16
+  bool parse_number(std::istream& input, int* value);
17
+  
18
+// TODO: *::parse() should be static functions.
19
+
20
+class Value;
21
+
22
+// A JSON Object
23
+class Object {
24
+  public:
25
+    Object();
26
+    ~Object();
27
+    bool parse(std::istream& input);
28
+
29
+    template <typename T>
30
+    bool has(const std::string& key);
31
+
32
+    // Always call has<>() first. If the key doesn't exist, consider
33
+    // the behavior undefined.
34
+    template <typename T>
35
+    T& get(const std::string& key);
36
+  private:
37
+    Object(const Object&);
38
+    Object& operator=(const Object&);
39
+
40
+    std::map<std::string, Value*> value_map_;
41
+};
42
+
43
+class Value;
44
+
45
+class Array {
46
+  public:
47
+    Array();
48
+    ~Array();
49
+    bool parse(std::istream& input);
50
+
51
+    unsigned int size() { return values_.size(); }
52
+
53
+    template <typename T>
54
+    bool has(unsigned int i);
55
+
56
+    template <typename T>
57
+    T& get(unsigned int i);
58
+
59
+  private:
60
+    Array(const Array&);
61
+    Array& operator=(const Array&);
62
+    std::vector<Value*> values_;
63
+};
64
+
65
+// A value could be a number, an array, a string, an object, a
66
+// boolean, or null
67
+class Value {
68
+  public:
69
+    class Null {};
70
+
71
+    Value();
72
+    ~Value();
73
+    bool parse(std::istream& input);
74
+    template<typename T>
75
+    bool is();
76
+    template<typename T>
77
+    T& get();
78
+  private:
79
+    Value(const Value&);
80
+    Value& operator=(const Value&);
81
+    enum {
82
+        INTEGER_,
83
+        STRING_,
84
+        BOOL_,
85
+        NULL_,
86
+        ARRAY_,
87
+        OBJECT_,
88
+        INVALID_
89
+    } type_;
90
+    union {
91
+        long integer_value_;
92
+        std::string* string_value_;
93
+        bool bool_value_;
94
+        Array* array_value_;
95
+        Object* object_value_;
96
+    };
97
+};
98
+
99
+template <typename T>
100
+bool Array::has(unsigned int i) {
101
+    if (i >= size()) {
102
+        return false;
103
+    } else {
104
+        return values_[i]->is<T>();
105
+    }
106
+}
107
+
108
+template <typename T>
109
+T& Array::get(unsigned int i) {
110
+    assert(i < size());
111
+    return values_[i]->get<T>();
112
+}
113
+
114
+template <typename T>
115
+bool Object::has(const std::string& key) {
116
+    return value_map_.count(key) > 0 && value_map_[key]->is<T>();
117
+}
118
+
119
+template <typename T>
120
+T& Object::get(const std::string& key) {
121
+    assert(has<T>(key));
122
+    return value_map_[key]->get<T>();
123
+}
124
+
125
+template<>
126
+inline bool Value::is<Value::Null>() {
127
+    return type_ == NULL_;
128
+}
129
+
130
+template<>
131
+inline bool Value::is<bool>() {
132
+    return type_ == BOOL_;
133
+}
134
+
135
+template<>
136
+inline bool Value::is<std::string>() {
137
+    return type_ == STRING_;
138
+}
139
+
140
+template<>
141
+inline bool Value::is<long>() {
142
+    return type_ == INTEGER_;
143
+}
144
+
145
+template<>
146
+inline bool Value::is<Array>() {
147
+    return type_ == ARRAY_;
148
+}
149
+
150
+template<>
151
+inline bool Value::is<Object>() {
152
+    return type_ == OBJECT_;
153
+}
154
+
155
+template<>
156
+inline bool& Value::get<bool>() {
157
+    assert(is<bool>());
158
+    return bool_value_;
159
+}
160
+
161
+template<>
162
+inline std::string& Value::get<std::string>() {
163
+    assert(is<std::string>());
164
+    return *string_value_;
165
+}
166
+
167
+template<>
168
+inline long& Value::get<long>() {
169
+    assert(is<long>());
170
+    return integer_value_;
171
+}
172
+
173
+template<>
174
+inline Array& Value::get<Array>() {
175
+    assert(is<Array>());
176
+    return *array_value_;
177
+}
178
+
179
+template<>
180
+inline Object& Value::get<Object>() {
181
+    assert(is<Object>());
182
+    return *object_value_;
183
+}
184
+
185
+}  // namespace jsonxx