Tk Library Source Code

View Ticket
Login
Ticket UUID: 3508603
Title: json: pretty-printing a dictionary
Type: Patch Version: None
Submitter: aldobu Created on: 2012-03-19 11:40:11
Subsystem: json Assigned To: andreas_kupries
Priority: 5 Medium Severity: Minor
Status: Deleted Last Modified: 2013-05-31 19:22:08
Resolution: Not Applicable Here Closed By: aku
    Closed on: 2013-05-31 19:22:08
Description:
Based on json.tcl (1.1.2)
I added a new proc named "json2prettydict"

Aim of this proc is to return a "pretty-printed" tcl-dictionary.

I think this proc could be useful for documentating complex nested dictionaries.

- Internal Notes -
"json2prettydict" is heavily based on existing json2dict and json::parseXXX procs.
I slightly changed the internal code of all json::parseXXX procs, so that
these procs, may be called in two ways: for getting a (traditional) tcl-dict
or for getting a pretty-printed tcl-dict.

[Source code is attached]

-------------------------------------
Here is a demo:

set jsonStr { \
 { "photos": { "page": 1, "pages": "726", "perpage": 3, "total": "7257", 
    "photo": [
      { "id": "6974156079", "owner": "74957296@N08", "secret": "005d743f82", "server": "7197", "farm": 8, "title": "Kenya Watamu \"Deep Sea Fishing\" \"Indian Ocean\" \"Blue Marlin\"", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
      { "id": "6822988100", "owner": "52857411@N08", "secret": "56630c18e8", "server": "7183", "farm": 8, "title": "Gedi Ruins, Local Guide", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
      { "id": "6822909640", "owner": "52857411@N08", "secret": "f4e392ea36", "server": "7063", "farm": 8, "title": "Local Fisherman, Mida Creek", "ispublic": 1, "isfriend": 0, "isfamily": 0 }
    ] }, "stat": "ok" }
}
# First, just for comparision, call json2dict
set d1 [json::json2dict $jsonStr]
 # result is the following
if {0} {

photos {page 1 pages 726 perpage 3 total 7257 photo {{id 6974156079 owner 74957296@N08 secret 005d743f82 server 7197 farm 8 title {Kenya Watamu "Deep Sea Fishing" "Indian Ocean" "Blue Marlin"} ispublic 1 isfriend 0 isfamily 0} {id 6822988100 owner 52857411@N08 secret 56630c18e8 server 7183 farm 8 title {Gedi Ruins, Local Guide} ispublic 1 isfriend 0 isfamily 0} {id 6822909640 owner 52857411@N08 secret f4e392ea36 server 7063 farm 8 title {Local Fisherman, Mida Creek} ispublic 1 isfriend 0 isfamily 0}}} stat ok

}
 # Then create a pretty printed dictionary
set d2 [json::json2prettydict $jsonStr]
 # result is the following
if {0} {

photos {
page 1
pages 726
perpage 3
total 7257
photo {
{
id 6974156079
owner 74957296@N08
secret 005d743f82
server 7197
farm 8
title {Kenya Watamu "Deep Sea Fishing" "Indian Ocean" "Blue Marlin"}
ispublic 1
isfriend 0
isfamily 0
}
{
id 6822988100
owner 52857411@N08
secret 56630c18e8
server 7183
farm 8
title {Gedi Ruins, Local Guide}
ispublic 1
isfriend 0
isfamily 0
}
{
id 6822909640
owner 52857411@N08
secret f4e392ea36
server 7063
farm 8
title {Local Fisherman, Mida Creek}
ispublic 1
isfriend 0
isfamily 0
}
}

}
stat ok

}

# -- end
User Comments: aldobu added on 2012-03-19 19:05:46:
My apologies for my inexperience ...
The sample pretty-printed dictionary in the above "detail" field, lost all its pretty "tabs" ...

If you want to see the good "indented" sample tcl-dictionary, open the [patch-request.txt]  attached file.

aldobu added on 2012-03-19 19:00:00:

File Added - 438667: patch-request.txt

aldobu added on 2012-03-19 18:40:17:

File Added - 438666: exp_json.tcl

Attachments: