RosettaCodeData/Task/JSON/Tcl/json-2.tcl

43 lines
1.1 KiB
Tcl

package require Tcl 8.6
package require json::write
proc tcl2json value {
# Guess the type of the value; deep *UNSUPPORTED* magic!
regexp {^value is a (.*?) with a refcount} \
[::tcl::unsupported::representation $value] -> type
switch $type {
string {
return [json::write string $value]
}
dict {
return [json::write object {*}[
dict map {k v} $value {tcl2json $v}]]
}
list {
return [json::write array {*}[lmap v $value {tcl2json $v}]]
}
int - double {
return [expr {$value}]
}
booleanString {
return [expr {$value ? "true" : "false"}]
}
default {
# Some other type; do some guessing...
if {$value eq "null"} {
# Tcl has *no* null value at all; empty strings are semantically
# different and absent variables aren't values. So cheat!
return $value
} elseif {[string is integer -strict $value]} {
return [expr {$value}]
} elseif {[string is double -strict $value]} {
return [expr {$value}]
} elseif {[string is boolean -strict $value]} {
return [expr {$value ? "true" : "false"}]
}
return [json::write string $value]
}
}
}