File: json_wrapper.h

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (216 lines) | stat: -rw-r--r-- 5,286 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef CERT_TRANS_UTIL_JSON_WRAPPER_H_
#define CERT_TRANS_UTIL_JSON_WRAPPER_H_

#include <glog/logging.h>
#include <json.h>
#undef TRUE   // json.h pollution
#undef FALSE  // json.h pollution

#include <event2/buffer.h>
#include <sstream>
#include <string>

#include "base/macros.h"
#include "proto/serializer.h"
#include "util/util.h"

class JsonArray;

// It appears that a new object, e.g. from a string, has a reference count
// of 1, and that any objects "got" from it will get freed when it is freed.

// Note that a JsonObject that is not Ok() should not be used for anything.
class JsonObject {
 public:
  explicit JsonObject(json_object* obj) : obj_(obj) {
  }

  explicit JsonObject(const std::ostringstream& response) {
    obj_ = json_tokener_parse(response.str().c_str());
  }

  explicit JsonObject(const std::string& response) {
    obj_ = json_tokener_parse(response.c_str());
  }

  // This constructor is destructive: if a JSON object is parsed
  // correctly, it will remove it from the front of the buffer. In
  // case of an error, the buffer is left unchanged.
  explicit JsonObject(evbuffer* buffer);

  JsonObject(const JsonArray& from, int offset,
             json_type type = json_type_object);

  JsonObject(const JsonObject& from, const char* field) {
    InitFromChild(from, field, json_type_object);
  }

  JsonObject() : obj_(json_object_new_object()) {
  }

  ~JsonObject() {
    if (obj_)
      json_object_put(obj_);
  }

  // Get the object out, and stop tracking it so we _won't_ put() it
  // when we are destroyed. The caller needs to ensure it is freed.
  json_object* Extract() {
    json_object* tmp = obj_;
    obj_ = NULL;
    return tmp;
  }

  bool Ok() const {
    return obj_ != NULL;
  }

  bool IsType(json_type type) const {
    return json_object_is_type(obj_, type);
  }

  const char* ToJson() const {
    return json_object_to_json_string(obj_);
  }

  void Add(const char* name, const JsonObject& addand) {
    Add(name, json_object_get(addand.obj_));
  }

  void Add(const char* name, int64_t value) {
    Add(name, json_object_new_int64(value));
  }

  void Add(const char* name, const std::string& value) {
    Add(name, json_object_new_string(value.c_str()));
  }

  void AddBase64(const char* name, const std::string& value) {
    Add(name, util::ToBase64(value));
  }

  void Add(const char* name, const ct::DigitallySigned& ds) {
    std::string signature;
    CHECK_EQ(Serializer::SerializeDigitallySigned(ds, &signature),
             cert_trans::serialization::SerializeResult::OK);
    AddBase64(name, signature);
  }

  void AddBoolean(const char* name, bool b) {
    Add(name, json_object_new_boolean(b));
  }

  const char* ToString() const {
    return json_object_to_json_string(obj_);
  }

  std::string DebugString() const {
    return json_object_to_json_string_ext(obj_, JSON_C_TO_STRING_PRETTY);
  }

 protected:
  JsonObject(const JsonObject& from, const char* field, json_type type) {
    InitFromChild(from, field, type);
  }

  json_object* obj_;

 private:
  void InitFromChild(const JsonObject& from, const char* field,
                     json_type type) {
    if (json_object_object_get_ex(from.obj_, field, &obj_)) {
      if (!json_object_is_type(obj_, type)) {
        LOG(ERROR) << "Don't understand " << field
                   << " field: " << from.ToJson();
        obj_ = NULL;
        return;
      }
    } else {
      VLOG(2) << "No " << field << " field";
      return;
    }
    // Increment reference count
    json_object_get(obj_);
  }

  void Add(const char* name, json_object* obj) {
    json_object_object_add(obj_, name, obj);
  }

  DISALLOW_COPY_AND_ASSIGN(JsonObject);
};

class JsonBoolean : public JsonObject {
 public:
  JsonBoolean(const JsonObject& from, const char* field)
      : JsonObject(from, field, json_type_boolean) {
  }

  bool Value() const {
    return json_object_get_boolean(obj_);
  }
};

class JsonString : public JsonObject {
 public:
  JsonString(const JsonObject& from, const char* field)
      : JsonObject(from, field, json_type_string) {
  }

  JsonString(const JsonArray& from, int offset)
      : JsonObject(from, offset, json_type_string) {
  }

  const char* Value() const {
    return json_object_get_string(obj_);
  }

  std::string FromBase64() {
    return util::FromBase64(Value());
  }
};

class JsonInt : public JsonObject {
 public:
  explicit JsonInt(json_object* jint) : JsonObject(jint) {
  }
  JsonInt(const JsonObject& from, const char* field)
      : JsonObject(from, field, json_type_int) {
  }

  int64_t Value() const {
    return json_object_get_int64(obj_);
  }
};

class JsonArray : public JsonObject {
 public:
  JsonArray(const JsonObject& from, const char* field)
      : JsonObject(from, field, json_type_array) {
  }

  JsonArray() : JsonObject(json_object_new_array()) {
  }

  void Add(json_object* addand) {
    json_object_array_add(obj_, addand);
  }

  void Add(const std::string& addand) {
    Add(json_object_new_string(addand.c_str()));
  }

  void Add(JsonObject* addand) {
    Add(addand->Extract());
  }

  void AddBase64(const std::string& addand) {
    Add(util::ToBase64(addand));
  }

  int Length() const {
    return json_object_array_length(obj_);
  }
};

#endif  // CERT_TRANS_UTIL_JSON_WRAPPER_H_