JSON Utils #325
Aliases: JSON Utils
15 verbs · 10 properties · 0 children
Verbs
| Verb | Spec | Flags | Definer | Lines |
|---|---|---|---|---|
parse | this none this | rxd | #325 | 19 |
parse_object | this none this | rxd | #325 | 33 |
parse_array | this none this | rxd | #325 | 24 |
parse_string | this none this | rxd | #325 | 14 |
parse_boolean | this none this | rxd | #325 | 13 |
parse_null | this none this | rxd | #325 | 11 |
parse_number | this none this | rxd | #325 | 21 |
count_whitespace | this none this | rxd | #325 | 10 |
next_whitespace | this none this | rxd | #325 | 5 |
find_closing_quote | this none this | rxd | #325 | 18 |
next_non_number_char | this none this | rxd | #325 | 13 |
is_number_char | this none this | rxd | #325 | 9 |
write | this none this | rxd | #325 | 33 |
quote | this none this | rxd | #325 | 1 |
read | this none this | rxd | #325 | 2 |
Properties
| Property | Definer | Flags | Owner | Value |
|---|---|---|---|---|
aliases | #1 | rc | #361 | {"JSON Utils"} |
description | #1 | rc | #361 | <clear> |
object_size | #1 | r | #29 | {9653, 1298434235} |
hidden_verbs | #1 | rc | #361 | <clear> |
phelp_msg | #1 | rc | #361 | <clear> |
weight | #1 | rc | #361 | <clear> |
owner_verbs | #1 | rc | #361 | <clear> |
plural_name | #1 | rc | #361 | <clear> |
client_image | #1 | rc | #361 | <clear> |
listening | #1 | rc | #361 | <clear> |
Ancestry
Ancestors (nearest first): #1 root
Children: none
Call graph
Source
parse
Referenced by
- #325:parse_object line 25:
this:parse - #325:parse_array line 16:
this:parse - #325:read line 2:
this:parse
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
Referenced by
- #325:parse line 8:
this:parse_object
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
Referenced by
- #325:parse line 10:
this:parse_array
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
Referenced by
- #325:parse line 12:
this:parse_string - #325:parse_object line 17:
this:parse_string
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
Referenced by
- #325:parse line 14:
this:parse_boolean
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
Referenced by
- #325:parse line 16:
this:parse_null
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
Referenced by
- #325:parse line 18:
this:parse_number
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
Referenced by
- #325:parse line 6:
this:count_whitespace - #325:parse_object line 6:
this:count_whitespace - #325:parse_object line 19:
this:count_whitespace - #325:parse_object line 27:
this:count_whitespace - #325:parse_array line 6:
this:count_whitespace - #325:parse_array line 18:
this:count_whitespace - #325:parse_string line 6:
this:count_whitespace - #325:parse_boolean line 6:
this:count_whitespace - #325:parse_null line 6:
this:count_whitespace - #325:parse_number line 6:
this:count_whitespace
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
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
Referenced by
- #325:parse_string line 12:
this:find_closing_quote
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
Referenced by
- #325:parse_number line 8:
this:next_non_number_char
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
Referenced by
- #325:next_non_number_char line 8:
this:is_number_char
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
Referenced by
- #325:write line 14:
this:write - #325:write line 23:
this:write
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
Referenced by
- #325:write line 4:
this:quote - #325:write line 14:
this:quote - #325:write line 30:
this:quote
Source
1return ("\"" + $su:substitute(args[1], {{"\"", "\\\""}})) + "\"";
read
Referenced by
- #326:_www_post line 3:
$json_utils:read
Source
1"$json_utils:read(json_text) => returns a data structure formed from the JSON text"; 2return this:parse(args[1])[1];