Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
5
Task/JSON/Tcl/json-1.tcl
Normal file
5
Task/JSON/Tcl/json-1.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package require json
|
||||
set sample {{ "foo": 1, "bar": [10, "apples"] }}
|
||||
|
||||
set parsed [json::json2dict $sample]
|
||||
puts $parsed
|
||||
42
Task/JSON/Tcl/json-2.tcl
Normal file
42
Task/JSON/Tcl/json-2.tcl
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Task/JSON/Tcl/json-3.tcl
Normal file
2
Task/JSON/Tcl/json-3.tcl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
set d [dict create blue [list 1 2] ocean water]
|
||||
puts [tcl2json $d]
|
||||
Loading…
Add table
Add a link
Reference in a new issue