-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.singkong
100 lines (86 loc) · 2.03 KB
/
json.singkong
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#
JSON
json.singkong
Part of Singkong Programming Language Interpreter
(c) Noprianto <[email protected]>
More information: https://nopri.github.io/singkong.html
Download Singkong: https://nopri.github.io/Singkong.jar
(License: Free to use or redistribute, no warranty)
To use this module:
load_module("json")
;
require(9.4)
var json_parse_ = fn(s, m)
"
[json] function: json_parse_: returns ARRAY, BOOLEAN, HASH, NULL, NUMBER, STRING from JSON STRING.
arguments: 2: STRING (JSON) and NUMBER (mode)
return value: ARRAY, BOOLEAN, HASH, NULL, NUMBER, STRING or empty STRING (error)
"
{
if (!is(s, "STRING")) {
return ""
}
if (!is(m, "NUMBER")) {
return ""
}
var s = trim(s)
#NULL;
if (s == "null") {
return null
}
#BOOLEAN;
if (s == "true") {
return true
}
if (s == "false") {
return false
}
#NUMBER;
var n = number(s, 0, true)
if (is(n, "NUMBER")) {
return n
}
#ARRAY;
if ( (startswith(s, "[") == true) & (endswith(s, "]") == true) ) {
var p = parse_array(s, true, m)
if (p == null) {
return ""
}
return p
}
#HASH;
if ( (startswith(s, "{") == true) & (endswith(s, "}") == true) ) {
var p = parse_hash(s, true, m)
if (p == null) {
return ""
}
return p
}
#STRING;
if ( (startswith(s, quote()) == true) & (endswith(s, quote()) == true) ) {
var p = parse_string(s)
if (p == null) {
return ""
}
return p
}
return ""
}
var json_parse = fn(s)
"
[json] function: json_parse: returns ARRAY, BOOLEAN, HASH, NULL, NUMBER, STRING from JSON STRING.
arguments: 1: STRING (JSON)
return value: ARRAY, BOOLEAN, HASH, NULL, NUMBER, STRING or empty STRING (error)
"
{
return json_parse_(s, 2)
}
var json_string = fn(a)
"
[json] function: json_string: returns JSON STRING.
arguments: 1: <any type>
return value: STRING
"
{
return string(a, true)
}