]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_jsonparser.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / rgw / rgw_jsonparser.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #include <errno.h>
5 #include <string.h>
6
7 #include <iostream>
8 #include <map>
9
10 #include "include/types.h"
11
12 #include "common/Formatter.h"
13 #include "common/ceph_json.h"
14
15 #include "rgw_common.h"
16
17 #define dout_subsys ceph_subsys_rgw
18
19
20 void dump_array(JSONObj *obj)
21 {
22
23 JSONObjIter iter = obj->find_first();
24
25 for (; !iter.end(); ++iter) {
26 JSONObj *o = *iter;
27 cout << "data=" << o->get_data() << std::endl;
28 }
29
30 }
31
32 struct Key {
33 string user;
34 string access_key;
35 string secret_key;
36
37 void decode_json(JSONObj *obj) {
38 JSONDecoder::decode_json("user", user, obj);
39 JSONDecoder::decode_json("access_key", access_key, obj);
40 JSONDecoder::decode_json("secret_key", secret_key, obj);
41 }
42 };
43
44 struct UserInfo {
45 string uid;
46 string display_name;
47 int max_buckets;
48 list<Key> keys;
49
50 void decode_json(JSONObj *obj) {
51 JSONDecoder::decode_json("user_id", uid, obj);
52 JSONDecoder::decode_json("display_name", display_name, obj);
53 JSONDecoder::decode_json("max_buckets", max_buckets, obj);
54 JSONDecoder::decode_json("keys", keys, obj);
55 }
56 };
57
58
59 int main(int argc, char **argv) {
60 JSONParser parser;
61
62 char buf[1024];
63 bufferlist bl;
64
65 for (;;) {
66 int done;
67 int len;
68
69 len = fread(buf, 1, sizeof(buf), stdin);
70 if (ferror(stdin)) {
71 cerr << "read error" << std::endl;
72 exit(-1);
73 }
74 done = feof(stdin);
75
76 bool ret = parser.parse(buf, len);
77 if (!ret)
78 cerr << "parse error" << std::endl;
79
80 if (done) {
81 bl.append(buf, len);
82 break;
83 }
84 }
85
86 JSONObjIter iter = parser.find_first();
87
88 for (; !iter.end(); ++iter) {
89 JSONObj *obj = *iter;
90 cout << "is_object=" << obj->is_object() << std::endl;
91 cout << "is_array=" << obj->is_array() << std::endl;
92 cout << "name=" << obj->get_name() << std::endl;
93 cout << "data=" << obj->get_data() << std::endl;
94 }
95
96 iter = parser.find_first("conditions");
97 if (!iter.end()) {
98 JSONObj *obj = *iter;
99
100 JSONObjIter iter2 = obj->find_first();
101 for (; !iter2.end(); ++iter2) {
102 JSONObj *child = *iter2;
103 cout << "is_object=" << child->is_object() << std::endl;
104 cout << "is_array=" << child->is_array() << std::endl;
105 if (child->is_array()) {
106 dump_array(child);
107 }
108 cout << "name=" << child->get_name() <<std::endl;
109 cout << "data=" << child->get_data() <<std::endl;
110 }
111 }
112
113 RGWUserInfo ui;
114
115 try {
116 ui.decode_json(&parser);
117 } catch (const JSONDecoder::err& e) {
118 cout << "failed to decode JSON input: " << e.what() << std::endl;
119 exit(1);
120 }
121
122 JSONFormatter formatter(true);
123
124 formatter.open_object_section("user_info");
125 ui.dump(&formatter);
126 formatter.close_section();
127
128 formatter.flush(std::cout);
129
130 std::cout << std::endl;
131 }
132