1 //===---------------------JSON.h --------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef utility_JSON_h_
10 #define utility_JSON_h_
12 #include "lldb/Utility/StringExtractor.h"
17 #include <type_traits>
22 namespace lldb_private {
27 virtual void Write(Stream &s) = 0;
29 typedef std::shared_ptr<JSONValue> SP;
31 enum class Kind { String, Number, True, False, Null, Object, Array };
33 JSONValue(Kind k) : m_kind(k) {}
35 Kind GetKind() const { return m_kind; }
37 virtual ~JSONValue() = default;
43 class JSONString : public JSONValue {
46 JSONString(const char *s);
47 JSONString(const std::string &s);
49 JSONString(const JSONString &s) = delete;
50 JSONString &operator=(const JSONString &s) = delete;
52 void Write(Stream &s) override;
54 typedef std::shared_ptr<JSONString> SP;
56 std::string GetData() { return m_data; }
58 static bool classof(const JSONValue *V) {
59 return V->GetKind() == JSONValue::Kind::String;
62 ~JSONString() override = default;
65 static std::string json_string_quote_metachars(const std::string &);
70 class JSONNumber : public JSONValue {
72 typedef std::shared_ptr<JSONNumber> SP;
74 // We cretae a constructor for all integer and floating point type with using
76 // SFINAE to avoid having ambiguous overloads because of the implicit type
78 // would have constructors only with int64_t, uint64_t and double types then
79 // constructing a JSONNumber from an int32_t (or any other similar type)
80 // would fail to compile.
82 template <typename T, typename std::enable_if<
83 std::is_integral<T>::value &&
84 std::is_unsigned<T>::value>::type * = nullptr>
85 explicit JSONNumber(T u)
86 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Unsigned) {
87 m_data.m_unsigned = u;
91 typename std::enable_if<std::is_integral<T>::value &&
92 std::is_signed<T>::value>::type * = nullptr>
93 explicit JSONNumber(T s)
94 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Signed) {
98 template <typename T, typename std::enable_if<
99 std::is_floating_point<T>::value>::type * = nullptr>
100 explicit JSONNumber(T d)
101 : JSONValue(JSONValue::Kind::Number), m_data_type(DataType::Double) {
105 ~JSONNumber() override = default;
107 JSONNumber(const JSONNumber &s) = delete;
108 JSONNumber &operator=(const JSONNumber &s) = delete;
110 void Write(Stream &s) override;
112 uint64_t GetAsUnsigned() const;
114 int64_t GetAsSigned() const;
116 double GetAsDouble() const;
118 static bool classof(const JSONValue *V) {
119 return V->GetKind() == JSONValue::Kind::Number;
123 enum class DataType : uint8_t { Unsigned, Signed, Double } m_data_type;
132 class JSONTrue : public JSONValue {
136 JSONTrue(const JSONTrue &s) = delete;
137 JSONTrue &operator=(const JSONTrue &s) = delete;
139 void Write(Stream &s) override;
141 typedef std::shared_ptr<JSONTrue> SP;
143 static bool classof(const JSONValue *V) {
144 return V->GetKind() == JSONValue::Kind::True;
147 ~JSONTrue() override = default;
150 class JSONFalse : public JSONValue {
154 JSONFalse(const JSONFalse &s) = delete;
155 JSONFalse &operator=(const JSONFalse &s) = delete;
157 void Write(Stream &s) override;
159 typedef std::shared_ptr<JSONFalse> SP;
161 static bool classof(const JSONValue *V) {
162 return V->GetKind() == JSONValue::Kind::False;
165 ~JSONFalse() override = default;
168 class JSONNull : public JSONValue {
172 JSONNull(const JSONNull &s) = delete;
173 JSONNull &operator=(const JSONNull &s) = delete;
175 void Write(Stream &s) override;
177 typedef std::shared_ptr<JSONNull> SP;
179 static bool classof(const JSONValue *V) {
180 return V->GetKind() == JSONValue::Kind::Null;
183 ~JSONNull() override = default;
186 class JSONObject : public JSONValue {
190 JSONObject(const JSONObject &s) = delete;
191 JSONObject &operator=(const JSONObject &s) = delete;
193 void Write(Stream &s) override;
195 typedef std::shared_ptr<JSONObject> SP;
197 static bool classof(const JSONValue *V) {
198 return V->GetKind() == JSONValue::Kind::Object;
201 bool SetObject(const std::string &key, JSONValue::SP value);
203 JSONValue::SP GetObject(const std::string &key);
205 ~JSONObject() override = default;
208 typedef std::map<std::string, JSONValue::SP> Map;
209 typedef Map::iterator Iterator;
213 class JSONArray : public JSONValue {
217 JSONArray(const JSONArray &s) = delete;
218 JSONArray &operator=(const JSONArray &s) = delete;
220 void Write(Stream &s) override;
222 typedef std::shared_ptr<JSONArray> SP;
224 static bool classof(const JSONValue *V) {
225 return V->GetKind() == JSONValue::Kind::Array;
229 typedef std::vector<JSONValue::SP> Vector;
230 typedef Vector::iterator Iterator;
231 typedef Vector::size_type Index;
232 typedef Vector::size_type Size;
235 bool SetObject(Index i, JSONValue::SP value);
237 bool AppendObject(JSONValue::SP value);
239 JSONValue::SP GetObject(Index i);
241 Size GetNumElements();
243 ~JSONArray() override = default;
248 class JSONParser : public StringExtractor {
268 JSONParser(llvm::StringRef data);
270 int GetEscapedChar(bool &was_escaped);
272 Token GetToken(std::string &value);
274 JSONValue::SP ParseJSONValue();
277 JSONValue::SP ParseJSONObject();
279 JSONValue::SP ParseJSONArray();
281 } // namespace lldb_private
283 #endif // utility_JSON_h_