From: Matt S Trout Date: Sun, 17 Jun 2012 23:28:46 +0000 (+0100) Subject: 0 indent means don't bother with newlines either X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e0b6d5f7724f0e3bf9fa3cb3e9c55a05ee62a6d;p=scpubgit%2FTenDotTcl.git 0 indent means don't bother with newlines either --- diff --git a/tcvJSON.tcl b/tcvJSON.tcl index 7b6d382..f0e781a 100755 --- a/tcvJSON.tcl +++ b/tcvJSON.tcl @@ -191,6 +191,7 @@ 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] @@ -199,21 +200,22 @@ proc tcvJSON::unparse {thing args} { } 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}] $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] @@ -221,7 +223,7 @@ proc tcvJSON::unparse {thing args} { 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} {