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 "lldb/Utility/StringExtractor.h"
18 #include <type_traits>
23 namespace lldb_private {
26 namespace lldb_private {
30 virtual void Write(Stream &s) = 0;
32 typedef std::shared_ptr<JSONValue> SP;
34 enum class Kind { String, Number, True, False, Null, Object, Array };
36 JSONValue(Kind k) : m_kind(k) {}
38 Kind GetKind() const { return m_kind; }
40 virtual ~JSONValue() = default;
46 class JSONString : public JSONValue {
49 JSONString(const char *s);
50 JSONString(const std::string &s);
52 JSONString(const JSONString &s) = delete;
53 JSONString &operator=(const JSONString &s) = delete;
55 void Write(Stream &s) override;
57 typedef std::shared_ptr<JSONString> SP;
59 std::string GetData() { return m_data; }
61 static bool classof(const JSONValue *V) {
62 return V->GetKind() == JSONValue::Kind::String;
65 ~JSONString() override = default;
68 static std::string json_string_quote_metachars(const std::string &);
73 class JSONNumber : public JSONValue {
75 typedef std::shared_ptr<JSONNumber> SP;
77 // We cretae a constructor for all integer and floating point type with using
79 // SFINAE to avoid having ambiguous overloads because of the implicit type
81 // would have constructors only with int64_t, uint64_t and double types then
83 // JSONNumber from an int32_t (or any other similar type) would fail to
86 template <typename T, typename std::enable_if<
87 std::is_integral<T>::value &&
88 std::is_unsigned<T>::value>::type * = nullptr>
89 explicit JSONNumber(T u)
90 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Unsigned) {
91 m_data.m_unsigned = u;
95 typename std::enable_if<std::is_integral<T>::value &&
96 std::is_signed<T>::value>::type * = nullptr>
97 explicit JSONNumber(T s)
98 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Signed) {
102 template <typename T, typename std::enable_if<
103 std::is_floating_point<T>::value>::type * = nullptr>
104 explicit JSONNumber(T d)
105 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Double) {
109 ~JSONNumber() override = default;
111 JSONNumber(const JSONNumber &s) = delete;
112 JSONNumber &operator=(const JSONNumber &s) = delete;
114 void Write(Stream &s) override;
116 uint64_t GetAsUnsigned() const;
118 int64_t GetAsSigned() const;
120 double GetAsDouble() const;
122 static bool classof(const JSONValue *V) {
123 return V->GetKind() == JSONValue::Kind::Number;
127 enum class DataType : uint8_t { Unsigned, Signed, Double } m_data_type;
136 class JSONTrue : public JSONValue {
140 JSONTrue(const JSONTrue &s) = delete;
141 JSONTrue &operator=(const JSONTrue &s) = delete;
143 void Write(Stream &s) override;
145 typedef std::shared_ptr<JSONTrue> SP;
147 static bool classof(const JSONValue *V) {
148 return V->GetKind() == JSONValue::Kind::True;
151 ~JSONTrue() override = default;
154 class JSONFalse : public JSONValue {
158 JSONFalse(const JSONFalse &s) = delete;
159 JSONFalse &operator=(const JSONFalse &s) = delete;
161 void Write(Stream &s) override;
163 typedef std::shared_ptr<JSONFalse> SP;
165 static bool classof(const JSONValue *V) {
166 return V->GetKind() == JSONValue::Kind::False;
169 ~JSONFalse() override = default;
172 class JSONNull : public JSONValue {
176 JSONNull(const JSONNull &s) = delete;
177 JSONNull &operator=(const JSONNull &s) = delete;
179 void Write(Stream &s) override;
181 typedef std::shared_ptr<JSONNull> SP;
183 static bool classof(const JSONValue *V) {
184 return V->GetKind() == JSONValue::Kind::Null;
187 ~JSONNull() override = default;
190 class JSONObject : public JSONValue {
194 JSONObject(const JSONObject &s) = delete;
195 JSONObject &operator=(const JSONObject &s) = delete;
197 void Write(Stream &s) override;
199 typedef std::shared_ptr<JSONObject> SP;
201 static bool classof(const JSONValue *V) {
202 return V->GetKind() == JSONValue::Kind::Object;
205 bool SetObject(const std::string &key, JSONValue::SP value);
207 JSONValue::SP GetObject(const std::string &key);
209 ~JSONObject() override = default;
212 typedef std::map<std::string, JSONValue::SP> Map;
213 typedef Map::iterator Iterator;
217 class JSONArray : public JSONValue {
221 JSONArray(const JSONArray &s) = delete;
222 JSONArray &operator=(const JSONArray &s) = delete;
224 void Write(Stream &s) override;
226 typedef std::shared_ptr<JSONArray> SP;
228 static bool classof(const JSONValue *V) {
229 return V->GetKind() == JSONValue::Kind::Array;
233 typedef std::vector<JSONValue::SP> Vector;
234 typedef Vector::iterator Iterator;
235 typedef Vector::size_type Index;
236 typedef Vector::size_type Size;
239 bool SetObject(Index i, JSONValue::SP value);
241 bool AppendObject(JSONValue::SP value);
243 JSONValue::SP GetObject(Index i);
245 Size GetNumElements();
247 ~JSONArray() override = default;
252 class JSONParser : public StringExtractor {
272 JSONParser(llvm::StringRef data);
274 int GetEscapedChar(bool &was_escaped);
276 Token GetToken(std::string &value);
278 JSONValue::SP ParseJSONValue();
281 JSONValue::SP ParseJSONObject();
283 JSONValue::SP ParseJSONArray();
285 } // namespace lldb_private
287 #endif // utility_JSON_h_