Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_TORQUE_LS_JSON_H_
6 : #define V8_TORQUE_LS_JSON_H_
7 :
8 : #include <map>
9 : #include <string>
10 : #include <vector>
11 :
12 : #include "src/base/logging.h"
13 : #include "src/base/template-utils.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 : namespace torque {
18 : namespace ls {
19 :
20 : struct JsonValue;
21 :
22 : using JsonObject = std::map<std::string, JsonValue>;
23 : using JsonArray = std::vector<JsonValue>;
24 :
25 914 : struct JsonValue {
26 : public:
27 : enum { OBJECT, ARRAY, STRING, NUMBER, BOOL, IS_NULL } tag;
28 :
29 : // JsonValues can only be moved, not copied.
30 187 : JsonValue() V8_NOEXCEPT = default;
31 : constexpr JsonValue(const JsonValue& other) = delete;
32 : JsonValue& operator=(const JsonValue& other) = delete;
33 :
34 127 : JsonValue(JsonValue&& other) V8_NOEXCEPT = default;
35 : JsonValue& operator=(JsonValue&& other) V8_NOEXCEPT = default;
36 :
37 : static JsonValue From(double number) {
38 : JsonValue result;
39 24 : result.tag = JsonValue::NUMBER;
40 24 : result.number_ = number;
41 : return result;
42 : }
43 :
44 34 : static JsonValue From(JsonObject object) {
45 : JsonValue result;
46 34 : result.tag = JsonValue::OBJECT;
47 68 : result.object_ = base::make_unique<JsonObject>(std::move(object));
48 34 : return result;
49 : }
50 :
51 : static JsonValue From(bool b) {
52 : JsonValue result;
53 4 : result.tag = JsonValue::BOOL;
54 3 : result.flag_ = b;
55 : return result;
56 : }
57 :
58 10 : static JsonValue From(const std::string& string) {
59 : JsonValue result;
60 28 : result.tag = JsonValue::STRING;
61 10 : result.string_ = string;
62 10 : return result;
63 : }
64 :
65 6 : static JsonValue From(JsonArray array) {
66 : JsonValue result;
67 6 : result.tag = JsonValue::ARRAY;
68 12 : result.array_ = base::make_unique<JsonArray>(std::move(array));
69 6 : return result;
70 : }
71 :
72 : static JsonValue JsonNull() {
73 : JsonValue result;
74 2 : result.tag = JsonValue::IS_NULL;
75 : return result;
76 : }
77 :
78 : bool IsNumber() const { return tag == NUMBER; }
79 : double ToNumber() const {
80 19 : CHECK(IsNumber());
81 19 : return number_;
82 : }
83 :
84 : bool IsBool() const { return tag == BOOL; }
85 : bool ToBool() const {
86 4 : CHECK(IsBool());
87 4 : return flag_;
88 : }
89 :
90 : bool IsString() const { return tag == STRING; }
91 : const std::string& ToString() const {
92 15 : CHECK(IsString());
93 13 : return string_;
94 : }
95 :
96 236 : bool IsObject() const { return object_ && tag == OBJECT; }
97 37 : const JsonObject& ToObject() const {
98 37 : CHECK(IsObject());
99 37 : return *object_;
100 : }
101 142 : JsonObject& ToObject() {
102 142 : CHECK(IsObject());
103 142 : return *object_;
104 : }
105 :
106 16 : bool IsArray() const { return array_ && tag == ARRAY; }
107 4 : const JsonArray& ToArray() const {
108 4 : CHECK(IsArray());
109 4 : return *array_;
110 : }
111 6 : JsonArray& ToArray() {
112 6 : CHECK(IsArray());
113 6 : return *array_;
114 : }
115 :
116 : private:
117 : double number_ = 0;
118 : bool flag_ = false;
119 : std::string string_;
120 : std::unique_ptr<JsonObject> object_;
121 : std::unique_ptr<JsonArray> array_;
122 : };
123 :
124 : std::string SerializeToString(const JsonValue& value);
125 :
126 : } // namespace ls
127 : } // namespace torque
128 : } // namespace internal
129 : } // namespace v8
130 :
131 : #endif // V8_TORQUE_LS_JSON_H_
|