fix json arrays

This commit is contained in:
Daniella / Tove 2024-11-15 15:19:59 +01:00
parent c33b25f260
commit fd0aed81fd
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
2 changed files with 28 additions and 5 deletions

View file

@ -3,9 +3,25 @@ construct json namespace {
_StringyJSON
;
props-to-sjson { s | with spaces props this ;
props:last:get<1> dup null eq not if {
"\"" swap _str :replace<"\\" "\\\\">:replace<"\"" "\\\""> concat "\"" concat 2 stop
} pop
def value, comma
"," spaces if { " " concat } =comma
props:last:get<1> =value
value null eq not if {
value gettype "array" eq if {
"[" value
:iter
:map<| 0 swap properties this:props-to-sjson>
:join<comma> concat
"]" concat
3 stop
}
"\""
value _str :replace<"\\" "\\\\">:replace<"\"" "\\\""> concat
"\"" concat
2 stop
}
"{"
props
:iter
@ -15,9 +31,9 @@ construct json namespace {
"\""
key :replace<"\\" "\\\\">:replace<"\"" "\\\""> concat
"\":" concat spaces if { " " concat }
spaces value properties this:props-to-json concat
spaces value properties this:props-to-sjson concat
}>
:join<", "> concat
:join<comma> concat
"}" concat
}
}

View file

@ -6,6 +6,7 @@
"spl/time.spl" import
"spl/httpserver/base.spl" import
"spl/linkedlist.spl" import
"spl/json.spl" import
"SPL tester" =program-name
@ -329,6 +330,12 @@ func main { int | with args ;
^ok =>? ^error not if { println }
"" println
"json test" println
0 [ 0 1 2 "hi" ] properties json:props-to-sjson println