diff --git a/node.gyp b/node.gyp index b0b6edffc93c7f..56774b574c956a 100644 --- a/node.gyp +++ b/node.gyp @@ -96,8 +96,6 @@ 'src/js_stream.cc', 'src/json_utils.cc', 'src/js_udp_wrap.cc', - 'src/json_parser.h', - 'src/json_parser.cc', 'src/module_wrap.cc', 'src/node.cc', 'src/node_api.cc', diff --git a/src/json_parser.cc b/src/json_parser.cc deleted file mode 100644 index 36d51e7d89d961..00000000000000 --- a/src/json_parser.cc +++ /dev/null @@ -1,157 +0,0 @@ -#include "json_parser.h" -#include "node_errors.h" -#include "node_v8_platform-inl.h" -#include "util-inl.h" - -namespace node { -using v8::Array; -using v8::Context; -using v8::Isolate; -using v8::Local; -using v8::Object; -using v8::String; -using v8::Value; - -JSONParser::JSONParser() {} - -bool JSONParser::Parse(const std::string& content) { - DCHECK(!parsed_); - - Isolate* isolate = isolate_.get(); - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - - Local context = Context::New(isolate); - Context::Scope context_scope(context); - - // It's not a real script, so don't print the source line. - errors::PrinterTryCatch bootstrapCatch( - isolate, errors::PrinterTryCatch::kDontPrintSourceLine); - Local json_string_value; - Local result_value; - if (!ToV8Value(context, content).ToLocal(&json_string_value) || - !json_string_value->IsString() || - !v8::JSON::Parse(context, json_string_value.As()) - .ToLocal(&result_value) || - !result_value->IsObject()) { - return false; - } - - context_.Reset(isolate, context); - content_.Reset(isolate, result_value.As()); - parsed_ = true; - - return true; -} - -std::optional JSONParser::GetTopLevelStringField( - std::string_view field) { - Isolate* isolate = isolate_.get(); - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - - Local context = context_.Get(isolate); - Context::Scope context_scope(context); - - Local content_object = content_.Get(isolate); - - Local value; - // It's not a real script, so don't print the source line. - errors::PrinterTryCatch bootstrapCatch( - isolate, errors::PrinterTryCatch::kDontPrintSourceLine); - Local field_local; - if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) { - return {}; - } - if (!content_object->Get(context, field_local).ToLocal(&value) || - !value->IsString()) { - return {}; - } - Utf8Value utf8_value(isolate, value); - return utf8_value.ToString(); -} - -std::optional JSONParser::GetTopLevelBoolField(std::string_view field) { - Isolate* isolate = isolate_.get(); - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - - Local context = context_.Get(isolate); - Context::Scope context_scope(context); - - Local content_object = content_.Get(isolate); - Local value; - bool has_field; - // It's not a real script, so don't print the source line. - errors::PrinterTryCatch bootstrapCatch( - isolate, errors::PrinterTryCatch::kDontPrintSourceLine); - Local field_local; - if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) { - return {}; - } - if (!content_object->Has(context, field_local).To(&has_field)) { - return {}; - } - if (!has_field) { - return false; - } - if (!content_object->Get(context, field_local).ToLocal(&value) || - !value->IsBoolean()) { - return {}; - } - return value->BooleanValue(isolate); -} - -std::optional JSONParser::GetTopLevelStringDict( - std::string_view field) { - Isolate* isolate = isolate_.get(); - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - Local context = context_.Get(isolate); - Local content_object = content_.Get(isolate); - Local value; - bool has_field; - // It's not a real script, so don't print the source line. - errors::PrinterTryCatch bootstrapCatch( - isolate, errors::PrinterTryCatch::kDontPrintSourceLine); - Local field_local; - if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) { - return std::nullopt; - } - if (!content_object->Has(context, field_local).To(&has_field)) { - return std::nullopt; - } - if (!has_field) { - return StringDict(); - } - if (!content_object->Get(context, field_local).ToLocal(&value) || - !value->IsObject()) { - return std::nullopt; - } - Local dict = value.As(); - Local keys; - if (!dict->GetOwnPropertyNames(context).ToLocal(&keys)) { - return std::nullopt; - } - std::unordered_map result; - uint32_t length = keys->Length(); - for (uint32_t i = 0; i < length; ++i) { - Local key; - Local value; - if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) - return StringDict(); - if (!dict->Get(context, key).ToLocal(&value) || !value->IsString()) - return StringDict(); - - Utf8Value key_utf8(isolate, key); - Utf8Value value_utf8(isolate, value); - result.emplace(*key_utf8, *value_utf8); - } - return result; -} - -} // namespace node diff --git a/src/json_parser.h b/src/json_parser.h deleted file mode 100644 index d491736d68fb0a..00000000000000 --- a/src/json_parser.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef SRC_JSON_PARSER_H_ -#define SRC_JSON_PARSER_H_ - -#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS - -#include -#include -#include -#include -#include "util.h" -#include "v8.h" - -namespace node { -// This is intended to be used to get some top-level fields out of a JSON -// without having to spin up a full Node.js environment that unnecessarily -// complicates things. -class JSONParser { - public: - using StringDict = std::unordered_map; - JSONParser(); - ~JSONParser() = default; - bool Parse(const std::string& content); - std::optional GetTopLevelStringField(std::string_view field); - std::optional GetTopLevelBoolField(std::string_view field); - std::optional GetTopLevelStringDict(std::string_view field); - - private: - // We might want a lighter-weight JSON parser for this use case. But for now - // using V8 is good enough. - RAIIIsolateWithoutEntering isolate_; - - v8::Global context_; - v8::Global content_; - bool parsed_ = false; -}; -} // namespace node - -#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS - -#endif // SRC_JSON_PARSER_H_