|
| 1 | +#include "node_json.h" |
| 2 | + |
| 3 | +#include "crdtp/json.h" |
| 4 | + |
| 5 | +namespace node { |
| 6 | +namespace inspector { |
| 7 | + |
| 8 | +using crdtp::ParserHandler; |
| 9 | +using crdtp::span; |
| 10 | +using crdtp::Status; |
| 11 | +using protocol::Binary; |
| 12 | +using protocol::BinaryValue; |
| 13 | +using protocol::DictionaryValue; |
| 14 | +using protocol::FundamentalValue; |
| 15 | +using protocol::ListValue; |
| 16 | +using protocol::String; |
| 17 | +using protocol::StringUtil; |
| 18 | +using protocol::StringValue; |
| 19 | +using protocol::Value; |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +// Uses the parsing events received from driver of `ParserHandler` |
| 24 | +// (e.g. crdtp::json::ParseJSON) into a protocol::Value instance. |
| 25 | +class ValueParserHandler : public ParserHandler { |
| 26 | + public: |
| 27 | + // Provides the parsed protocol::Value. |
| 28 | + std::unique_ptr<Value> ReleaseRoot() { return std::move(root_); } |
| 29 | + |
| 30 | + // The first parsing error encountered; `status().ok()` is the default. |
| 31 | + Status status() const { return status_; } |
| 32 | + |
| 33 | + private: |
| 34 | + // Implementation of `ParserHandler`. |
| 35 | + void HandleMapBegin() override { |
| 36 | + if (!status_.ok()) return; |
| 37 | + std::unique_ptr<DictionaryValue> dict = DictionaryValue::create(); |
| 38 | + DictionaryValue* dict_ptr = dict.get(); |
| 39 | + AddValueToParent(std::move(dict)); |
| 40 | + stack_.emplace_back(dict_ptr); |
| 41 | + } |
| 42 | + |
| 43 | + void HandleMapEnd() override { |
| 44 | + if (!status_.ok()) return; |
| 45 | + DCHECK(!stack_.empty()); |
| 46 | + DCHECK(stack_.back().is_dict); |
| 47 | + stack_.pop_back(); |
| 48 | + } |
| 49 | + |
| 50 | + void HandleArrayBegin() override { |
| 51 | + if (!status_.ok()) return; |
| 52 | + std::unique_ptr<ListValue> list = ListValue::create(); |
| 53 | + ListValue* list_ptr = list.get(); |
| 54 | + AddValueToParent(std::move(list)); |
| 55 | + stack_.emplace_back(list_ptr); |
| 56 | + } |
| 57 | + |
| 58 | + void HandleArrayEnd() override { |
| 59 | + if (!status_.ok()) return; |
| 60 | + DCHECK(!stack_.empty()); |
| 61 | + DCHECK(!stack_.back().is_dict); |
| 62 | + stack_.pop_back(); |
| 63 | + } |
| 64 | + |
| 65 | + void HandleString8(span<uint8_t> chars) override { |
| 66 | + AddStringToParent(StringUtil::fromUTF8(chars.data(), chars.size())); |
| 67 | + } |
| 68 | + |
| 69 | + void HandleString16(span<uint16_t> chars) override { |
| 70 | + AddStringToParent(StringUtil::fromUTF16(chars.data(), chars.size())); |
| 71 | + } |
| 72 | + |
| 73 | + void HandleBinary(span<uint8_t> bytes) override { |
| 74 | + AddValueToParent( |
| 75 | + BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size()))); |
| 76 | + } |
| 77 | + |
| 78 | + void HandleDouble(double value) override { |
| 79 | + AddValueToParent(FundamentalValue::create(value)); |
| 80 | + } |
| 81 | + |
| 82 | + void HandleInt32(int32_t value) override { |
| 83 | + AddValueToParent(FundamentalValue::create(value)); |
| 84 | + } |
| 85 | + |
| 86 | + void HandleBool(bool value) override { |
| 87 | + AddValueToParent(FundamentalValue::create(value)); |
| 88 | + } |
| 89 | + |
| 90 | + void HandleNull() override { AddValueToParent(Value::null()); } |
| 91 | + |
| 92 | + void HandleError(Status error) override { status_ = error; } |
| 93 | + |
| 94 | + // Adding strings and values to the parent value. |
| 95 | + // Strings are handled separately because they can be keys for |
| 96 | + // dictionary values. |
| 97 | + void AddStringToParent(String str) { |
| 98 | + if (!status_.ok()) return; |
| 99 | + if (!root_) { |
| 100 | + DCHECK(!key_is_pending_); |
| 101 | + root_ = StringValue::create(str); |
| 102 | + } else if (stack_.back().is_dict) { |
| 103 | + // If we already have a pending key, then this is the value of the |
| 104 | + // key/value pair. Otherwise, it's the new pending key. |
| 105 | + if (key_is_pending_) { |
| 106 | + stack_.back().dict->setString(pending_key_, str); |
| 107 | + key_is_pending_ = false; |
| 108 | + } else { |
| 109 | + pending_key_ = std::move(str); |
| 110 | + key_is_pending_ = true; |
| 111 | + } |
| 112 | + } else { // Top of the stack is a list. |
| 113 | + DCHECK(!key_is_pending_); |
| 114 | + stack_.back().list->pushValue(StringValue::create(str)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + void AddValueToParent(std::unique_ptr<Value> value) { |
| 119 | + if (!status_.ok()) return; |
| 120 | + if (!root_) { |
| 121 | + DCHECK(!key_is_pending_); |
| 122 | + root_ = std::move(value); |
| 123 | + } else if (stack_.back().is_dict) { |
| 124 | + DCHECK(key_is_pending_); |
| 125 | + stack_.back().dict->setValue(pending_key_, std::move(value)); |
| 126 | + key_is_pending_ = false; |
| 127 | + } else { // Top of the stack is a list. |
| 128 | + DCHECK(!key_is_pending_); |
| 129 | + stack_.back().list->pushValue(std::move(value)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // `status_.ok()` is the default; if we receive an error event |
| 134 | + // we keep the first one and stop modifying any other state. |
| 135 | + Status status_; |
| 136 | + |
| 137 | + // The root of the parsed protocol::Value tree. |
| 138 | + std::unique_ptr<Value> root_; |
| 139 | + |
| 140 | + // If root_ is a list or a dictionary, this stack keeps track of |
| 141 | + // the container we're currently parsing as well as its ancestors. |
| 142 | + struct ContainerState { |
| 143 | + explicit ContainerState(DictionaryValue* dict) |
| 144 | + : is_dict(true), dict(dict) {} |
| 145 | + explicit ContainerState(ListValue* list) : is_dict(false), list(list) {} |
| 146 | + |
| 147 | + bool is_dict; |
| 148 | + union { |
| 149 | + DictionaryValue* dict; |
| 150 | + ListValue* list; |
| 151 | + }; |
| 152 | + }; |
| 153 | + std::vector<ContainerState> stack_; |
| 154 | + |
| 155 | + // For maps, keys and values are alternating events, so we keep the |
| 156 | + // key around and process it when the value arrives. |
| 157 | + bool key_is_pending_ = false; |
| 158 | + String pending_key_; |
| 159 | +}; |
| 160 | +} // anonymous namespace |
| 161 | + |
| 162 | +std::unique_ptr<Value> JsonUtil::ParseJSON(const uint8_t* chars, size_t size) { |
| 163 | + ValueParserHandler handler; |
| 164 | + crdtp::json::ParseJSON(span<uint8_t>(chars, size), &handler); |
| 165 | + if (handler.status().ok()) return handler.ReleaseRoot(); |
| 166 | + return nullptr; |
| 167 | +} |
| 168 | + |
| 169 | +std::unique_ptr<Value> JsonUtil::ParseJSON(const uint16_t* chars, size_t size) { |
| 170 | + ValueParserHandler handler; |
| 171 | + crdtp::json::ParseJSON(span<uint16_t>(chars, size), &handler); |
| 172 | + if (handler.status().ok()) return handler.ReleaseRoot(); |
| 173 | + return nullptr; |
| 174 | +} |
| 175 | + |
| 176 | +std::unique_ptr<Value> JsonUtil::parseJSON(const std::string_view string) { |
| 177 | + if (string.empty()) return nullptr; |
| 178 | + |
| 179 | + return ParseJSON(reinterpret_cast<const uint8_t*>(string.data()), |
| 180 | + string.size()); |
| 181 | +} |
| 182 | + |
| 183 | +std::unique_ptr<Value> JsonUtil::parseJSON(v8_inspector::StringView string) { |
| 184 | + if (string.length() == 0) return nullptr; |
| 185 | + if (string.is8Bit()) return ParseJSON(string.characters8(), string.length()); |
| 186 | + return ParseJSON(string.characters16(), string.length()); |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace inspector |
| 190 | +} // namespace node |
0 commit comments