From fd0aed81fd46cc48ca14cf8ec87982c00c949266 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Fri, 15 Nov 2024 15:19:59 +0100 Subject: [PATCH] fix json arrays --- spl/json.spl | 26 +++++++++++++++++++++----- test.spl | 7 +++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/spl/json.spl b/spl/json.spl index b49f5c7..6d261fd 100644 --- a/spl/json.spl +++ b/spl/json.spl @@ -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 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 concat "}" concat } } diff --git a/test.spl b/test.spl index 6a0d2b8..5a258b3 100644 --- a/test.spl +++ b/test.spl @@ -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 + +