v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
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
15namespace v8 {
16namespace internal {
17namespace torque {
18namespace ls {
19
20struct JsonValue;
21
22using JsonObject = std::map<std::string, JsonValue>;
23using JsonArray = std::vector<JsonValue>;
24
25struct JsonValue {
26 public:
28
29 // JsonValues can only be moved, not copied.
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
37 static JsonValue From(double number) {
40 result.number_ = number;
41 return result;
42 }
43
44 static JsonValue From(JsonObject object) {
47 result.object_ = std::make_unique<JsonObject>(std::move(object));
48 return result;
49 }
50
51 static JsonValue From(bool b) {
54 result.flag_ = b;
55 return result;
56 }
57
58 static JsonValue From(const std::string& string) {
61 result.string_ = string;
62 return result;
63 }
64
65 static JsonValue From(JsonArray array) {
68 result.array_ = std::make_unique<JsonArray>(std::move(array));
69 return result;
70 }
71
75 return result;
76 }
77
78 bool IsNumber() const { return tag == NUMBER; }
79 double ToNumber() const {
80 CHECK(IsNumber());
81 return number_;
82 }
83
84 bool IsBool() const { return tag == BOOL; }
85 bool ToBool() const {
86 CHECK(IsBool());
87 return flag_;
88 }
89
90 bool IsString() const { return tag == STRING; }
91 const std::string& ToString() const {
92 CHECK(IsString());
93 return string_;
94 }
95
96 bool IsObject() const { return object_ && tag == OBJECT; }
97 const JsonObject& ToObject() const {
98 CHECK(IsObject());
99 return *object_;
100 }
102 CHECK(IsObject());
103 return *object_;
104 }
105
106 bool IsArray() const { return array_ && tag == ARRAY; }
107 const JsonArray& ToArray() const {
108 CHECK(IsArray());
109 return *array_;
110 }
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
124std::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_
ZoneVector< RpoNumber > & result
std::map< std::string, JsonValue > JsonObject
Definition json.h:22
std::vector< JsonValue > JsonArray
Definition json.h:23
std::string SerializeToString(const JsonValue &value)
Definition json.cc:60
template const char * string
#define V8_NOEXCEPT
#define CHECK(condition)
Definition logging.h:124
static JsonValue From(JsonArray array)
Definition json.h:65
std::unique_ptr< JsonArray > array_
Definition json.h:121
std::unique_ptr< JsonObject > object_
Definition json.h:120
static JsonValue From(bool b)
Definition json.h:51
static JsonValue From(JsonObject object)
Definition json.h:44
enum v8::internal::torque::ls::JsonValue::@156 tag
static JsonValue From(double number)
Definition json.h:37
static JsonValue JsonNull()
Definition json.h:72
const std::string & ToString() const
Definition json.h:91
JsonValue() V8_NOEXCEPT=default
const JsonArray & ToArray() const
Definition json.h:107
const JsonObject & ToObject() const
Definition json.h:97
static JsonValue From(const std::string &string)
Definition json.h:58