summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_jsonparser.cc
blob: 057572d980f871142024b3d8a435ab4e21a6c667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#include <errno.h>
#include <string.h>

#include <iostream>
#include <map>

#include "include/types.h"

#include "common/Formatter.h"
#include "common/ceph_json.h"

#include "rgw_common.h"

#define dout_subsys ceph_subsys_rgw


void dump_array(JSONObj *obj)
{

  JSONObjIter iter = obj->find_first();

  for (; !iter.end(); ++iter) { 
    JSONObj *o = *iter;
    cout << "data=" << o->get_data() << std::endl;
  }

}
                                  
struct Key {
  string user;
  string access_key;
  string secret_key;

  void decode_json(JSONObj *obj) {
    JSONDecoder::decode_json("user", user, obj);
    JSONDecoder::decode_json("access_key", access_key, obj);
    JSONDecoder::decode_json("secret_key", secret_key, obj);
  }
};

struct UserInfo {
  string uid;
  string display_name;
  int max_buckets;
  list<Key> keys;

  void decode_json(JSONObj *obj) {
    JSONDecoder::decode_json("user_id", uid, obj);
    JSONDecoder::decode_json("display_name", display_name, obj);
    JSONDecoder::decode_json("max_buckets", max_buckets, obj);
    JSONDecoder::decode_json("keys", keys, obj);
  }
};


int main(int argc, char **argv) {
  JSONParser parser;

  char buf[1024];
  bufferlist bl;

  for (;;) {
    int done;
    int len;

    len = fread(buf, 1, sizeof(buf), stdin);
    if (ferror(stdin)) {
      cerr << "read error" << std::endl;
      exit(-1);
    }
    done = feof(stdin);

    bool ret = parser.parse(buf, len);
    if (!ret)
      cerr << "parse error" << std::endl;

    if (done) {
      bl.append(buf, len);
      break;
    }
  }

  JSONObjIter iter = parser.find_first();

  for (; !iter.end(); ++iter) { 
    JSONObj *obj = *iter;
    cout << "is_object=" << obj->is_object() << std::endl;
    cout << "is_array=" << obj->is_array() << std::endl;
    cout << "name=" << obj->get_name() << std::endl;
    cout << "data=" << obj->get_data() << std::endl;
  }

  iter = parser.find_first("conditions");
  if (!iter.end()) {
    JSONObj *obj = *iter;

    JSONObjIter iter2 = obj->find_first();
    for (; !iter2.end(); ++iter2) {
      JSONObj *child = *iter2;
      cout << "is_object=" << child->is_object() << std::endl;
      cout << "is_array=" << child->is_array() << std::endl;
      if (child->is_array()) {
        dump_array(child);
      }
      cout << "name=" << child->get_name() <<std::endl;
      cout << "data=" << child->get_data() <<std::endl;
    }
  }

  RGWUserInfo ui;

  try {
    ui.decode_json(&parser);
  } catch (const JSONDecoder::err& e) {
    cout << "failed to decode JSON input: " << e.what() << std::endl;
    exit(1);
  }

  JSONFormatter formatter(true);

  formatter.open_object_section("user_info");
  ui.dump(&formatter);
  formatter.close_section();

  formatter.flush(std::cout);

  std::cout << std::endl;
}