MCPcopy Create free account
hub / github.com/mozilla/rhino / NativeJSON

Class NativeJSON

rhino/src/main/java/org/mozilla/javascript/NativeJSON.java:30–606  ·  view source on GitHub ↗

This class implements the JSON native object. See ECMA 15.12. @author Matthew Crumley, Raphael Speyer

Source from the content-addressed store, hash-verified

28 * @author Matthew Crumley, Raphael Speyer
29 */
30public final class NativeJSON extends ScriptableObject {
31 @Serial private static final long serialVersionUID = -4567599697595654984L;
32
33 private static final String JSON_TAG = "JSON";
34
35 private static final int MAX_STRINGIFY_GAP_LENGTH = 10;
36
37 private static final ClassDescriptor DESCRIPTION;
38
39 static {
40 DESCRIPTION =
41 new ClassDescriptor.Builder(JSON_TAG)
42 .withMethod(CTOR, "parse", 2, NativeJSON::parse)
43 .withMethod(CTOR, "stringify", 3, NativeJSON::stringify)
44 .withProp(CTOR, "toSource", value("JSON"))
45 .withProp(
46 CTOR, SymbolKey.TO_STRING_TAG, value(JSON_TAG, DONTENUM | READONLY))
47 .build();
48 }
49
50 static Object init(Context cx, VarScope scope, boolean sealed) {
51 return DESCRIPTION.populateGlobal(cx, scope, new NativeJSON(), sealed);
52 }
53
54 private NativeJSON() {}
55
56 @Override
57 public String getClassName() {
58 return "JSON";
59 }
60
61 private static Object parse(
62 Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
63 String jtext = ScriptRuntime.toString(args, 0);
64 Object reviver = null;
65 if (args.length > 1) {
66 reviver = args[1];
67 }
68 if (reviver instanceof Callable) {
69 return parse(cx, f.getDeclarationScope(), jtext, (Callable) reviver);
70 }
71 return parse(cx, f.getDeclarationScope(), jtext);
72 }
73
74 private static Object stringify(
75 Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) {
76 Object value = Undefined.instance, replacer = null, space = null;
77
78 if (args.length > 0) {
79 value = args[0];
80 if (args.length > 1) {
81 replacer = args[1];
82 if (args.length > 2) {
83 space = args[2];
84 }
85 }
86 }
87 return stringify(cx, s, value, replacer, space);

Callers

nothing calls this directly

Calls 4

withPropMethod · 0.80
withMethodMethod · 0.80
buildMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected