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 LLDB_TOOLS_DEBUGSERVER_SOURCE_JSON_H
10 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_JSON_H
12 #include "StdStringExtractor.h"
27 virtual void Write(std::ostream
&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(std::ostream
&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
80 // JSONNumber from an int32_t (or any other similar type) would fail to
83 template <typename T
, typename
std::enable_if
<
84 std::is_integral
<T
>::value
&&
85 std::is_unsigned
<T
>::value
>::type
* = nullptr>
86 explicit JSONNumber(T u
)
87 : JSONValue(JSONValue::Kind::Number
), m_data_type(DataType::Unsigned
) {
88 m_data
.m_unsigned
= u
;
92 typename
std::enable_if
<std::is_integral
<T
>::value
&&
93 std::is_signed
<T
>::value
>::type
* = nullptr>
94 explicit JSONNumber(T s
)
95 : JSONValue(JSONValue::Kind::Number
), m_data_type(DataType::Signed
) {
99 template <typename T
, typename
std::enable_if
<
100 std::is_floating_point
<T
>::value
>::type
* = nullptr>
101 explicit JSONNumber(T d
)
102 : JSONValue(JSONValue::Kind::Number
), m_data_type(DataType::Double
) {
106 ~JSONNumber() override
= default;
108 JSONNumber(const JSONNumber
&s
) = delete;
109 JSONNumber
&operator=(const JSONNumber
&s
) = delete;
111 void Write(std::ostream
&s
) override
;
113 uint64_t GetAsUnsigned() const;
115 int64_t GetAsSigned() const;
117 double GetAsDouble() const;
119 static bool classof(const JSONValue
*V
) {
120 return V
->GetKind() == JSONValue::Kind::Number
;
124 enum class DataType
: uint8_t { Unsigned
, Signed
, Double
} m_data_type
;
133 class JSONTrue
: public JSONValue
{
137 JSONTrue(const JSONTrue
&s
) = delete;
138 JSONTrue
&operator=(const JSONTrue
&s
) = delete;
140 void Write(std::ostream
&s
) override
;
142 typedef std::shared_ptr
<JSONTrue
> SP
;
144 static bool classof(const JSONValue
*V
) {
145 return V
->GetKind() == JSONValue::Kind::True
;
148 ~JSONTrue() override
= default;
151 class JSONFalse
: public JSONValue
{
155 JSONFalse(const JSONFalse
&s
) = delete;
156 JSONFalse
&operator=(const JSONFalse
&s
) = delete;
158 void Write(std::ostream
&s
) override
;
160 typedef std::shared_ptr
<JSONFalse
> SP
;
162 static bool classof(const JSONValue
*V
) {
163 return V
->GetKind() == JSONValue::Kind::False
;
166 ~JSONFalse() override
= default;
169 class JSONNull
: public JSONValue
{
173 JSONNull(const JSONNull
&s
) = delete;
174 JSONNull
&operator=(const JSONNull
&s
) = delete;
176 void Write(std::ostream
&s
) override
;
178 typedef std::shared_ptr
<JSONNull
> SP
;
180 static bool classof(const JSONValue
*V
) {
181 return V
->GetKind() == JSONValue::Kind::Null
;
184 ~JSONNull() override
= default;
187 class JSONObject
: public JSONValue
{
191 JSONObject(const JSONObject
&s
) = delete;
192 JSONObject
&operator=(const JSONObject
&s
) = delete;
194 void Write(std::ostream
&s
) override
;
196 typedef std::shared_ptr
<JSONObject
> SP
;
198 static bool classof(const JSONValue
*V
) {
199 return V
->GetKind() == JSONValue::Kind::Object
;
202 bool SetObject(const std::string
&key
, JSONValue::SP value
);
204 JSONValue::SP
GetObject(const std::string
&key
) const;
206 /// Return keyed value as bool
209 /// The value of the key to lookup
211 /// \param[out] value
212 /// The value of the key as a bool. Undefined if the key doesn't
213 /// exist or if the key is not either true or false.
216 /// true if the key existed as was a bool value; false otherwise.
217 /// Note the return value is *not* the value of the bool, use
218 /// \b value for that.
219 bool GetObjectAsBool(const std::string
&key
, bool &value
) const;
221 bool GetObjectAsString(const std::string
&key
, std::string
&value
) const;
223 ~JSONObject() override
= default;
226 typedef std::map
<std::string
, JSONValue::SP
> Map
;
227 typedef Map::iterator Iterator
;
231 class JSONArray
: public JSONValue
{
235 JSONArray(const JSONArray
&s
) = delete;
236 JSONArray
&operator=(const JSONArray
&s
) = delete;
238 void Write(std::ostream
&s
) override
;
240 typedef std::shared_ptr
<JSONArray
> SP
;
242 static bool classof(const JSONValue
*V
) {
243 return V
->GetKind() == JSONValue::Kind::Array
;
247 typedef std::vector
<JSONValue::SP
> Vector
;
248 typedef Vector::iterator Iterator
;
249 typedef Vector::size_type Index
;
250 typedef Vector::size_type Size
;
253 bool SetObject(Index i
, JSONValue::SP value
);
255 bool AppendObject(JSONValue::SP value
);
257 JSONValue::SP
GetObject(Index i
);
259 Size
GetNumElements();
261 ~JSONArray() override
= default;
266 class JSONParser
: public StdStringExtractor
{
286 JSONParser(const char *cstr
);
288 int GetEscapedChar(bool &was_escaped
);
290 Token
GetToken(std::string
&value
);
292 JSONValue::SP
ParseJSONValue();
295 JSONValue::SP
ParseJSONValue(const std::string
&value
, const Token
&token
);
297 JSONValue::SP
ParseJSONObject();
299 JSONValue::SP
ParseJSONArray();
302 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_JSON_H