Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Renamed createDocumentFromTypedList createFromTypedList - it is still a mouthful.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | asTypedList
Files: files | file ages | folders
SHA3-256: 6bc8b7435a74c8ab59b59b470b570f42211311e1e31696c1eca08e04f9676332
User & Date: rolf 2024-09-23 20:41:04
Context
2024-09-27
00:15
Improved error handling and error messages. check-in: cf19b79d68 user: rolf tags: asTypedList
2024-09-23
20:41
Renamed createDocumentFromTypedList createFromTypedList - it is still a mouthful. check-in: 6bc8b7435a user: rolf tags: asTypedList
18:49
Fixed syntax error. check-in: ba093cc945 user: rolf tags: asTypedList
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/dom.xml.

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
          <desc>Creates a new 'empty' DOM document object without any
          element node. <m>objVar</m> controls the memory handling as
          explained above. If the option -jsonType is given the
          created document node will be of the given JSON type.</desc>
        </commanddef>

        <commanddef>
        <command><cmd>dom</cmd> <method>createDocumentFromTypedList</method> <m>typedList</m> ?<m>objVar</m>?</command>
          <desc><p>Creates a new DOM document from the argument <m>typedList</m>.
          The <m>objVar</m> argument controls the memory handling as
          explained above.</p>

          <p>The <m>typedList</m> argument must be a Tcl list and must
          follow the format of the output of the document command
          method <m>asTypedList</m>, see there.</p></desc>







|







403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
          <desc>Creates a new 'empty' DOM document object without any
          element node. <m>objVar</m> controls the memory handling as
          explained above. If the option -jsonType is given the
          created document node will be of the given JSON type.</desc>
        </commanddef>

        <commanddef>
        <command><cmd>dom</cmd> <method>createFromTypedList</method> <m>typedList</m> ?<m>objVar</m>?</command>
          <desc><p>Creates a new DOM document from the argument <m>typedList</m>.
          The <m>objVar</m> argument controls the memory handling as
          explained above.</p>

          <p>The <m>typedList</m> argument must be a Tcl list and must
          follow the format of the output of the document command
          method <m>asTypedList</m>, see there.</p></desc>

Changes to generic/tcldom.c.

6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
    } else {
        doc = domCreateDocument (uri, Tcl_GetString(objv[2]));
    }
    return tcldom_returnDocumentObj (interp, doc, newObjName, 1, 0);
}

/*----------------------------------------------------------------------------
|   tcldom_createDocumentFromTypedList
|
\---------------------------------------------------------------------------*/
static
int tcldom_createDocumentFromTypedList (
    ClientData  UNUSED(clientData),
    Tcl_Interp *interp,
    int         objc,
    Tcl_Obj    * const objv[]
)
{
    domDocument *doc;







|



|







6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
    } else {
        doc = domCreateDocument (uri, Tcl_GetString(objv[2]));
    }
    return tcldom_returnDocumentObj (interp, doc, newObjName, 1, 0);
}

/*----------------------------------------------------------------------------
|   tcldom_createFromTypedList
|
\---------------------------------------------------------------------------*/
static
int tcldom_createFromTypedList (
    ClientData  UNUSED(clientData),
    Tcl_Interp *interp,
    int         objc,
    Tcl_Obj    * const objv[]
)
{
    domDocument *doc;
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
    domLength     repllen;
    Tcl_CmdInfo   cmdInfo;
    Tcl_Obj     * mobjv[MAX_REWRITE_ARGS], *newObj, *storedErrMsg;
    Tcl_DString   cleardString;

    static const char *domMethods[] = {
        "createDocument",  "createDocumentNS",   "createNodeCmd",
        "createDocumentFromTypedList", "parse",  "setStoreLineColumn",
        "isCharData",      "isName",             "isPIName",
        "isQName",         "isComment",          "isCDATA",
        "isPIValue",       "isNCName",           "createDocumentNode",
        "setNameCheck",    "setTextCheck",       "setObjectCommands",
        "featureinfo",     "isBMPCharData",      "clearString",
        "isHTML5CustomName",
#ifdef TCL_THREADS
        "attachDocument",  "detachDocument",
#endif
        NULL
    };
    enum domMethod {
        m_createDocument,    m_createDocumentNS,   m_createNodeCmd,
        m_createDocumentFromTypedList, m_parse,    m_setStoreLineColumn,
        m_isCharData,        m_isName,             m_isPIName,
        m_isQName,           m_isComment,          m_isCDATA,
        m_isPIValue,         m_isNCName,           m_createDocumentNode,
        m_setNameCheck,      m_setTextCheck,       m_setObjectCommands,
        m_featureinfo,       m_isBMPCharData,      m_clearString,
        m_isHTML5CustomName
#ifdef TCL_THREADS







|













|







7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
    domLength     repllen;
    Tcl_CmdInfo   cmdInfo;
    Tcl_Obj     * mobjv[MAX_REWRITE_ARGS], *newObj, *storedErrMsg;
    Tcl_DString   cleardString;

    static const char *domMethods[] = {
        "createDocument",  "createDocumentNS",   "createNodeCmd",
        "createFromTypedList", "parse",          "setStoreLineColumn",
        "isCharData",      "isName",             "isPIName",
        "isQName",         "isComment",          "isCDATA",
        "isPIValue",       "isNCName",           "createDocumentNode",
        "setNameCheck",    "setTextCheck",       "setObjectCommands",
        "featureinfo",     "isBMPCharData",      "clearString",
        "isHTML5CustomName",
#ifdef TCL_THREADS
        "attachDocument",  "detachDocument",
#endif
        NULL
    };
    enum domMethod {
        m_createDocument,    m_createDocumentNS,   m_createNodeCmd,
        m_createFromTypedList, m_parse,            m_setStoreLineColumn,
        m_isCharData,        m_isName,             m_isPIName,
        m_isQName,           m_isComment,          m_isCDATA,
        m_isPIValue,         m_isNCName,           m_createDocumentNode,
        m_setNameCheck,      m_setTextCheck,       m_setObjectCommands,
        m_featureinfo,       m_isBMPCharData,      m_clearString,
        m_isHTML5CustomName
#ifdef TCL_THREADS
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843

        case m_createDocumentNS:
            return tcldom_createDocumentNS(clientData, interp, --objc, objv+1);

        case m_createDocumentNode:
            return tcldom_createDocumentNode (clientData, interp, --objc,
                                              objv+1);
        case m_createDocumentFromTypedList:
            return tcldom_createDocumentFromTypedList (clientData, interp,
                                                       --objc, objv+1);
        case m_createNodeCmd:
            return nodecmd_createNodeCmd(interp, --objc, objv+1,
                                         !TcldomDATA(dontCheckName),
                                         !TcldomDATA(dontCheckCharData));
        case m_parse:
            return tcldom_parse(clientData, interp, --objc, objv+1);








|
|
|







7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843

        case m_createDocumentNS:
            return tcldom_createDocumentNS(clientData, interp, --objc, objv+1);

        case m_createDocumentNode:
            return tcldom_createDocumentNode (clientData, interp, --objc,
                                              objv+1);
        case m_createFromTypedList:
            return tcldom_createFromTypedList (clientData, interp, --objc,
                                               objv+1);
        case m_createNodeCmd:
            return nodecmd_createNodeCmd(interp, --objc, objv+1,
                                         !TcldomDATA(dontCheckName),
                                         !TcldomDATA(dontCheckCharData));
        case m_parse:
            return tcldom_parse(clientData, interp, --objc, objv+1);

Changes to tests/domjson.test.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#    json-5.*: Max nesting
#    json-6.*: asJSON
#    json-7.*: jsonType
#    json-8.*: appendFromScript
#    json-9.*: cloneNode
#    json-10.*: asTclValue
#    json-11.*: asTypedList
#    json-12.*: createDocumentFromTypedList
# Copyright (c) 2017 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

testConstraint needExpand 1
if {$tcl_version < 8.5} {
    testConstraint needExpand 0







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#    json-5.*: Max nesting
#    json-6.*: asJSON
#    json-7.*: jsonType
#    json-8.*: appendFromScript
#    json-9.*: cloneNode
#    json-10.*: asTclValue
#    json-11.*: asTypedList
#    json-12.*: createFromTypedList
# Copyright (c) 2017 Rolf Ade.

source [file join [file dir [info script]] loadtdom.tcl]

testConstraint needExpand 1
if {$tcl_version < 8.5} {
    testConstraint needExpand 0
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
        set doc [dom parse -json $json]
        lappend result [$doc asTypedList]
        $doc delete
    }
    set result
} {{STRING {string foo}} NULL TRUE FALSE {NUMBER -1.23}}

test json-12.1 {createDocumentFromTypedList} {
    set doc [dom createDocumentFromTypedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}]
    set result [$doc asJSON]
    $doc delete
    set result
} {{"Titel":"Wirtschaftsinformatik","Keywords":["Introduction","Basics"],"Year":2022}}

test json-12.2 {createDocumentFromTypedList} {
    set typedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}
    set doc [dom createDocumentFromTypedList $typedList]
    set result [$doc asTypedList]
    $doc delete
    expr {$typedList eq $result}
} 1

proc createFromTypedList {list} {
    dom createDocumentFromTypedList $list doc
    if {$list eq [$doc asTypedList]} {
        return ""
    }
    error "Invalid turnaround."
}
test json-12.3 {createDocumentFromTypedList} {
    createFromTypedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}
} {}

test json-12.4 {createDocumentFromTypedList} {
    set result ""
    foreach json {
        {STRING foo}
        NULL
        TRUE
        FALSE
        {NUMBER -1.23}
    } {
        set doc [dom createDocumentFromTypedList $json]
        set this [$doc asTypedList]
        if {$json ne $this} {
            lappend result "json $json this $this"
        } else {
            lappend result 1
        }
        $doc delete







|
|





|

|






|





|



|








|







3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
        set doc [dom parse -json $json]
        lappend result [$doc asTypedList]
        $doc delete
    }
    set result
} {{STRING {string foo}} NULL TRUE FALSE {NUMBER -1.23}}

test json-12.1 {createFromTypedList} {
    set doc [dom createFromTypedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}]
    set result [$doc asJSON]
    $doc delete
    set result
} {{"Titel":"Wirtschaftsinformatik","Keywords":["Introduction","Basics"],"Year":2022}}

test json-12.2 {createFromTypedList} {
    set typedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}
    set doc [dom createFromTypedList $typedList]
    set result [$doc asTypedList]
    $doc delete
    expr {$typedList eq $result}
} 1

proc createFromTypedList {list} {
    dom createFromTypedList $list doc
    if {$list eq [$doc asTypedList]} {
        return ""
    }
    error "Invalid turnaround."
}
test json-12.3 {createFromTypedList} {
    createFromTypedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}}
} {}

test json-12.4 {createFromTypedList} {
    set result ""
    foreach json {
        {STRING foo}
        NULL
        TRUE
        FALSE
        {NUMBER -1.23}
    } {
        set doc [dom createFromTypedList $json]
        set this [$doc asTypedList]
        if {$json ne $this} {
            lappend result "json $json this $this"
        } else {
            lappend result 1
        }
        $doc delete
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
            # raise error here does not help much because if the test
            # data is expected to be invalid the command will be
            # [catch]ed. Therefore the puts to catch attention.
            puts  "Unexpeced invalid json data '$data'"
        }
        set njson [$doc asJSON]
        set typedList [$doc asTypedList]
        _dom createDocumentFromTypedList $typedList docFromTyped
        if {$njson ne [$docFromTyped asJSON]} {
            error "Normalized json '$data' differs from normalized json created with createDocumentFromTypedList"
        }
        return [uplevel 1 [linsert $args 0 _dom]]
    }
    source [file join [file dir [info script]] domjson.test]
    rename dom {}
    rename _dom dom
}







|

|







3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
            # raise error here does not help much because if the test
            # data is expected to be invalid the command will be
            # [catch]ed. Therefore the puts to catch attention.
            puts  "Unexpeced invalid json data '$data'"
        }
        set njson [$doc asJSON]
        set typedList [$doc asTypedList]
        _dom createFromTypedList $typedList docFromTyped
        if {$njson ne [$docFromTyped asJSON]} {
            error "Normalized json '$data' differs from normalized json created with createFromTypedList"
        }
        return [uplevel 1 [linsert $args 0 _dom]]
    }
    source [file join [file dir [info script]] domjson.test]
    rename dom {}
    rename _dom dom
}