Tcl Library Source Code

View Ticket
Login
Ticket UUID: 52b764e4dd9a4e9b939bc228379e47fbc758ca3a
Title: json2dict incorrectly adds braces to strings starting with $
Type: Bug Version: json::write 1.0.3, json 1.3.3
Submitter: anonymous Created on: 2015-01-22 17:02:43
Subsystem: json Assigned To:
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2015-02-11 13:55:43
Resolution: None Closed By: nobody
    Closed on:
Description:
json2dict turns an array into a nested list when one item starts with $. $ should not have special meaning in JSON, so I am not sure what happens here. Steps to reproduce:

30 % set var [json::write string \$::x ]
"$::x"
31 % set json [json::write array \"length\" $var]
["length","$::x"]
32 % set dict [json::json2dict $json]
length {$::x}
User Comments: anonymous added on 2015-02-11 13:55:43:
Oops, my bad, I haven't noticed that there was an array and not an object. In this case json::json2dict happens to return a list, not a dictionary. This behavior is reasonable but undocumented, which can be considered a bug.

As for your examples with [expr], it's just the way lists or dicts work. Their string representations are guarded to make absolutely sure you can extract their items in exactly the same form they were added. So, once more: use string representation of lists or dictionaries only if you know exactly what you're doing. Otherwise it's safer to extract individual items using [lindex] or to iterate using [foreach], or join them into a string via [join].

anonymous added on 2015-02-10 09:09:34:
The point is that the original json was an array, not an object containing key:value pairs. "length" is not a key and could be anything. The problem becomes crucial if you do function calls via JSON, and the array is the list of parameters which can normally be given as is to a tcl proc or command

% set ::x 5
5
% set d {1 + \$::x}
1 + $::x
expr $d
6

works perfectly fine. but:

% set json [json::write array 1 \"+\" $var]
[1,"+","$::x"]
% set a [json::json2dict $json]
1 + {$::x}
% expr $a
can't use non-numeric string as operand of "+"

This being Tcl, I arguably should be able to use the string representation whenever I want to. There is just no good reason for this to be formatted as a list with a single member. Try it without the $ and it will be just fine.

anonymous added on 2015-02-10 05:42:25:
The json::json2dict command returns dictionary or set of key-value pairs. What you see in the command output is its string representation. You're not supposed to use the string representation unless you know what you're doing. Just execute

% dict get $dict length
$::x

and you'll get your initial string.

This doesn't look like a bug to me.