mirror of
https://github.com/nodejs/node.git
synced 2026-12-05 13:58:45 -05:00
Refs: https://github.com/nodejs/single-executable/discussions/60 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: https://github.com/nodejs/node/pull/47588 Fixes: https://github.com/nodejs/node/issues/47741 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#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 "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:
|
|
JSONParser();
|
|
~JSONParser() {}
|
|
bool Parse(const std::string& content);
|
|
std::optional<std::string> GetTopLevelStringField(std::string_view field);
|
|
std::optional<bool> GetTopLevelBoolField(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.
|
|
static void FreeIsolate(v8::Isolate* isolate);
|
|
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_;
|
|
DeleteFnPtr<v8::Isolate, FreeIsolate> isolate_;
|
|
v8::HandleScope handle_scope_;
|
|
v8::Global<v8::Context> context_;
|
|
v8::Context::Scope context_scope_;
|
|
v8::Global<v8::Object> content_;
|
|
bool parsed_ = false;
|
|
};
|
|
} // namespace node
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#endif // SRC_JSON_PARSER_H_
|