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
82 // constructing a JSONNumber from an int32_t (or any other similar type)
83 // would fail to compile.
85 template <typename T, typename std::enable_if<
86 std::is_integral<T>::value &&
87 std::is_unsigned<T>::value>::type * = nullptr>
88 explicit JSONNumber(T u)
89 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Unsigned) {
90 m_data.m_unsigned = u;
94 typename std::enable_if<std::is_integral<T>::value &&
95 std::is_signed<T>::value>::type * = nullptr>
96 explicit JSONNumber(T s)
97 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Signed) {
101 template <typename T, typename std::enable_if<
102 std::is_floating_point<T>::value>::type * = nullptr>
103 explicit JSONNumber(T d)
104 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Double) {
108 ~JSONNumber() override = default;
110 JSONNumber(const JSONNumber &s) = delete;
111 JSONNumber &operator=(const JSONNumber &s) = delete;
113 void Write(Stream &s) override;
115 uint64_t GetAsUnsigned() const;
117 int64_t GetAsSigned() const;
119 double GetAsDouble() const;
121 static bool classof(const JSONValue *V) {
122 return V->GetKind() == JSONValue::Kind::Number;
126 enum class DataType : uint8_t { Unsigned, Signed, Double } m_data_type;
135 class JSONTrue : public JSONValue {
139 JSONTrue(const JSONTrue &s) = delete;
140 JSONTrue &operator=(const JSONTrue &s) = delete;
142 void Write(Stream &s) override;
144 typedef std::shared_ptr<JSONTrue> SP;
146 static bool classof(const JSONValue *V) {
147 return V->GetKind() == JSONValue::Kind::True;
150 ~JSONTrue() override = default;
153 class JSONFalse : public JSONValue {
157 JSONFalse(const JSONFalse &s) = delete;
158 JSONFalse &operator=(const JSONFalse &s) = delete;
160 void Write(Stream &s) override;
162 typedef std::shared_ptr<JSONFalse> SP;
164 static bool classof(const JSONValue *V) {
165 return V->GetKind() == JSONValue::Kind::False;
168 ~JSONFalse() override = default;
171 class JSONNull : public JSONValue {
175 JSONNull(const JSONNull &s) = delete;
176 JSONNull &operator=(const JSONNull &s) = delete;
178 void Write(Stream &s) override;
180 typedef std::shared_ptr<JSONNull> SP;
182 static bool classof(const JSONValue *V) {
183 return V->GetKind() == JSONValue::Kind::Null;
186 ~JSONNull() override = default;
189 class JSONObject : public JSONValue {
193 JSONObject(const JSONObject &s) = delete;
194 JSONObject &operator=(const JSONObject &s) = delete;
196 void Write(Stream &s) override;
198 typedef std::shared_ptr<JSONObject> SP;
200 static bool classof(const JSONValue *V) {
201 return V->GetKind() == JSONValue::Kind::Object;
204 bool SetObject(const std::string &key, JSONValue::SP value);
206 JSONValue::SP GetObject(const std::string &key);
208 ~JSONObject() override = default;
211 typedef std::map<std::string, JSONValue::SP> Map;
212 typedef Map::iterator Iterator;
216 class JSONArray : public JSONValue {
220 JSONArray(const JSONArray &s) = delete;
221 JSONArray &operator=(const JSONArray &s) = delete;
223 void Write(Stream &s) override;
225 typedef std::shared_ptr<JSONArray> SP;
227 static bool classof(const JSONValue *V) {
228 return V->GetKind() == JSONValue::Kind::Array;
232 typedef std::vector<JSONValue::SP> Vector;
233 typedef Vector::iterator Iterator;
234 typedef Vector::size_type Index;
235 typedef Vector::size_type Size;
238 bool SetObject(Index i, JSONValue::SP value);
240 bool AppendObject(JSONValue::SP value);
242 JSONValue::SP GetObject(Index i);
244 Size GetNumElements();
246 ~JSONArray() override = default;
251 class JSONParser : public StringExtractor {
271 JSONParser(llvm::StringRef data);
273 int GetEscapedChar(bool &was_escaped);
275 Token GetToken(std::string &value);
277 JSONValue::SP ParseJSONValue();
280 JSONValue::SP ParseJSONObject();
282 JSONValue::SP ParseJSONArray();
284 } // namespace lldb_private
286 #endif // utility_JSON_h_