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 1153 : struct JsonValue {
26 : public:
27 : enum { OBJECT, ARRAY, STRING, NUMBER, BOOL, IS_NULL } tag;
28 :
29 : static JsonValue From(double number) {
30 : JsonValue result;
31 24 : result.tag = JsonValue::NUMBER;
32 24 : result.number_ = number;
33 : return result;
34 : }
35 :
36 34 : static JsonValue From(JsonObject object) {
37 : JsonValue result;
38 34 : result.tag = JsonValue::OBJECT;
39 68 : result.object_ = base::make_unique<JsonObject>(std::move(object));
40 34 : return result;
41 : }
42 :
43 : static JsonValue From(bool b) {
44 : JsonValue result;
45 4 : result.tag = JsonValue::BOOL;
46 3 : result.flag_ = b;
47 : return result;
48 : }
49 :
50 : static JsonValue From(const std::string& string) {
51 : JsonValue result;
52 28 : result.tag = JsonValue::STRING;
53 : result.string_ = string;
54 : return result;
55 : }
56 :
57 6 : static JsonValue From(JsonArray array) {
58 : JsonValue result;
59 6 : result.tag = JsonValue::ARRAY;
60 12 : result.array_ = base::make_unique<JsonArray>(std::move(array));
61 6 : return result;
62 : }
63 :
64 : static JsonValue JsonNull() {
65 : JsonValue result;
66 2 : result.tag = JsonValue::IS_NULL;
67 : return result;
68 : }
69 :
70 : bool IsNumber() const { return tag == NUMBER; }
71 : double ToNumber() const {
72 19 : CHECK(IsNumber());
73 19 : return number_;
74 : }
75 :
76 : bool IsBool() const { return tag == BOOL; }
77 : bool ToBool() const {
78 4 : CHECK(IsBool());
79 4 : return flag_;
80 : }
81 :
82 : bool IsString() const { return tag == STRING; }
83 : const std::string& ToString() const {
84 15 : CHECK(IsString());
85 13 : return string_;
86 : }
87 :
88 236 : bool IsObject() const { return object_ && tag == OBJECT; }
89 37 : const JsonObject& ToObject() const {
90 37 : CHECK(IsObject());
91 37 : return *object_;
92 : }
93 142 : JsonObject& ToObject() {
94 142 : CHECK(IsObject());
95 142 : return *object_;
96 : }
97 :
98 16 : bool IsArray() const { return array_ && tag == ARRAY; }
99 4 : const JsonArray& ToArray() const {
100 4 : CHECK(IsArray());
101 4 : return *array_;
102 : }
103 6 : JsonArray& ToArray() {
104 6 : CHECK(IsArray());
105 6 : return *array_;
106 : }
107 :
108 : private:
109 : double number_ = 0;
110 : bool flag_ = false;
111 : std::string string_;
112 : std::unique_ptr<JsonObject> object_;
113 : std::unique_ptr<JsonArray> array_;
114 : };
115 :
116 : std::string SerializeToString(const JsonValue& value);
117 :
118 : } // namespace ls
119 : } // namespace torque
120 : } // namespace internal
121 : } // namespace v8
122 :
123 : #endif // V8_TORQUE_LS_JSON_H_
|