Repository navigation
Expand file tree
/
Copy pathjson.js
More file actions
81 lines (81 loc) · 3.06 KB
/
Copy pathjson.js
File metadata and controls
81 lines (81 loc) · 3.06 KB
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
import { PHPExtension } from "../PHPExtension.js";
export class JSONExtension extends PHPExtension {
name = "json";
onInit(engine) {
this.constants = {
json_error_none: 0,
json_error_depth: 1,
json_error_state_mismatch: 2,
json_error_ctrl_char: 3,
json_error_syntax: 4,
json_error_utf8: 5,
json_hex_tag: 1,
json_hex_amp: 2,
json_hex_apos: 4,
json_hex_quot: 8,
json_force_object: 16,
json_numeric_check: 32,
json_unescaped_slashes: 64,
json_pretty_print: 128,
json_unescaped_unicode: 256,
json_partial_output_on_error: 512,
json_preserve_zero_fraction: 1024,
json_throw_on_error: 4194304,
};
let lastError = 0;
let lastErrorMsg = "No error";
this.functions = {
json_encode: async (ctx, valueArg, flagsArg) => {
try {
const value = await valueArg?.get();
const flags = Number(await flagsArg?.get()) || 0;
lastError = 0;
lastErrorMsg = "No error";
const space = (flags & 128) ? 2 : undefined;
let res = JSON.stringify(value, null, space);
if (res === undefined)
return "null";
if (flags & 1) { // JSON_HEX_TAG
res = res.replace(/</g, "\\u003C").replace(/>/g, "\\u003E");
}
if (flags & 2) { // JSON_HEX_AMP
res = res.replace(/&/g, "\\u0026");
}
if (flags & 4) { // JSON_HEX_APOS
res = res.replace(/'/g, "\\u0027");
}
if (flags & 8) { // JSON_HEX_QUOT
res = res.replace(/"/g, "\\u0022");
}
if (flags & 64) { // JSON_UNESCAPED_SLASHES
res = res.replace(/\\\//g, "/");
}
return res;
}
catch (e) {
lastError = 4;
lastErrorMsg = e.message || "Syntax error";
return false;
}
},
json_decode: async (ctx, jsonStrArg, assocArg) => {
try {
const jsonStr = String(await jsonStrArg?.get() ?? "");
lastError = 0;
lastErrorMsg = "No error";
if (!jsonStr.trim())
return null;
return JSON.parse(jsonStr);
}
catch (e) {
lastError = 4;
lastErrorMsg = e.message || "Syntax error";
return null;
}
},
json_last_error: async (ctx) => lastError,
json_last_error_msg: async (ctx) => lastErrorMsg,
};
}
}
//# sourceMappingURL=json.js.map