FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/json_parser.cc at main · fetchapi/node · GitHub
fetchapi
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
json_parser.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (85 loc) · 2.81 KB
Breadcrumbs
node
/
src
/
json_parser.cc
Copy path
File metadata and controls
101 lines (85 loc) · 2.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#
include
"
json_parser.h
"
#
include
"
node_errors.h
"
#
include
"
node_v8_platform-inl.h
"
#
include
"
util-inl.h
"
namespace
node
{
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::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::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::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);
}
}
//
namespace node
Back
|
FazBrowse Home
|
New Git URL