Posted to tcl by Dereckson at Mon Oct 16 10:20:39 GMT 2017view raw
- # Converts a key value key value ... list into a JSON object
- proc kvlist2json {listToEncode} {
- set objectToEncode {}
- foreach {k v} $listToEncode {
- lappend objectToEncode $k
- lappend objectToEncode [::json::write string $v]
- }
- ::json::write object {*}$objectToEncode
- }
Comments
Posted by Dereckson at Mon Oct 16 14:17:02 GMT 2017 [text] [code]
As this data structure is actually a dictionary, this code could be simplified though `dict map` use: proc dict2json {dictToEncode} { ::json::write object {*}[dict map {k v} $dictToEncode { set v [::json::write string $v] }] } It should be noted it considers every value are strings, which is not something wanted in most case, where null/true/false/numeric values should be left unquoted.