Tk Library Source Code

View Ticket
Login
Ticket UUID: 2909962
Title: Faster, more readable implementation of json parser
Type: Patch Version: None
Submitter: thomasmaeder Created on: 2009-12-07 11:22:16
Subsystem: json Assigned To: andreas_kupries
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2009-12-11 00:51:14
Resolution: Accepted Closed By: andreas_kupries
    Closed on: 2009-12-10 17:51:14
Description:
When parsing documents of some nontrivial size (i.e. >100kB), I have found the json parser package 1.0 to be far too slow.

Here is a considerably faster implementation (I have measured speed gains of a factor of 30). The main difference is that a regular expression is used for tokenizing the json text.

This means that the parsing process restrict itself to a sequence of recognized tokens; it is blind to non-whitespace text between recognized tokens. I have therefore added a proc json::validate which can be used to test the validity of the entire text.

I have not touched the *2json procs.

The code is commented according to our internal guidelines; adaptations to the tcllib guidelines may thus have to be made if it is to be published in a future tcllib release.

I haven't changed the package version number, and my CVS has tampered with the CVS mark in the header.
User Comments: andreas_kupries added on 2009-12-11 00:51:14:

allow_comments - 1

The revised json.tcl passes the test suite. Committed to head with slight modifications (changed a few '==' to 'eq' because they were string, well character, comparisons, and 'eq' is better for that, as it doesn't check if the arguments are numeric or not, as '==' does).

The new version number is 1.1.

thomasmaeder added on 2009-12-10 15:49:07:
Thanks for testing! I had forgotten the 'u' in Unicode character literals ('\u6021').

I have just (2009-12-10 09:48) attached the fixed json.tcl

thomasmaeder added on 2009-12-10 15:48:22:

File Added - 354630: json.tcl

andreas_kupries added on 2009-12-09 01:36:17:
Thanks Thomas.

I have now run this code against the json.test testsuite in Tcllib, and it reports one failure, for the 'menu' test case. The others all pass.

The JSON it fails on is

{"menu": {
    "id": "file",
    "value": "File:",
    "unival": "\u6021:",
    "popup": {
        "menuitem": [
                     {"value": "Open", "onclick": "OpenDoc()"},
                     {"value": "Close", "onclick": "CloseDoc()"}
                    ]
    }
}
}

See the attached file X.log for the stacktrace and error message. It basically expected ',' or '}', and got a ':'. I do not know if the test input is wrong, or your code missing something.

Please check.

andreas_kupries added on 2009-12-09 01:33:24:

File Added - 354388: json.test

andreas_kupries added on 2009-12-09 01:32:22:

File Added - 354387: X.log

thomasmaeder added on 2009-12-07 18:22:16:

File Added - 354197: json.tcl

Attachments: