JSON Utils #325

Parent #1Owner #361Flags readSource hellcore/hellcore.db

Aliases: JSON Utils

15 verbs · 10 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
parsethis none thisrxd#32519
parse_objectthis none thisrxd#32533
parse_arraythis none thisrxd#32524
parse_stringthis none thisrxd#32514
parse_booleanthis none thisrxd#32513
parse_nullthis none thisrxd#32511
parse_numberthis none thisrxd#32521
count_whitespacethis none thisrxd#32510
next_whitespacethis none thisrxd#3255
find_closing_quotethis none thisrxd#32518
next_non_number_charthis none thisrxd#32513
is_number_charthis none thisrxd#3259
writethis none thisrxd#32533
quotethis none thisrxd#3251
readthis none thisrxd#3252

Properties

PropertyDefinerFlagsOwnerValue
aliases#1rc#361{"JSON Utils"}
description#1rc#361<clear>
object_size#1r#29{9653, 1298434235}
hidden_verbs#1rc#361<clear>
phelp_msg#1rc#361<clear>
weight#1rc#361<clear>
owner_verbs#1rc#361<clear>
plural_name#1rc#361<clear>
client_image#1rc#361<clear>
listening#1rc#361<clear>

Ancestry

Ancestors (nearest first): #1 root

Children: none

Call graph

calls n325_0 #325:parse n325_7 #325:count_whitespace n325_0->n325_7 n325_1 #325:parse_object n325_0->n325_1 n325_2 #325:parse_array n325_0->n325_2 n325_3 #325:parse_string n325_0->n325_3 n20_14 #20:uppercase n325_0->n20_14 n325_4 #325:parse_boolean n325_0->n325_4 n325_5 #325:parse_null n325_0->n325_5 n325_6 #325:parse_number n325_0->n325_6 n325_1->n325_0 n325_1->n325_7 n325_1->n325_3 n325_2->n325_0 n325_2->n325_7 n325_3->n325_7 n325_9 #325:find_closing_quote n325_3->n325_9 n325_4->n325_7 n325_4->n20_14 n325_5->n325_7 n325_5->n20_14 n325_6->n325_7 n325_10 #325:next_non_number_char n325_6->n325_10 n325_11 #325:is_number_char n325_10->n325_11 n325_12 #325:write n325_12->n325_12 n325_13 #325:quote n325_12->n325_13 n325_14 #325:read n325_14->n325_0

Source

parse

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$json_utils:parse(STR json_text) => returns whatever object type the json_text describes and its length in the JSON text";
2if ((length(args) != 1) || typeof(args[1] != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if (json_text[cursor] == "{")
8return this:parse_object(json_text);
9elseif (json_text[cursor] == "[")
10return this:parse_array(json_text);
11elseif (json_text[cursor] == "\"")
12return this:parse_string(json_text);
13elseif (($su:lowercase(json_text[cursor]) == "t") || ($su:lowercase(json_text[cursor]) == "f"))
14return this:parse_boolean(json_text);
15elseif ($su:lowercase(json_text[cursor]) == "n")
16return this:parse_null(json_text);
17else
18return this:parse_number(json_text);
19endif

parse_object

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_object(STR json_text) => creates an object (hash) from the given text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if (json_text[cursor] != "{")
8raise(E_INVARG);
9else
10cursor = cursor + 1;
11endif
12object = [];
13"JSON Objects look like:";
14"  { \"name\" : value , \"name\" : value ... } ";
15while (json_text[cursor] != "}")
16"Pull out the name";
17{name, length} = this:parse_string(json_text[cursor..$]);
18cursor = cursor + length;
19cursor = cursor + this:count_whitespace(json_text[cursor..$]);
20if (json_text[cursor] != ":")
21raise(E_INVARG);
22else
23cursor = cursor + 1;
24endif
25{value, length} = this:parse(json_text[cursor..$]);
26cursor = cursor + length;
27cursor = cursor + this:count_whitespace(json_text[cursor..$]);
28if (json_text[cursor] == ",")
29cursor = cursor + 1;
30endif
31object[name] = value;
32endwhile
33return {object, cursor};

parse_array

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_array(STR json_text) => creates an array (list) from the given text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if (json_text[cursor] != "[")
8raise(E_INVARG);
9endif
10array = {};
11cursor = cursor + 1;
12"JSON Arrays look like:";
13"  [ value, value ... ] ";
14while (json_text[cursor] != "]")
15yield;
16{value, length} = this:parse(json_text[cursor..$]);
17cursor = cursor + length;
18cursor = cursor + this:count_whitespace(json_text[cursor..$]);
19if (json_text[cursor] == ",")
20cursor = cursor + 1;
21endif
22array = listappend(array, value);
23endwhile
24return {array, cursor};

parse_string

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_string(STR json_text) => return a string and its length in the JSON text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if (json_text[cursor] != "\"")
8raise(E_INVARG);
9else
10cursor = cursor + 1;
11endif
12closing_quote = this:find_closing_quote(json_text[cursor..$]);
13string = json_text[cursor..(cursor + closing_quote) - 2];
14return {string, (cursor + closing_quote) - 1};

parse_boolean

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_boolean(STR json_text) => return a boolean (1 or 0)  and its length in the JSON text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if ((length(json_text) >= (cursor + 3)) && ($su:lowercase(json_text[cursor..cursor + 3]) == "true"))
8return {1, cursor + 3};
9elseif ((length(json_text) >= (cursor + 4)) && ($su:lowercase(json_text[cursor..cursor + 4]) == "false"))
10return {0, cursor + 4};
11else
12raise(E_INVARG);
13endif

parse_null

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_null(STR json_text) => return a null (empty string, chosen somewhat arbitrarily)  and its length in the JSON text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7if ((length(json_text) >= (cursor + 3)) && ($su:lowercase(json_text[cursor..cursor + 3]) == "null"))
8return {"", cursor + 3};
9else
10raise(E_INVARG);
11endif

parse_number

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$ju:parse_number(STR json_text) => return a number and its length in the JSON text";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = this:count_whitespace(json_text) + 1;
7"Find where this number ends";
8next_nonnumber = this:next_non_number_char(json_text[cursor..$]);
9if (!next_nonnumber)
10next_nonnumber = length(json_text[cursor..$]) + 1;
11endif
12border = (cursor + next_nonnumber) - 2;
13num_str = json_text[cursor..border];
14"Check to see what kind of number it is";
15if ((index(num_str, "e") || index(num_str, "E")) || index(num_str, "."))
16"It's a real number";
17return {tofloat(num_str), border};
18else
19"It's an integer";
20return {toint(num_str), border};
21endif

count_whitespace

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$json_utils:count_whitespace(STR json_text) => return the number of whitespaces at the beginning of the string";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = 1;
7while ((cursor <= length(json_text)) && (json_text[cursor] == " "))
8cursor = cursor + 1;
9endwhile
10return cursor - 1;

next_whitespace

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

none

Source

1"$json_utils:next_whitespace(STR json_str) => returns the index of the first whitespace we find";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5return index(args[1], " ");

find_closing_quote

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$json_utils:find_closing_quote(STR json_str) => returns the index of the quote that closes the JSON string, or 0 if it's not found";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_str = args[1];
6cursor = 1;
7while (cursor <= length(json_str))
8quote = index(json_str[cursor..$], "\"");
9if (quote)
10"See if the quote has a backslash... if it doesn't it's not real";
11if ((quote == 1) || (json_str[quote - 1] != "\\"))
12return quote;
13endif
14cursor = cursor + quote;
15else
16return 0;
17endif
18endwhile

next_non_number_char

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$json_utils:next_whitespace(STR json_str) => returns the index of the first whitespace we find";
2if ((length(args) != 1) || (typeof(args[1]) != STR))
3raise(E_INVARG);
4endif
5json_text = args[1];
6cursor = 1;
7while (cursor <= length(json_text))
8if (!this:is_number_char(json_text[cursor]))
9return cursor;
10endif
11cursor = cursor + 1;
12endwhile
13return 0;

is_number_char

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1if (((length(args) != 1) || (typeof(args[1]) != STR)) || (length(args[1]) != 1))
2raise(E_INVARG);
3endif
4digit = args[1][1];
5if (((((((((((((digit == "0") || (digit == "1")) || (digit == "2")) || (digit == "3")) || (digit == "4")) || (digit == "5")) || (digit == "6")) || (digit == "7")) || (digit == "8")) || (digit == "9")) || (digit == ".")) || (digit == "e")) || (digit == "E"))
6return 1;
7else
8return 0;
9endif

write

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1pip = args[1];
2wrote_first_entry = 0;
3if (typeof(pip) == STR)
4return this:quote(pip);
5elseif ((typeof(pip) == INT) || (typeof(pip) == FLOAT))
6return tostr(pip);
7elseif (typeof(pip) == HASH)
8output = "{";
9for entry in (pip)
10{key, value} = entry;
11if (wrote_first_entry)
12output = output + ",";
13endif
14output = ((output + this:quote(key)) + ":") + this:write(value);
15endfor
16return output + "}";
17elseif (typeof(pip) == LIST)
18output = "[";
19for entry in (pip)
20if (wrote_first_entry)
21output = output + ",";
22endif
23output = output + this:write(entry);
24wrote_first_entry = 1;
25endfor
26return output + "]";
27elseif (typeof(pip) == OBJ)
28"This should probably dump all verbs, flags, and props";
29"Right now objects are just converted to strings...";
30return this:quote(tostr(pip));
31else
32raise(E_INVARG);
33endif

quote

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1return ("\"" + $su:substitute(args[1], {{"\"", "\\\""}})) + "\"";

read

Spec this none thisFlags rxdOwner #361Definer #325

Referenced by

Source

1"$json_utils:read(json_text) => returns a data structure formed from the JSON text";
2return this:parse(args[1])[1];