]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_json.h
5 #include <include/types.h>
10 #pragma push_macro("_ASSERT_H")
13 #include "json_spirit/json_spirit.h"
17 #pragma pop_macro("_ASSERT_H")
20 #include "Formatter.h"
23 using namespace json_spirit
;
29 typedef map
<string
, JSONObj
*>::iterator map_iter_t
;
36 void set(const JSONObjIter::map_iter_t
&_cur
, const JSONObjIter::map_iter_t
&_end
);
50 string name
; // corresponds to obj_type in XMLObj
53 multimap
<string
, JSONObj
*> children
;
54 map
<string
, string
> attr_map
;
55 void handle_value(Value v
);
59 JSONObj() : parent(NULL
){}
63 void init(JSONObj
*p
, Value v
, string n
);
65 string
& get_name() { return name
; }
66 string
& get_data() { return data_string
; }
67 bool get_data(const string
& key
, string
*dest
);
68 JSONObj
*get_parent();
69 void add_child(string el
, JSONObj
*child
);
70 bool get_attr(string name
, string
& attr
);
71 JSONObjIter
find(const string
& name
);
72 JSONObjIter
find_first();
73 JSONObjIter
find_first(const string
& name
);
74 JSONObj
*find_obj(const string
& name
);
76 friend ostream
& operator<<(ostream
&out
,
77 const JSONObj
&obj
); // does not work, FIXME
81 vector
<string
> get_array_elements();
84 class JSONParser
: public JSONObj
91 ~JSONParser() override
;
92 void handle_data(const char *s
, int len
);
94 bool parse(const char *buf_
, int len
);
97 bool parse(const char *file_name
);
99 const char *get_json() { return json_buffer
.c_str(); }
100 void set_failure() { success
= false; }
109 err(const string
& m
) : message(m
) {}
114 JSONDecoder(bufferlist
& bl
) {
115 if (!parser
.parse(bl
.c_str(), bl
.length())) {
116 cout
<< "JSONDecoder::err()" << std::endl
;
117 throw JSONDecoder::err("failed to parse JSON input");
122 static bool decode_json(const char *name
, T
& val
, JSONObj
*obj
, bool mandatory
= false);
125 static bool decode_json(const char *name
, C
& container
, void (*cb
)(C
&, JSONObj
*obj
), JSONObj
*obj
, bool mandatory
= false);
128 static void decode_json(const char *name
, T
& val
, T
& default_val
, JSONObj
*obj
);
132 void decode_json_obj(T
& val
, JSONObj
*obj
)
134 val
.decode_json(obj
);
137 static inline void decode_json_obj(string
& val
, JSONObj
*obj
)
139 val
= obj
->get_data();
142 void decode_json_obj(unsigned long long& val
, JSONObj
*obj
);
143 void decode_json_obj(long long& val
, JSONObj
*obj
);
144 void decode_json_obj(unsigned long& val
, JSONObj
*obj
);
145 void decode_json_obj(long& val
, JSONObj
*obj
);
146 void decode_json_obj(unsigned& val
, JSONObj
*obj
);
147 void decode_json_obj(int& val
, JSONObj
*obj
);
148 void decode_json_obj(bool& val
, JSONObj
*obj
);
149 void decode_json_obj(bufferlist
& val
, JSONObj
*obj
);
151 void decode_json_obj(utime_t
& val
, JSONObj
*obj
);
154 void decode_json_obj(list
<T
>& l
, JSONObj
*obj
)
158 JSONObjIter iter
= obj
->find_first();
160 for (; !iter
.end(); ++iter
) {
163 decode_json_obj(val
, o
);
169 void decode_json_obj(deque
<T
>& l
, JSONObj
*obj
)
173 JSONObjIter iter
= obj
->find_first();
175 for (; !iter
.end(); ++iter
) {
178 decode_json_obj(val
, o
);
184 void decode_json_obj(set
<T
>& l
, JSONObj
*obj
)
188 JSONObjIter iter
= obj
->find_first();
190 for (; !iter
.end(); ++iter
) {
193 decode_json_obj(val
, o
);
199 void decode_json_obj(vector
<T
>& l
, JSONObj
*obj
)
203 JSONObjIter iter
= obj
->find_first();
205 for (; !iter
.end(); ++iter
) {
208 decode_json_obj(val
, o
);
213 template<class K
, class V
>
214 void decode_json_obj(map
<K
, V
>& m
, JSONObj
*obj
)
218 JSONObjIter iter
= obj
->find_first();
220 for (; !iter
.end(); ++iter
) {
224 JSONDecoder::decode_json("key", key
, o
);
225 JSONDecoder::decode_json("val", val
, o
);
230 template<class K
, class V
>
231 void decode_json_obj(multimap
<K
, V
>& m
, JSONObj
*obj
)
235 JSONObjIter iter
= obj
->find_first();
237 for (; !iter
.end(); ++iter
) {
241 JSONDecoder::decode_json("key", key
, o
);
242 JSONDecoder::decode_json("val", val
, o
);
243 m
.insert(make_pair(key
, val
));
248 void decode_json_obj(C
& container
, void (*cb
)(C
&, JSONObj
*obj
), JSONObj
*obj
)
252 JSONObjIter iter
= obj
->find_first();
254 for (; !iter
.end(); ++iter
) {
261 bool JSONDecoder::decode_json(const char *name
, T
& val
, JSONObj
*obj
, bool mandatory
)
263 JSONObjIter iter
= obj
->find_first(name
);
266 string s
= "missing mandatory field " + string(name
);
274 decode_json_obj(val
, *iter
);
276 string s
= string(name
) + ": ";
285 bool JSONDecoder::decode_json(const char *name
, C
& container
, void (*cb
)(C
&, JSONObj
*), JSONObj
*obj
, bool mandatory
)
289 JSONObjIter iter
= obj
->find_first(name
);
292 string s
= "missing mandatory field " + string(name
);
299 decode_json_obj(container
, cb
, *iter
);
301 string s
= string(name
) + ": ";
310 void JSONDecoder::decode_json(const char *name
, T
& val
, T
& default_val
, JSONObj
*obj
)
312 JSONObjIter iter
= obj
->find_first(name
);
319 decode_json_obj(val
, *iter
);
322 string s
= string(name
) + ": ";
329 static void encode_json(const char *name
, const T
& val
, ceph::Formatter
*f
)
331 f
->open_object_section(name
);
338 void encode_json(const char *name
, const string
& val
, ceph::Formatter
*f
);
339 void encode_json(const char *name
, const char *val
, ceph::Formatter
*f
);
340 void encode_json(const char *name
, bool val
, ceph::Formatter
*f
);
341 void encode_json(const char *name
, int val
, ceph::Formatter
*f
);
342 void encode_json(const char *name
, unsigned val
, ceph::Formatter
*f
);
343 void encode_json(const char *name
, long val
, ceph::Formatter
*f
);
344 void encode_json(const char *name
, unsigned long val
, ceph::Formatter
*f
);
345 void encode_json(const char *name
, long long val
, ceph::Formatter
*f
);
346 void encode_json(const char *name
, const utime_t
& val
, ceph::Formatter
*f
);
347 void encode_json(const char *name
, const bufferlist
& bl
, ceph::Formatter
*f
);
348 void encode_json(const char *name
, long long unsigned val
, ceph::Formatter
*f
);
351 static void encode_json(const char *name
, const std::list
<T
>& l
, ceph::Formatter
*f
)
353 f
->open_array_section(name
);
354 for (typename
std::list
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
355 encode_json("obj", *iter
, f
);
360 static void encode_json(const char *name
, const std::deque
<T
>& l
, ceph::Formatter
*f
)
362 f
->open_array_section(name
);
363 for (typename
std::deque
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
364 encode_json("obj", *iter
, f
);
368 static void encode_json(const char *name
, const std::set
<T
>& l
, ceph::Formatter
*f
)
370 f
->open_array_section(name
);
371 for (typename
std::set
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
372 encode_json("obj", *iter
, f
);
378 static void encode_json(const char *name
, const std::vector
<T
>& l
, ceph::Formatter
*f
)
380 f
->open_array_section(name
);
381 for (typename
std::vector
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
382 encode_json("obj", *iter
, f
);
387 template<class K
, class V
>
388 static void encode_json(const char *name
, const std::map
<K
, V
>& m
, ceph::Formatter
*f
)
390 f
->open_array_section(name
);
391 for (typename
std::map
<K
, V
>::const_iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
392 f
->open_object_section("entry");
393 encode_json("key", i
->first
, f
);
394 encode_json("val", i
->second
, f
);
400 template<class K
, class V
>
401 static void encode_json(const char *name
, const std::multimap
<K
, V
>& m
, ceph::Formatter
*f
)
403 f
->open_array_section(name
);
404 for (typename
std::multimap
<K
, V
>::const_iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
405 f
->open_object_section("entry");
406 encode_json("key", i
->first
, f
);
407 encode_json("val", i
->second
, f
);
412 template<class K
, class V
>
413 void encode_json_map(const char *name
, const map
<K
, V
>& m
, ceph::Formatter
*f
)
415 f
->open_array_section(name
);
416 typename map
<K
,V
>::const_iterator iter
;
417 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
418 encode_json("obj", iter
->second
, f
);
424 template<class K
, class V
>
425 void encode_json_map(const char *name
, const char *index_name
,
426 const char *object_name
, const char *value_name
,
427 void (*cb
)(const char *, const V
&, ceph::Formatter
*, void *), void *parent
,
428 const map
<K
, V
>& m
, ceph::Formatter
*f
)
430 f
->open_array_section(name
);
431 typename map
<K
,V
>::const_iterator iter
;
432 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
434 f
->open_object_section("key_value");
435 f
->dump_string(index_name
, iter
->first
);
439 f
->open_object_section(object_name
);
443 cb(value_name
, iter
->second
, f
, parent
);
445 encode_json(value_name
, iter
->second
, f
);
458 template<class K
, class V
>
459 void encode_json_map(const char *name
, const char *index_name
,
460 const char *object_name
, const char *value_name
,
461 const map
<K
, V
>& m
, ceph::Formatter
*f
)
463 encode_json_map
<K
, V
>(name
, index_name
, object_name
, value_name
, NULL
, NULL
, m
, f
);
466 template<class K
, class V
>
467 void encode_json_map(const char *name
, const char *index_name
, const char *value_name
,
468 const map
<K
, V
>& m
, ceph::Formatter
*f
)
470 encode_json_map
<K
, V
>(name
, index_name
, NULL
, value_name
, NULL
, NULL
, m
, f
);