1 //===---------------------JSON.h --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef utility_JSON_h_
11 #define utility_JSON_h_
13 #include "StdStringExtractor.h"
28 virtual void Write(std::ostream &s) = 0;
30 typedef std::shared_ptr<JSONValue> SP;
32 enum class Kind { String, Number, True, False, Null, Object, Array };
34 JSONValue(Kind k) : m_kind(k) {}
36 Kind GetKind() const { return m_kind; }
38 virtual ~JSONValue() = default;
44 class JSONString : public JSONValue {
47 JSONString(const char *s);
48 JSONString(const std::string &s);
50 JSONString(const JSONString &s) = delete;
51 JSONString &operator=(const JSONString &s) = delete;
53 void Write(std::ostream &s) override;
55 typedef std::shared_ptr<JSONString> SP;
57 std::string GetData() { return m_data; }
59 static bool classof(const JSONValue *V) {
60 return V->GetKind() == JSONValue::Kind::String;
63 ~JSONString() override = default;
66 static std::string json_string_quote_metachars(const std::string &);
71 class JSONNumber : public JSONValue {
73 typedef std::shared_ptr<JSONNumber> SP;
75 // We cretae a constructor for all integer and floating point type with using
77 // SFINAE to avoid having ambiguous overloads because of the implicit type
79 // would have constructors only with int64_t, uint64_t and double types then
81 // JSONNumber from an int32_t (or any other similar type) would fail to
84 template <typename T, typename std::enable_if<
85 std::is_integral<T>::value &&
86 std::is_unsigned<T>::value>::type * = nullptr>
87 explicit JSONNumber(T u)
88 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Unsigned) {
89 m_data.m_unsigned = u;
93 typename std::enable_if<std::is_integral<T>::value &&
94 std::is_signed<T>::value>::type * = nullptr>
95 explicit JSONNumber(T s)
96 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Signed) {
100 template <typename T, typename std::enable_if<
101 std::is_floating_point<T>::value>::type * = nullptr>
102 explicit JSONNumber(T d)
103 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Double) {
107 ~JSONNumber() override = default;
109 JSONNumber(const JSONNumber &s) = delete;
110 JSONNumber &operator=(const JSONNumber &s) = delete;
112 void Write(std::ostream &s) override;
114 uint64_t GetAsUnsigned() const;
116 int64_t GetAsSigned() const;
118 double GetAsDouble() const;
120 static bool classof(const JSONValue *V) {
121 return V->GetKind() == JSONValue::Kind::Number;
125 enum class DataType : uint8_t { Unsigned, Signed, Double } m_data_type;
134 class JSONTrue : public JSONValue {
138 JSONTrue(const JSONTrue &s) = delete;
139 JSONTrue &operator=(const JSONTrue &s) = delete;
141 void Write(std::ostream &s) override;
143 typedef std::shared_ptr<JSONTrue> SP;
145 static bool classof(const JSONValue *V) {
146 return V->GetKind() == JSONValue::Kind::True;
149 ~JSONTrue() override = default;
152 class JSONFalse : public JSONValue {
156 JSONFalse(const JSONFalse &s) = delete;
157 JSONFalse &operator=(const JSONFalse &s) = delete;
159 void Write(std::ostream &s) override;
161 typedef std::shared_ptr<JSONFalse> SP;
163 static bool classof(const JSONValue *V) {
164 return V->GetKind() == JSONValue::Kind::False;
167 ~JSONFalse() override = default;
170 class JSONNull : public JSONValue {
174 JSONNull(const JSONNull &s) = delete;
175 JSONNull &operator=(const JSONNull &s) = delete;
177 void Write(std::ostream &s) override;
179 typedef std::shared_ptr<JSONNull> SP;
181 static bool classof(const JSONValue *V) {
182 return V->GetKind() == JSONValue::Kind::Null;
185 ~JSONNull() override = default;
188 class JSONObject : public JSONValue {
192 JSONObject(const JSONObject &s) = delete;
193 JSONObject &operator=(const JSONObject &s) = delete;
195 void Write(std::ostream &s) override;
197 typedef std::shared_ptr<JSONObject> SP;
199 static bool classof(const JSONValue *V) {
200 return V->GetKind() == JSONValue::Kind::Object;
203 bool SetObject(const std::string &key, JSONValue::SP value);
205 JSONValue::SP GetObject(const std::string &key) const;
207 // -------------------------------------------------------------------------
208 /// Return keyed value as bool
211 /// The value of the key to lookup
213 /// @param[out] value
214 /// The value of the key as a bool. Undefined if the key doesn't
215 /// exist or if the key is not either true or false.
218 /// true if the key existed as was a bool value; false otherwise.
219 /// Note the return value is *not* the value of the bool, use
220 /// \b value for that.
221 // -------------------------------------------------------------------------
222 bool GetObjectAsBool(const std::string &key, bool &value) const;
224 bool GetObjectAsString(const std::string &key, std::string &value) const;
226 ~JSONObject() override = default;
229 typedef std::map<std::string, JSONValue::SP> Map;
230 typedef Map::iterator Iterator;
234 class JSONArray : public JSONValue {
238 JSONArray(const JSONArray &s) = delete;
239 JSONArray &operator=(const JSONArray &s) = delete;
241 void Write(std::ostream &s) override;
243 typedef std::shared_ptr<JSONArray> SP;
245 static bool classof(const JSONValue *V) {
246 return V->GetKind() == JSONValue::Kind::Array;
250 typedef std::vector<JSONValue::SP> Vector;
251 typedef Vector::iterator Iterator;
252 typedef Vector::size_type Index;
253 typedef Vector::size_type Size;
256 bool SetObject(Index i, JSONValue::SP value);
258 bool AppendObject(JSONValue::SP value);
260 JSONValue::SP GetObject(Index i);
262 Size GetNumElements();
264 ~JSONArray() override = default;
269 class JSONParser : public StdStringExtractor {
289 JSONParser(const char *cstr);
291 int GetEscapedChar(bool &was_escaped);
293 Token GetToken(std::string &value);
295 JSONValue::SP ParseJSONValue();
298 JSONValue::SP ParseJSONObject();
300 JSONValue::SP ParseJSONArray();
303 #endif // utility_JSON_h_