]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_json.h
4 #include <include/types.h>
8 #pragma push_macro("_ASSERT_H")
11 #include "json_spirit/json_spirit.h"
15 #pragma pop_macro("_ASSERT_H")
18 #include "Formatter.h"
20 using namespace json_spirit
;
26 typedef map
<string
, JSONObj
*>::iterator map_iter_t
;
33 void set(const JSONObjIter::map_iter_t
&_cur
, const JSONObjIter::map_iter_t
&_end
);
47 string name
; // corresponds to obj_type in XMLObj
50 multimap
<string
, JSONObj
*> children
;
51 map
<string
, string
> attr_map
;
52 void handle_value(Value v
);
56 JSONObj() : parent(NULL
){}
60 void init(JSONObj
*p
, Value v
, string n
);
62 string
& get_name() { return name
; }
63 string
& get_data() { return data_string
; }
64 bool get_data(const string
& key
, string
*dest
);
65 JSONObj
*get_parent();
66 void add_child(string el
, JSONObj
*child
);
67 bool get_attr(string name
, string
& attr
);
68 JSONObjIter
find(const string
& name
);
69 JSONObjIter
find_first();
70 JSONObjIter
find_first(const string
& name
);
71 JSONObj
*find_obj(const string
& name
);
73 friend ostream
& operator<<(ostream
&out
,
74 const JSONObj
&obj
); // does not work, FIXME
78 vector
<string
> get_array_elements();
81 class JSONParser
: public JSONObj
88 ~JSONParser() override
;
89 void handle_data(const char *s
, int len
);
91 bool parse(const char *buf_
, int len
);
94 bool parse(const char *file_name
);
96 const char *get_json() { return json_buffer
.c_str(); }
97 void set_failure() { success
= false; }
106 err(const string
& m
) : message(m
) {}
111 JSONDecoder(bufferlist
& bl
) {
112 if (!parser
.parse(bl
.c_str(), bl
.length())) {
113 cout
<< "JSONDecoder::err()" << std::endl
;
114 throw JSONDecoder::err("failed to parse JSON input");
119 static bool decode_json(const char *name
, T
& val
, JSONObj
*obj
, bool mandatory
= false);
122 static bool decode_json(const char *name
, C
& container
, void (*cb
)(C
&, JSONObj
*obj
), JSONObj
*obj
, bool mandatory
= false);
125 static void decode_json(const char *name
, T
& val
, T
& default_val
, JSONObj
*obj
);
129 void decode_json_obj(T
& val
, JSONObj
*obj
)
131 val
.decode_json(obj
);
134 static inline void decode_json_obj(string
& val
, JSONObj
*obj
)
136 val
= obj
->get_data();
139 void decode_json_obj(unsigned long long& val
, JSONObj
*obj
);
140 void decode_json_obj(long long& val
, JSONObj
*obj
);
141 void decode_json_obj(unsigned long& val
, JSONObj
*obj
);
142 void decode_json_obj(long& val
, JSONObj
*obj
);
143 void decode_json_obj(unsigned& val
, JSONObj
*obj
);
144 void decode_json_obj(int& val
, JSONObj
*obj
);
145 void decode_json_obj(bool& val
, JSONObj
*obj
);
146 void decode_json_obj(bufferlist
& val
, JSONObj
*obj
);
148 void decode_json_obj(utime_t
& val
, JSONObj
*obj
);
151 void decode_json_obj(list
<T
>& l
, JSONObj
*obj
)
155 JSONObjIter iter
= obj
->find_first();
157 for (; !iter
.end(); ++iter
) {
160 decode_json_obj(val
, o
);
166 void decode_json_obj(deque
<T
>& l
, JSONObj
*obj
)
170 JSONObjIter iter
= obj
->find_first();
172 for (; !iter
.end(); ++iter
) {
175 decode_json_obj(val
, o
);
181 void decode_json_obj(set
<T
>& l
, JSONObj
*obj
)
185 JSONObjIter iter
= obj
->find_first();
187 for (; !iter
.end(); ++iter
) {
190 decode_json_obj(val
, o
);
196 void decode_json_obj(vector
<T
>& l
, JSONObj
*obj
)
200 JSONObjIter iter
= obj
->find_first();
202 for (; !iter
.end(); ++iter
) {
205 decode_json_obj(val
, o
);
210 template<class K
, class V
, class C
= std::less
<K
> >
211 void decode_json_obj(map
<K
, V
, C
>& m
, JSONObj
*obj
)
215 JSONObjIter iter
= obj
->find_first();
217 for (; !iter
.end(); ++iter
) {
221 JSONDecoder::decode_json("key", key
, o
);
222 JSONDecoder::decode_json("val", val
, o
);
227 template<class K
, class V
>
228 void decode_json_obj(multimap
<K
, V
>& m
, JSONObj
*obj
)
232 JSONObjIter iter
= obj
->find_first();
234 for (; !iter
.end(); ++iter
) {
238 JSONDecoder::decode_json("key", key
, o
);
239 JSONDecoder::decode_json("val", val
, o
);
240 m
.insert(make_pair(key
, val
));
245 void decode_json_obj(C
& container
, void (*cb
)(C
&, JSONObj
*obj
), JSONObj
*obj
)
249 JSONObjIter iter
= obj
->find_first();
251 for (; !iter
.end(); ++iter
) {
258 bool JSONDecoder::decode_json(const char *name
, T
& val
, JSONObj
*obj
, bool mandatory
)
260 JSONObjIter iter
= obj
->find_first(name
);
263 string s
= "missing mandatory field " + string(name
);
271 decode_json_obj(val
, *iter
);
273 string s
= string(name
) + ": ";
282 bool JSONDecoder::decode_json(const char *name
, C
& container
, void (*cb
)(C
&, JSONObj
*), JSONObj
*obj
, bool mandatory
)
286 JSONObjIter iter
= obj
->find_first(name
);
289 string s
= "missing mandatory field " + string(name
);
296 decode_json_obj(container
, cb
, *iter
);
298 string s
= string(name
) + ": ";
307 void JSONDecoder::decode_json(const char *name
, T
& val
, T
& default_val
, JSONObj
*obj
)
309 JSONObjIter iter
= obj
->find_first(name
);
316 decode_json_obj(val
, *iter
);
319 string s
= string(name
) + ": ";
326 static void encode_json(const char *name
, const T
& val
, ceph::Formatter
*f
)
328 f
->open_object_section(name
);
335 void encode_json(const char *name
, const string
& val
, ceph::Formatter
*f
);
336 void encode_json(const char *name
, const char *val
, ceph::Formatter
*f
);
337 void encode_json(const char *name
, bool val
, ceph::Formatter
*f
);
338 void encode_json(const char *name
, int val
, ceph::Formatter
*f
);
339 void encode_json(const char *name
, unsigned val
, ceph::Formatter
*f
);
340 void encode_json(const char *name
, long val
, ceph::Formatter
*f
);
341 void encode_json(const char *name
, unsigned long val
, ceph::Formatter
*f
);
342 void encode_json(const char *name
, long long val
, ceph::Formatter
*f
);
343 void encode_json(const char *name
, const utime_t
& val
, ceph::Formatter
*f
);
344 void encode_json(const char *name
, const bufferlist
& bl
, ceph::Formatter
*f
);
345 void encode_json(const char *name
, long long unsigned val
, ceph::Formatter
*f
);
348 static void encode_json(const char *name
, const std::list
<T
>& l
, ceph::Formatter
*f
)
350 f
->open_array_section(name
);
351 for (typename
std::list
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
352 encode_json("obj", *iter
, f
);
357 static void encode_json(const char *name
, const std::deque
<T
>& l
, ceph::Formatter
*f
)
359 f
->open_array_section(name
);
360 for (typename
std::deque
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
361 encode_json("obj", *iter
, f
);
365 static void encode_json(const char *name
, const std::set
<T
>& l
, ceph::Formatter
*f
)
367 f
->open_array_section(name
);
368 for (typename
std::set
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
369 encode_json("obj", *iter
, f
);
375 static void encode_json(const char *name
, const std::vector
<T
>& l
, ceph::Formatter
*f
)
377 f
->open_array_section(name
);
378 for (typename
std::vector
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
379 encode_json("obj", *iter
, f
);
384 template<class K
, class V
, class C
= std::less
<K
> >
385 static void encode_json(const char *name
, const std::map
<K
, V
, C
>& m
, ceph::Formatter
*f
)
387 f
->open_array_section(name
);
388 for (typename
std::map
<K
, V
, C
>::const_iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
389 f
->open_object_section("entry");
390 encode_json("key", i
->first
, f
);
391 encode_json("val", i
->second
, f
);
397 template<class K
, class V
>
398 static void encode_json(const char *name
, const std::multimap
<K
, V
>& m
, ceph::Formatter
*f
)
400 f
->open_array_section(name
);
401 for (typename
std::multimap
<K
, V
>::const_iterator i
= m
.begin(); i
!= m
.end(); ++i
) {
402 f
->open_object_section("entry");
403 encode_json("key", i
->first
, f
);
404 encode_json("val", i
->second
, f
);
409 template<class K
, class V
>
410 void encode_json_map(const char *name
, const map
<K
, V
>& m
, ceph::Formatter
*f
)
412 f
->open_array_section(name
);
413 typename map
<K
,V
>::const_iterator iter
;
414 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
415 encode_json("obj", iter
->second
, f
);
421 template<class K
, class V
>
422 void encode_json_map(const char *name
, const char *index_name
,
423 const char *object_name
, const char *value_name
,
424 void (*cb
)(const char *, const V
&, ceph::Formatter
*, void *), void *parent
,
425 const map
<K
, V
>& m
, ceph::Formatter
*f
)
427 f
->open_array_section(name
);
428 typename map
<K
,V
>::const_iterator iter
;
429 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
431 f
->open_object_section("key_value");
432 f
->dump_string(index_name
, iter
->first
);
436 f
->open_object_section(object_name
);
440 cb(value_name
, iter
->second
, f
, parent
);
442 encode_json(value_name
, iter
->second
, f
);
455 template<class K
, class V
>
456 void encode_json_map(const char *name
, const char *index_name
,
457 const char *object_name
, const char *value_name
,
458 const map
<K
, V
>& m
, ceph::Formatter
*f
)
460 encode_json_map
<K
, V
>(name
, index_name
, object_name
, value_name
, NULL
, NULL
, m
, f
);
463 template<class K
, class V
>
464 void encode_json_map(const char *name
, const char *index_name
, const char *value_name
,
465 const map
<K
, V
>& m
, ceph::Formatter
*f
)
467 encode_json_map
<K
, V
>(name
, index_name
, NULL
, value_name
, NULL
, NULL
, m
, f
);