Index: openacs-4/packages/acs-tcl/tcl/json-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/json-procs.tcl,v diff -u -r1.11.2.5 -r1.11.2.6 --- openacs-4/packages/acs-tcl/tcl/json-procs.tcl 24 Jul 2024 08:05:05 -0000 1.11.2.5 +++ openacs-4/packages/acs-tcl/tcl/json-procs.tcl 25 Jul 2024 09:19:52 -0000 1.11.2.6 @@ -653,33 +653,41 @@ return [join $output \n] } +ad_proc util::tdomDoc2dict {doc} { + + Helper proc for util::json2dict, which outputsreturns the provided + tDOM document in the form of a Tcl dict. + +} { + return [util::tdomNodes2dict [$doc childNodes] [$doc jsonType]] +} -ad_proc -private util::tdomNodes2dict { nodes } { - Helper proc for util::json2dict, which outputs the tDOM structure +ad_proc -private util::tdomNodes2dict { nodes parentType } { + + Helper proc for util::json2dict, which returns the tDOM structure in the form of a Tcl dict. Use this proc only on dom structures created with "porse -json", since it depends on the internal node structure of tDOM. It would be certainly better to have this function built-in in tDOM (call - like "asDict", similar to "asXML") + like "asDict", similar to "asXML") @return dict @author Gustaf Neumann } { set result "" foreach n $nodes { set children [$n childNodes] - #puts "tdomNodes2dict $n [$n jsonType] [$n nodeName] <[$n nodeValue]> #children [llength $children]" - set childrendValue [tdomNodes2dict $children] + set jsonType [$n jsonType] + set childrendValue [util::tdomNodes2dict $children $jsonType] - switch [$n jsonType] { + switch $jsonType { OBJECT { - if {[$n nodeName] ne "objectcontainer"} { - lappend result [$n nodeName] $childrendValue - } else { - lappend result $childrendValue + if {[$n nodeName] ne "objectcontainer" || $parentType eq "OBJECT"} { + lappend result [$n nodeName] } + lappend result $childrendValue } NONE { lappend result [$n nodeName] $childrendValue @@ -697,7 +705,6 @@ } } } - #puts "tdomNodes2dict returns <$result>" return $result } @@ -715,7 +722,7 @@ @author Gustaf Neumann } { #ns_log notice "PARSE\n$jsonText" - return [util::tdomNodes2dict [[dom parse -json $jsonText] childNodes]] + return [util::tdomDoc2dict [dom parse -json $jsonText]] } # Local variables: Index: openacs-4/packages/acs-tcl/tcl/test/json-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/test/json-test-procs.tcl,v diff -u -r1.1.2.3 -r1.1.2.4 --- openacs-4/packages/acs-tcl/tcl/test/json-test-procs.tcl 24 Jul 2024 08:05:05 -0000 1.1.2.3 +++ openacs-4/packages/acs-tcl/tcl/test/json-test-procs.tcl 25 Jul 2024 09:19:52 -0000 1.1.2.4 @@ -38,4 +38,12 @@ }} set jsonDict [util::json2dict $json] aa_equals with-array-container $jsonDict {Titel Wirtschaftsinformatik Schlagworte {{hello world} Einführung}} + + set json {{ + "Titel": "Wirtschaftsinformatik", + "nested": {"a":1, "b":{"o1":1,"o2":2}, "objectcontainer": {"o3":3,"o4":4}}, + "objectcontainer": {"a":"b","c":"d"} + }} + set jsonDict [util::json2dict $json] + aa_equals literal-objectcontainer $jsonDict {Titel Wirtschaftsinformatik nested {a 1 b {o1 1 o2 2} objectcontainer {o3 3 o4 4}} objectcontainer {a b c d}} }