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 <memory> 10 #include <string> 11 #include <vector> 12 13 #include "src/base/logging.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 struct JsonValue { 26 public: 27 enum { OBJECT, ARRAY, STRING, NUMBER, BOOL, IS_NULL } tag; 28 29 // JsonValues can only be moved, not copied. 30 JsonValue() V8_NOEXCEPT = default; 31 constexpr JsonValue(const JsonValue& other) = delete; 32 JsonValue& operator=(const JsonValue& other) = delete; 33 34 JsonValue(JsonValue&& other) V8_NOEXCEPT = default; 35 JsonValue& operator=(JsonValue&& other) V8_NOEXCEPT = default; 36 FromJsonValue37 static JsonValue From(double number) { 38 JsonValue result; 39 result.tag = JsonValue::NUMBER; 40 result.number_ = number; 41 return result; 42 } 43 FromJsonValue44 static JsonValue From(JsonObject object) { 45 JsonValue result; 46 result.tag = JsonValue::OBJECT; 47 result.object_ = std::make_unique<JsonObject>(std::move(object)); 48 return result; 49 } 50 FromJsonValue51 static JsonValue From(bool b) { 52 JsonValue result; 53 result.tag = JsonValue::BOOL; 54 result.flag_ = b; 55 return result; 56 } 57 FromJsonValue58 static JsonValue From(const std::string& string) { 59 JsonValue result; 60 result.tag = JsonValue::STRING; 61 result.string_ = string; 62 return result; 63 } 64 FromJsonValue65 static JsonValue From(JsonArray array) { 66 JsonValue result; 67 result.tag = JsonValue::ARRAY; 68 result.array_ = std::make_unique<JsonArray>(std::move(array)); 69 return result; 70 } 71 JsonNullJsonValue72 static JsonValue JsonNull() { 73 JsonValue result; 74 result.tag = JsonValue::IS_NULL; 75 return result; 76 } 77 IsNumberJsonValue78 bool IsNumber() const { return tag == NUMBER; } ToNumberJsonValue79 double ToNumber() const { 80 CHECK(IsNumber()); 81 return number_; 82 } 83 IsBoolJsonValue84 bool IsBool() const { return tag == BOOL; } ToBoolJsonValue85 bool ToBool() const { 86 CHECK(IsBool()); 87 return flag_; 88 } 89 IsStringJsonValue90 bool IsString() const { return tag == STRING; } ToStringJsonValue91 const std::string& ToString() const { 92 CHECK(IsString()); 93 return string_; 94 } 95 IsObjectJsonValue96 bool IsObject() const { return object_ && tag == OBJECT; } ToObjectJsonValue97 const JsonObject& ToObject() const { 98 CHECK(IsObject()); 99 return *object_; 100 } ToObjectJsonValue101 JsonObject& ToObject() { 102 CHECK(IsObject()); 103 return *object_; 104 } 105 IsArrayJsonValue106 bool IsArray() const { return array_ && tag == ARRAY; } ToArrayJsonValue107 const JsonArray& ToArray() const { 108 CHECK(IsArray()); 109 return *array_; 110 } ToArrayJsonValue111 JsonArray& ToArray() { 112 CHECK(IsArray()); 113 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_ 132