1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * JSON serialization and deserialization operations.
13 #include <stdint.h> // uint32_t
15 #include "jstypes.h" // JS_PUBLIC_API
17 #include "js/RootingAPI.h" // JS::Handle, JS::MutableHandle
27 using JSONWriteCallback
= bool (*)(const char16_t
* buf
, uint32_t len
,
31 * Performs the JSON.stringify operation, as specified by ECMAScript, except
32 * writing stringified data by repeated calls of |callback|, with each such
33 * call passed |data| as argument.
35 extern JS_PUBLIC_API
bool JS_Stringify(JSContext
* cx
,
36 JS::MutableHandle
<JS::Value
> value
,
37 JS::Handle
<JSObject
*> replacer
,
38 JS::Handle
<JS::Value
> space
,
39 JSONWriteCallback callback
, void* data
);
44 * An API akin to JS_Stringify but with the goal of not having observable
45 * side-effects when the stringification is performed. This means it does not
46 * allow a replacer or a custom space and has the following constraints on its
49 * 1) The input must be a plain object or array, not an abitrary value.
50 * 2) Every value in the graph reached by the algorithm starting with this
51 * object must be one of the following: null, undefined, a string (NOT a
52 * string object!), a boolean, a finite number (i.e. no NaN or Infinity or
53 * -Infinity), a plain object with no accessor properties, or an Array with
56 * The actual behavior differs from JS_Stringify only in asserting the above and
57 * NOT attempting to get the "toJSON" property from things, since that could
58 * clearly have side-effects.
60 extern JS_PUBLIC_API
bool ToJSONMaybeSafely(JSContext
* cx
,
61 JS::Handle
<JSObject
*> input
,
62 JSONWriteCallback callback
,
68 * Performs the JSON.parse operation as specified by ECMAScript.
70 extern JS_PUBLIC_API
bool JS_ParseJSON(JSContext
* cx
, const char16_t
* chars
,
72 JS::MutableHandle
<JS::Value
> vp
);
75 * Performs the JSON.parse operation as specified by ECMAScript.
77 extern JS_PUBLIC_API
bool JS_ParseJSON(JSContext
* cx
, JS::Handle
<JSString
*> str
,
78 JS::MutableHandle
<JS::Value
> vp
);
81 * Performs the JSON.parse operation as specified by ECMAScript, using the
82 * given |reviver| argument as the corresponding optional argument to that
85 extern JS_PUBLIC_API
bool JS_ParseJSONWithReviver(
86 JSContext
* cx
, const char16_t
* chars
, uint32_t len
,
87 JS::Handle
<JS::Value
> reviver
, JS::MutableHandle
<JS::Value
> vp
);
90 * Performs the JSON.parse operation as specified by ECMAScript, using the
91 * given |reviver| argument as the corresponding optional argument to that
94 extern JS_PUBLIC_API
bool JS_ParseJSONWithReviver(
95 JSContext
* cx
, JS::Handle
<JSString
*> str
, JS::Handle
<JS::Value
> reviver
,
96 JS::MutableHandle
<JS::Value
> vp
);
98 #endif /* js_JSON_h */