Bug 1744318 [wpt PR 31881] - Add a test for https://crrev.com/c/3311271, a=testonly
[gecko.git] / js / public / JSON.h
blob1d3eae99e8f32c821e9eec18fde3d158bd82b957
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/. */
6 /*
7 * JSON serialization and deserialization operations.
8 */
10 #ifndef js_JSON_h
11 #define js_JSON_h
13 #include <stdint.h> // uint32_t
15 #include "jstypes.h" // JS_PUBLIC_API
17 #include "js/TypeDecls.h"
19 using JSONWriteCallback = bool (*)(const char16_t* buf, uint32_t len,
20 void* data);
22 /**
23 * Performs the JSON.stringify operation, as specified by ECMAScript, except
24 * writing stringified data by repeated calls of |callback|, with each such
25 * call passed |data| as argument.
27 extern JS_PUBLIC_API bool JS_Stringify(JSContext* cx,
28 JS::MutableHandle<JS::Value> value,
29 JS::Handle<JSObject*> replacer,
30 JS::Handle<JS::Value> space,
31 JSONWriteCallback callback, void* data);
33 namespace JS {
35 /**
36 * An API akin to JS_Stringify but with the goal of not having observable
37 * side-effects when the stringification is performed. This means it does not
38 * allow a replacer or a custom space and has the following constraints on its
39 * input:
41 * 1) The input must be a plain object or array, not an abitrary value.
42 * 2) Every value in the graph reached by the algorithm starting with this
43 * object must be one of the following: null, undefined, a string (NOT a
44 * string object!), a boolean, a finite number (i.e. no NaN or Infinity or
45 * -Infinity), a plain object with no accessor properties, or an Array with
46 * no holes.
48 * The actual behavior differs from JS_Stringify only in asserting the above and
49 * NOT attempting to get the "toJSON" property from things, since that could
50 * clearly have side-effects.
52 extern JS_PUBLIC_API bool ToJSONMaybeSafely(JSContext* cx,
53 JS::Handle<JSObject*> input,
54 JSONWriteCallback callback,
55 void* data);
57 } /* namespace JS */
59 /**
60 * Performs the JSON.parse operation as specified by ECMAScript.
62 extern JS_PUBLIC_API bool JS_ParseJSON(JSContext* cx, const char16_t* chars,
63 uint32_t len,
64 JS::MutableHandle<JS::Value> vp);
66 /**
67 * Performs the JSON.parse operation as specified by ECMAScript.
69 extern JS_PUBLIC_API bool JS_ParseJSON(JSContext* cx, JS::Handle<JSString*> str,
70 JS::MutableHandle<JS::Value> vp);
72 /**
73 * Performs the JSON.parse operation as specified by ECMAScript, using the
74 * given |reviver| argument as the corresponding optional argument to that
75 * function.
77 extern JS_PUBLIC_API bool JS_ParseJSONWithReviver(
78 JSContext* cx, const char16_t* chars, uint32_t len,
79 JS::Handle<JS::Value> reviver, JS::MutableHandle<JS::Value> vp);
81 /**
82 * Performs the JSON.parse operation as specified by ECMAScript, using the
83 * given |reviver| argument as the corresponding optional argument to that
84 * function.
86 extern JS_PUBLIC_API bool JS_ParseJSONWithReviver(
87 JSContext* cx, JS::Handle<JSString*> str, JS::Handle<JS::Value> reviver,
88 JS::MutableHandle<JS::Value> vp);
90 #endif /* js_JSON_h */