1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h"
7
8 #include "src/counters.h"
9 #include "src/json-parser.h"
10 #include "src/json-stringifier.h"
11 #include "src/objects-inl.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // ES6 section 24.3.1 JSON.parse.
BUILTIN(JsonParse)17 BUILTIN(JsonParse) {
18 HandleScope scope(isolate);
19 Handle<Object> source = args.atOrUndefined(isolate, 1);
20 Handle<Object> reviver = args.atOrUndefined(isolate, 2);
21 Handle<String> string;
22 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, string,
23 Object::ToString(isolate, source));
24 string = String::Flatten(string);
25 RETURN_RESULT_OR_FAILURE(
26 isolate, string->IsSeqOneByteString()
27 ? JsonParser<true>::Parse(isolate, string, reviver)
28 : JsonParser<false>::Parse(isolate, string, reviver));
29 }
30
31 // ES6 section 24.3.2 JSON.stringify.
BUILTIN(JsonStringify)32 BUILTIN(JsonStringify) {
33 HandleScope scope(isolate);
34 JsonStringifier stringifier(isolate);
35 Handle<Object> object = args.atOrUndefined(isolate, 1);
36 Handle<Object> replacer = args.atOrUndefined(isolate, 2);
37 Handle<Object> indent = args.atOrUndefined(isolate, 3);
38 RETURN_RESULT_OR_FAILURE(isolate,
39 stringifier.Stringify(object, replacer, indent));
40 }
41
42 } // namespace internal
43 } // namespace v8
44