X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=tcvJSON.tcl;h=f0e781adac84484e9470c6a2db4352948dfce8db;hb=0e93bc12741bd93c6e35d81da4a44b32f7a8cdf9;hp=a0edff4f56b6025c3a674579395e89bc13692981;hpb=867f23e8e79c53bdf7885f3588f2fdff6d6bb315;p=scpubgit%2FTenDotTcl.git diff --git a/tcvJSON.tcl b/tcvJSON.tcl index a0edff4..f0e781a 100755 --- a/tcvJSON.tcl +++ b/tcvJSON.tcl @@ -191,32 +191,39 @@ proc tcvJSON::unparse {thing args} { set type [lindex $thing 0] set indent 2 set indentIncr 2 + set nl "\n" if {[llength $args] > 0} { set indent [lindex $args 0] + if {[llength $args] > 1} { + set indentIncr [lindex $args 1] + } else { + set indentIncr $indent + } + if {$indent == 0} { set nl "" } } switch -- $type { {list} { - append output "\[\n[string repeat " " $indent]" + append output "\[$nl[string repeat " " $indent]" set tmp {} foreach element [lrange $thing 1 end] { - lappend tmp [unparse $element [expr {$indent + $indentIncr}]] + lappend tmp [unparse $element [expr {$indent + $indentIncr}] $indentIncr] } - append output [join $tmp ",\n[string repeat " " $indent]"] - append output "\n[string repeat " " [expr {$indent - $indentIncr}]]\]" + append output [join $tmp ",$nl[string repeat " " $indent]"] + append output "$nl[string repeat " " [expr {$indent - $indentIncr}]]\]" } {obj} { - append output "\{\n[string repeat " " $indent]" + append output "\{$nl[string repeat " " $indent]" set tmp {} for {set i 1} {$i < [llength $thing]} {incr i} { set key [lindex $thing $i] set value [lindex $thing [incr i]] - lappend tmp "\"$key\": [unparse $value [expr {$indent + $indentIncr}]]" + lappend tmp "\"$key\": [unparse $value [expr {$indent + $indentIncr}] $indentIncr]" } append output [join $tmp ",\n[string repeat " " $indent]"] - append output "\n[string repeat " " [expr {$indent - $indentIncr}]]\}" + append output "$nl[string repeat " " [expr {$indent - $indentIncr}]]\}" } {str} {