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.13 -r1.14 --- openacs-4/packages/acs-tcl/tcl/json-procs.tcl 11 Sep 2024 06:15:48 -0000 1.13 +++ openacs-4/packages/acs-tcl/tcl/json-procs.tcl 22 Oct 2024 09:37:22 -0000 1.14 @@ -653,60 +653,64 @@ return [join $output \n] } -ad_proc util::tdomDoc2dict {doc} { +if {![::acs::icanuse "domDoc asTclValue"]} { - Helper proc for util::json2dict, which outputsreturns the provided - tDOM document in the form of a Tcl dict. + ad_proc -private util::tdomNodes2dict { nodes parentType } { -} { - return [util::tdomNodes2dict [$doc childNodes] [$doc jsonType]] -} + 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") -ad_proc -private util::tdomNodes2dict { nodes parentType } { + @return dict + @author Gustaf Neumann + } { + set result "" + foreach n $nodes { + set children [$n childNodes] + set jsonType [$n jsonType] + set childrendValue [util::tdomNodes2dict $children $jsonType] - 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") - - @return dict - @author Gustaf Neumann -} { - set result "" - foreach n $nodes { - set children [$n childNodes] - set jsonType [$n jsonType] - set childrendValue [util::tdomNodes2dict $children $jsonType] - - switch $jsonType { - OBJECT { - if {[$n nodeName] ne "objectcontainer" || $parentType eq "OBJECT"} { - lappend result [$n nodeName] + switch $jsonType { + OBJECT { + if {[$n nodeName] ne "objectcontainer" || $parentType eq "OBJECT"} { + lappend result [$n nodeName] + } + lappend result $childrendValue } - lappend result $childrendValue + NONE { + lappend result [$n nodeName] $childrendValue + } + ARRAY { + if {[$n nodeName] ne "arraycontainer" || $parentType eq "OBJECT"} { + lappend result [$n nodeName] + } + lappend result $childrendValue + } + default { + set op [expr {[llength $nodes] > 1 ? "lappend" : "set"} ] + $op result [$n nodeValue] + } } - NONE { - lappend result [$n nodeName] $childrendValue - } - ARRAY { - if {[$n nodeName] ne "arraycontainer" || $parentType eq "OBJECT"} { - lappend result [$n nodeName] - } - lappend result $childrendValue - } - default { - set op [expr {[llength $nodes] > 1 ? "lappend" : "set"} ] - $op result [$n nodeValue] - } } + return $result } - return $result } +ad_proc util::tdomDoc2dict {doc} { + + Helper proc for util::json2dict, which outputsreturns the provided + tDOM document in the form of a Tcl dict. + +} { + expr {[::acs::icanuse "domDoc asTclValue"] + ? [$doc asTclValue] + : [util::tdomNodes2dict [$doc childNodes] [$doc jsonType]] } +} + ad_proc util::json2dict { jsonText } { Parse JSON text into a Tcl dict. @@ -722,7 +726,9 @@ } { #ns_log notice "PARSE\n$jsonText" set doc [dom parse -json -- $jsonText] - set result [util::tdomDoc2dict $doc] + set result [expr {[::acs::icanuse "domDoc asTclValue"] + ? [$doc asTclValue] + : [util::tdomDoc2dict $doc]}] $doc delete return $result }