Repository: nodejs/node
Pull request: src: remove JSONParser

This is now unused after
https://github.com/nodejs/node/pull/59473 and and https://github.com/nodejs/node/pull/59323

Fixes: https://github.com/nodejs/node/issues/59288

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: node.gyp
patch: @@ -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',

filename: src/json_parser.cc
patch: @@ -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 = 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<Value> json_string_value;
-  Local<Value> result_value;
-  if (!ToV8Value(context, content).ToLocal(&json_string_value) ||
-      !json_string_value->IsString() ||
-      !v8::JSON::Parse(context, json_string_value.As<String>())
-           .ToLocal(&result_value) ||
-      !result_value->IsObject()) {
-    return false;
-  }
-
-  context_.Reset(isolate, context);
-  content_.Reset(isolate, result_value.As<Object>());
-  parsed_ = true;
-
-  return true;
-}
-
-std::optional<std::string> 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 = context_.Get(isolate);
-  Context::Scope context_scope(context);
-
-  Local<Object> content_object = content_.Get(isolate);
-
-  Local<Value> value;
-  // It's not a real script, so don't print the source line.
-  errors::PrinterTryCatch bootstrapCatch(
-      isolate, errors::PrinterTryCatch::kDontPrintSourceLine);
-  Local<Value> 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<bool> 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 = context_.Get(isolate);
-  Context::Scope context_scope(context);
-
-  Local<Object> content_object = content_.Get(isolate);
-  Local<Value> 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<Value> 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::StringDict> 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 = context_.Get(isolate);
-  Local<Object> content_object = content_.Get(isolate);
-  Local<Value> 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<Value> 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<Object> dict = value.As<Object>();
-  Local<Array> keys;
-  if (!dict->GetOwnPropertyNames(context).ToLocal(&keys)) {
-    return std::nullopt;
-  }
-  std::unordered_map<std::string, std::string> result;
-  uint32_t length = keys->Length();
-  for (uint32_t i = 0; i < length; ++i) {
-    Local<Value> key;
-    Local<Value> 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

filename: src/json_parser.h
patch: @@ -1,40 +0,0 @@
-#ifndef SRC_JSON_PARSER_H_
-#define SRC_JSON_PARSER_H_
-
-#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
-
-#include <memory>
-#include <optional>
-#include <string>
-#include <unordered_map>
-#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<std::string, std::string>;
-  JSONParser();
-  ~JSONParser() = default;
-  bool Parse(const std::string& content);
-  std::optional<std::string> GetTopLevelStringField(std::string_view field);
-  std::optional<bool> GetTopLevelBoolField(std::string_view field);
-  std::optional<StringDict> 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<v8::Context> context_;
-  v8::Global<v8::Object> content_;
-  bool parsed_ = false;
-};
-}  // namespace node
-
-#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
-
-#endif  // SRC_JSON_PARSER_H_