json tcl_确定TCL中变量的类型
Tcl的變量沒有類型(除了他們是否真的變量的關聯數組 - 即使用$foo(bar)語法 - FO r你使用array exists)但Tcl的值。好吧,有點。 Tcl可以在不同類型之間進行變異,因為它認為合適,并且不公開這些信息[*];你所能做的就是檢查一個值是否符合特定的類型。
這種一致性檢查與完成string is(在您需要的-strict選項,丑陋的歷史原因):
if {[string is integer -strict $foo]} {
puts "$foo is an integer!"
}
if {[string is list $foo]} { # Only [string is] where -strict has no effect
puts "$foo is a list! (length: [llength $foo])"
if {[llength $foo]&1 == 0} {
# All dictionaries conform to lists with even length
puts "$foo is a dictionary! (entries: [dict size $foo])"
}
}
請注意,所有值符合字符串類型; Tcl的值是總是可序列化。對于JSON序列化,可以使用骯臟的黑客來產生一個“正確的”序列化(嚴格地說,從Tcl的角度來看,所有東西都是正確的,但這對其他語言并不完全有幫助)與Tcl 8.6。代碼要做到這一點,原本張貼在Rosetta Code是:
package require Tcl 8.6
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 {
# Skip to the mapping code at the bottom
}
dict {
set result "{"
set pfx ""
dict for {k v} $value {
append result $pfx [tcl2json $k] ": " [tcl2json $v]
set pfx ", "
}
return [append result "}"]
}
list {
set result "\["
set pfx ""
foreach v $value {
append result $pfx [tcl2json $v]
set pfx ", "
}
return [append result "\]"]
}
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"}]
}
}
}
# For simplicity, all "bad" characters are mapped to \u... substitutions
set mapped [subst -novariables [regsub -all {[][\u0000-\u001f\\""]} \
$value {[format "\\\\u%04x" [scan {& } %c]]}]]
return "\"$mapped\""
}
警告:不支持上面的代碼。這取決于骯臟的黑客。它很容易在沒有預警的情況下突破。 (但是它不工作。移植到Tcl的8.5將需要一個微小的C擴展到讀出的類型的注釋。)
[*]嚴格,但它用于發現當前類型注釋提供一個不支持的接口的價值為8.6 - 作為::tcl::unsupported::representation的一部分 - 但該信息采用刻意的人類可讀形式,如有更改,恕不另行通知。它用于調試,而不是代碼。而且,Tcl在內部使用相當多的不同類型(例如,,緩存的命令和變量名稱),在正常情況下你不想探測;事情是引擎蓋下相當復雜的...
總結
以上是生活随笔為你收集整理的json tcl_确定TCL中变量的类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kotlin AAPT: error:
- 下一篇: CAP 原则与 BASE 理论