Tcl Library Source Code

View Ticket
Login
Ticket UUID: 29aa482121acc35ee82ebab543cac56cff97af96
Title: JSON module fails to parse data
Type: Bug Version: 1.3.3
Submitter: arjenmarkus Created on: 2015-05-13 11:01:12
Subsystem: json Assigned To:
Priority: 5 Medium Severity: Critical
Status: Open Last Modified: 2015-05-21 04:52:21
Resolution: None Closed By: nobody
    Closed on:
Description:
The following code produces a dict consisting only of the key "message":

package require json
set data { \
  "message": "success", \
  "number": 6, \
  "people": [ \
    {
      "craft": "ISS", \
      "name": "Anton Schkaplerov"\
    }, \
    { \
      "craft": "ISS", \
      "name": "Samantha Cristoforetti"\
    }, \
    { \
      "craft": "ISS", \
      "name": "Terry Virts"\
    }, \
    { \
      "craft": "ISS", \
      "name": "Gennady Padalka"\
    }, \
    { \
      "craft": "ISS", \
      "name": "Mikhail Kornienko"\
    }, \
    { \
      "craft": "ISS", \
      "name": "Scott Kelly"\
    } \
  ] \
}

set d [::json::json2dict $data]
puts $d
puts [dict keys $d]
User Comments: anonymous (claiming to be aspect) added on 2015-05-21 04:52:21:
JSON syntax is described at http://json.org/ - {} denote an object (dict), [] an array (list) and so forth.

With the caveat that I don't know how to load the critcl implementation of json on my system, so am unable to test it ... (aku's input would help here!)

I suggest the following change to modules/json/json.man, which seems to accurately reflect the module's (Tcl implementation) behaviour.


A "-strict" option which throws an error if the complete input is not consumed seems like it might also be a good idea, but that's a non-trivial change from my position (quite unfamiliar with the module myself!).  Since most users will be consuming a complete JSON object (delimited with {}, so an incomplete/truncated input will error), this is probably not a high-priority change.


--- json.man    2014-10-08 00:33:09.154819953 +1100
+++ json.man    2015-05-21 14:33:55.475753629 +1000
@@ -33,8 +33,8 @@
 Parse JSON formatted text [arg txt] into a Tcl dict and return
 the value.
 
-[para] If [arg txt] contains more than one JSON entity only the
-first one is returned.
+[para] The first complete JSON entity in [arg txt] will be returned, and any
+trailing text ignored.
 
 
 [call [cmd ::json::many-json2dict] [arg txt] [opt [arg max]]]
@@ -47,6 +47,9 @@
 limits. A value of "[arg max] == 0" does not make sense and will cause the
 command to throw an error.
 
+[para] Extra text at the end of [arg txt] which is not formatted as correct
+JSON will be ignored.
+
 [list_end]
 [para]

arjenmarkus added on 2015-05-18 07:02:46:
Either documented or there should be an error message stating that it is not a well-formed JSON string. I am not familiar enough with JSON, so I am pretty ignorant about the proper syntax. Also the fact that JSON requires {} _as a part of the string_ is a bit confusing to me, used as I am to Tcl that uses {} as delimiters.

anonymous (claiming to be aspect) added on 2015-05-16 03:17:08:
I don't believe this is a bug, as $data is not correctly as a json object:

% json::json2dict "{ $data }"

returns the expected response, with keys {message number people}.

without {}, json2dict parses the first quoted string as a json value and ignores the rest of its input.  This probably ought to be documented.