Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $def maxstring 64000 2 /
- lvar filler_text
- $def filler filler_text @ not if "****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************" begin dup strcat dup strlen maxstring > if maxstring strcut pop break then repeat dup filler_text ! else filler_text @ then
- : object2JSON[ any:input -- array of strings ]
- input @ string? if input @ strlen maxstring -- > if maxstring "JSON: String exceeds %i char limit." fmtstring abort then then
- input @ string? if { "\"" input @ "\\\\" "\\" subst "\\\"" "\"" subst "\\/" "/" subst "\"" }cat 1 array_make exit then
- input @ int? if input @ intostr 1 array_make exit then
- input @ float? if input @ "%f" fmtstring 1 array_make exit then
- input @ dbref? if input @ "%d#" fmtstring 1 array_make exit then
- input @ lock? if input @ unparselock "\\\\" "\\" subst "\\%" "%" subst "%%%s%%" fmtstring 1 array_make exit then
- 0 array_make var! output
- input @ dictionary? if
- "{" output @ array_appenditem output !
- input @ foreach var! value var! key
- key @ object2JSON output @ dup array_last pop ++ rot array_insertrange output !
- ":" output @ array_appenditem output !
- value @ object2JSON output @ dup array_last pop ++ rot array_insertrange output !
- "," output @ array_appenditem output !
- repeat
- "}" output @ dup array_last pop array_setitem exit
- then
- input @ array? if
- "[" output @ array_appenditem output !
- input @ foreach var! value pop
- value @ object2JSON output @ dup array_last pop ++ rot array_insertrange output !
- "," output @ array_appenditem output !
- repeat
- "]" output @ dup array_last pop array_setitem exit
- then
- "JSON: Error in object2JSON" abort
- ; PUBLIC object2JSON
- $libdef object2JSON
- : JSONmember[ str:input -- key value ]
- input @ 1 array_make "$lib/damagedJSON" match "JSON2object" call
- dup 0 array_getitem swap 1 array_getitem
- ; SAFECALL JSONmember
- : JSONelement[ str:input -- value ]
- input @ 1 array_make "$lib/damagedJSON" match "JSON2object" call
- 0 array_getitem
- ;
- : JSON2object[ array:JSONarray -- arr:data ]
- "" var! JSONstring
- 0 array_make var! output
- begin
- JSONstring @ not if
- JSONarray @ 0 array_getitem JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- then
- JSONstring @ case
- "{" instr 1 = when
- JSONstring @ 1 strcut nip JSONstring !
- 0 array_make_dict
- begin
- JSONstring @ "\"([^\"\\\\]*|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9a-f]{4})*\"" "" REG_ALL regsub dup dup "," instr ?dup not if 99999999 then swap "\"" instr ?dup not if 9999999 then < swap dup "," instr ?dup not if 99999999 then swap "}" instr ?dup not if 9999999 then < and if
- JSONstring @ dup
- begin
- dup "\"([^\"]*)\"" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\[([^\\[\\]]*)\\]" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\{([^\\{\\}]*)\\}" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- 1 until
- "^(\\[[^\\]]*\\]|\\{[^\\}]*\\}|\"[^\"]*\"|[^,])*" REG_ALL regexp nip 0 array_getitem 1 array_getitem strcut 1 strcut nip JSONstring ! JSONmember rot rot array_setitem
- continue
- else
- JSONstring @ "\"([^\"\\\\]*|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9a-f]{4})*\"" "" REG_ALL regsub dup "}" instr ?dup not if 99999999 then swap "\"" instr ?dup not if 9999999 then < if
- JSONstring @ dup
- begin
- dup "\"([^\"]*)\"" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\[([^\\[\\]]*)\\]" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\{([^\\{\\}]*)\\}" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- 1 until
- "^(\\[[^\\]]*\\]|\\{[^\\}]*\\}|\"[^\"]*\"|[^\\}])*" REG_ALL regexp nip 0 array_getitem 1 array_getitem strcut 1 strcut nip JSONstring ! JSONmember rot rot array_setitem
- output @ array_appenditem output !
- break
- then
- then
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- repeat
- end
- "[" instr 1 = when
- JSONstring @ 1 strcut nip JSONstring !
- 0 array_make
- begin
- JSONstring @ "\"([^\"\\\\]*|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9a-f]{4})*\"" "" REG_ALL regsub dup dup "," instr ?dup not if 99999999 then swap "\"" instr ?dup not if 9999999 then < swap dup "," instr ?dup not if 99999999 then swap "]" instr ?dup not if 9999999 then < and if
- JSONstring @ dup
- begin
- dup "\"([^\"]*)\"" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\[([^\\[\\]]*)\\]" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\{([^\\{\\}]*)\\}" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- 1 until
- "^(\\[[^\\]]*\\]|\\{[^\\}]*\\}|\"[^\"]*\"|[^,])*" REG_ALL regexp nip 0 array_getitem 1 array_getitem strcut 1 strcut nip JSONstring ! JSONelement swap array_appenditem
- continue
- else
- JSONstring @ "\"([^\"\\\\]*|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9a-f]{4})*\"" "" REG_ALL regsub dup "]" instr ?dup not if 99999999 then swap "\"" instr ?dup not if 9999999 then < if
- JSONstring @ dup
- begin
- dup "\"([^\"]*)\"" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\[([^\\[\\]]*)\\]" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- dup "\\{([^\\{\\}]*)\\}" REG_ALL regexp var! x 0 array_getitem if x @ 0 array_getitem 1 array_getitem filler swap strcut pop swap x @ 0 array_getitem 0 array_getitem -- strcut x @ 0 array_getitem 1 array_getitem strcut nip rot swap strcat strcat continue then
- 1 until
- "^(\\[[^\\]]*\\]|\\{[^\\}]*\\}|\"[^\"]*\"|[^\\]])*" REG_ALL regexp nip 0 array_getitem 1 array_getitem strcut 1 strcut nip JSONstring ! JSONelement swap array_appenditem
- output @ array_appenditem output !
- break
- then
- then
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- repeat
- end
- "\"" instr 1 = when
- begin
- JSONstring @ "\"([^\"\\\\]*|\\\\[\"\\\\\\/bfnrt]|\\\\u[0-9a-f]{4})*\"" REG_ICASE regexp 0 array_getitem swap not if
- pop
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- continue
- then
- JSONstring @ swap 1 array_getitem strcut JSONstring ! 1 strcut nip "\"" rsplit pop
- "\"" "\\\"" subst "/" "\\/" subst "\\" "\\\\" subst
- output @ array_appenditem output !
- 1 until
- end
- "#" instr 1 = when
- begin
- JSONstring @ "#[0-9]*#" REG_ICASE regexp 0 array_getitem swap not if
- pop
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- continue
- then
- JSONstring @ swap 1 array_getitem strcut JSONstring ! 1 strcut nip "#" rsplit pop stod output @ array_appenditem output !
- 1 until
- end
- "%" instr 1 = when
- begin
- JSONstring @ "%([^\\\\%]*|\\\\[\\\\%])*%" REG_ICASE regexp 0 array_getitem swap not if
- pop
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- continue
- then
- JSONstring @ swap 1 array_getitem strcut JSONstring ! 1 strcut nip "#" rsplit pop
- "%" "\\%" subst "\\" "\\\\" subst parselock
- output @ array_appenditem output !
- 1 until
- end
- ":" instr 1 = when
- JSONstring @ 1 strcut nip JSONstring !
- end
- dup "true" instring 1 = swap "false" instring 1 = or when
- begin
- JSONstring @ "true|false" REG_ICASE regexp swap not if
- pop
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- continue
- then
- 0 array_getitem JSONstring @ swap 1 array_getitem strcut JSONstring ! "true" stringcmp not if 1 else 0 then output @ array_appenditem output !
- 1 until
- end
- pop 1 when
- begin
- JSONstring @ "-?(?=[1-9]|0(?!\\d))\\d+(\\.\\d+)?([eE][+-]?\\d+)?" REG_ICASE regexp swap not if
- pop
- JSONstring @ JSONarray @ 0 array_getitem ?dup not if "" then strcat JSONstring !
- JSONarray @ 0 array_delitem JSONarray !
- continue
- then
- 0 array_getitem JSONstring @ swap 1 array_getitem strcut JSONstring ! dup var! number
- dup atoi swap strtof floor int = if
- number @ atoi
- else
- number @ strtof
- then
- output @ array_appenditem output !
- 1 until
- end
- endcase
- JSONarray @ not JSONstring @ not and until
- output @
- ; PUBLIC JSON2object
- $libdef JSON2object
Advertisement
Add Comment
Please, Sign In to add comment