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

Overview
Comment:Added a first version (fully working for correct input but with some things left so do) of a way to create a (JSON) DOM document from a typed list in the format produced by asTypedList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | asTypedList
Files: files | file ages | folders
SHA3-256: 49831a455b0d7b5b06596762f2b34fe294838c3dbd748beb80a9ccb8f82eed10
User & Date: rolf 2024-09-18 23:11:38
Context
2024-09-18
23:57
Made the optioal newObjVar argument of createDocumentFromTypedList in fact work. Added some documentation. check-in: 24f228477d user: rolf tags: asTypedList
23:11
Added a first version (fully working for correct input but with some things left so do) of a way to create a (JSON) DOM document from a typed list in the format produced by asTypedList. check-in: 49831a455b user: rolf tags: asTypedList
2024-09-10
23:12
Removed the first approach, improved the second, added a few basic tests and some documentation. check-in: 28577906a9 user: rolf tags: asTypedList
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/domjson.c.

451
452
453
454
455
456
457






































































































































































































































































458
459
460
461
462
463
464
    } else if (c == '\0') {
        return 0;   /* End of input */
    } else {
        errReturn(i,JSON_SYNTAX_ERR);
    }
}








































































































































































































































































domDocument *
JSON_Parse (
    char *json,    /* Complete text of the json string being parsed */
    char *documentElement, /* name of the root element, may be NULL */
    int   maxnesting,
    char **errStr,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
    } else if (c == '\0') {
        return 0;   /* End of input */
    } else {
        errReturn(i,JSON_SYNTAX_ERR);
    }
}

static inline int
getJSONTypeFromList (
    Tcl_Interp *interp,
    Tcl_Obj *list,
    Tcl_Obj **typeValue
    )
{
    Tcl_Obj *symbol;
    char *s;
    domLength slen;
    
    if (Tcl_ListObjIndex (interp, list, 0, &symbol) != TCL_OK) {
        return -1;
    }
    if (!symbol) {
        /* Empty lists are not allowed.
         TODO: appropriate error msg */
        return -1;
    }
    s = Tcl_GetStringFromObj (symbol, &slen);
    if (strcmp (s, "STRING") == 0) {
        Tcl_ListObjIndex (interp, list, 1, typeValue);
        if (*typeValue == NULL) {
            /* Missing value element 
               TODO: appropriate error msg */
            return -1;
        }
        return JSON_STRING;
    } else if (strcmp (s, "OBJECT") == 0) {
        Tcl_ListObjIndex (interp, list, 1, typeValue);
        if (*typeValue == NULL) {
            /* Missing value element 
               TODO: appropriate error msg */
            return -1;
        }
        return JSON_OBJECT;
    } else if (strcmp (s, "NUMBER") == 0) {
        Tcl_ListObjIndex (interp, list, 1, typeValue);
        if (*typeValue == NULL) {
            /* Missing value element 
               TODO: appropriate error msg */
            return -1;
        }
        return JSON_NUMBER;
    } else if (strcmp (s, "ARRAY") == 0) {
        Tcl_ListObjIndex (interp, list, 1, typeValue);
        if (*typeValue == NULL) {
            /* Missing value element 
               TODO: appropriate error msg */
            return -1;
        }
        return JSON_ARRAY;
    } else if (strcmp (s, "TRUE") == 0) {
        return JSON_TRUE;
    } else if (strcmp (s, "FALSE") == 0) {
        return JSON_FALSE;
    } else if (strcmp (s, "NULL") == 0) {
        return JSON_NULL;
    } else {
        return -1;
    }            
}

static int
TypedList2DOMWorker (
    Tcl_Interp *interp,
    domNode *parent,
    Tcl_Obj *value
    )
{
    Tcl_Obj *property, *pvalue, *pdetail, *aelm, *adetail;
    domLength llen, i, strl;
    domNode *pnode, *container;
    domTextNode *textNode;
    char *str;
    int jsonType;
    
    switch (parent->info) {
    case JSON_OBJECT:
        if (Tcl_ListObjLength (interp, value, &llen) != TCL_OK) {
            return TCL_ERROR;
        }
        if (llen % 2 != 0) {
            Tcl_ResetResult (interp);
            Tcl_AppendResult (interp, "An OBJECT value must be a Tcl "
                              "list with an even number of elements.",
                              (char *) NULL);
            return TCL_ERROR;
        }
        for (i = 0; i < llen; i += 2) {
            /* Since we loop over all elements every element is
             * present and there is no need to check property or
             * pvalue for NULL. */
            Tcl_ListObjIndex (interp, value, i, &property);
            Tcl_ListObjIndex (interp, value, i+1, &pvalue);
            pnode = domAppendNewElementNode (parent, Tcl_GetString (property),
                                             NULL);
            jsonType = getJSONTypeFromList (interp, pvalue, &pdetail);
            if (jsonType < 0) {
                return TCL_ERROR;
            }
            if (jsonType < 3) {
                /* JSON_OBJECT or JSON_ARRAY */
                pnode->info = jsonType;
                if (TypedList2DOMWorker (interp, pnode, pdetail) != TCL_OK) {
                    return TCL_ERROR;
                }
            } else {
                /* The other json types are represented by a text node.*/
                switch (jsonType) {
                case JSON_NUMBER:
                    /* TODO check number */
                    /* fall through */
                case JSON_STRING:
                    str = Tcl_GetStringFromObj (pdetail, &strl);
                    break;
                default:
                    str = "";
                    strl = 0;
                    break;
                }
                textNode = domNewTextNode (parent->ownerDocument,
                                           str, strl, TEXT_NODE);
                textNode->info = jsonType;
                domAppendChild (pnode, (domNode *) textNode);
            }
        }
        break;
    case JSON_ARRAY:
        if (Tcl_ListObjLength (interp, value, &llen) != TCL_OK) {
            return TCL_ERROR;
        }
        for (i = 0; i < llen; i++) {
            Tcl_ListObjIndex (interp, value, i, &aelm);
            jsonType = getJSONTypeFromList (interp, aelm, &adetail);
            if (jsonType < 0) {
                return TCL_ERROR;
            }
            switch (jsonType) {
            case JSON_OBJECT:
                container = domAppendNewElementNode (parent,
                                                     JSON_OBJECT_CONTAINER,
                                                     NULL);
                container->info = JSON_OBJECT;                
                TypedList2DOMWorker (interp, container, adetail);
                break;
            case JSON_ARRAY:
                container = domAppendNewElementNode (parent,
                                                     JSON_ARRAY_CONTAINER,
                                                     NULL);
                container->info = JSON_ARRAY;                
                TypedList2DOMWorker (interp, container, adetail);
                break;
            default:
                /* The other json types are represented by a text node.*/
                switch (jsonType) {
                case JSON_NUMBER:
                    /* TODO check number */
                    /* fall through */
                case JSON_STRING:
                    str = Tcl_GetStringFromObj (adetail, &strl);
                    break;
                default:
                    str = "";
                    strl = 0;
                    break;
                }
                textNode = domNewTextNode (parent->ownerDocument,
                                           str, strl, TEXT_NODE);
                textNode->info = jsonType;
                domAppendChild (parent, (domNode *) textNode);
                break;
            }
        }
        break;
    case JSON_NULL:
        textNode = domNewTextNode (parent->ownerDocument,
                                   "", 0, TEXT_NODE);
        textNode->info = JSON_NULL;
        domAppendChild (parent, (domNode *) textNode);
        break;
    case JSON_TRUE:
        textNode = domNewTextNode (parent->ownerDocument,
                                   "", 0, TEXT_NODE);
        textNode->info = JSON_TRUE;
        domAppendChild (parent, (domNode *) textNode);
        break;
    case JSON_FALSE:
        textNode = domNewTextNode (parent->ownerDocument,
                                   "", 0, TEXT_NODE);
        textNode->info = JSON_FALSE;
        domAppendChild (parent, (domNode *) textNode);
        break;
    case JSON_NUMBER:
        str = Tcl_GetStringFromObj (value, &strl);
        break;
    case JSON_STRING:
        str = Tcl_GetStringFromObj (value, &strl);
        textNode = domNewTextNode (parent->ownerDocument, str, strl,
                                   TEXT_NODE);
        textNode->info = JSON_STRING;
        break;
    }
    return TCL_OK;
}
    
domDocument *
TypedList2DOM (
    Tcl_Interp *interp,
    Tcl_Obj *typedList
    )
{
    domDocument *doc;
    domNode *rootNode;
    domTextNode *textNode;
    Tcl_Obj *value;
    char *str;
    domLength strl;
    int jsonType;

    jsonType = getJSONTypeFromList (interp, typedList, &value);
    if (jsonType < 0) {
        return NULL;
    }
    doc  = domCreateDoc (NULL, 0);
    rootNode = doc->rootNode;
    switch (jsonType) {
    case JSON_OBJECT:
        rootNode->info = JSON_OBJECT;
        if (TypedList2DOMWorker (interp, rootNode, value) != TCL_OK) {
            goto error;
        }
        break;
    case JSON_ARRAY:
        rootNode->info = JSON_ARRAY;
        if (TypedList2DOMWorker (interp, rootNode, value) != TCL_OK) {
            goto error;
        }
    default:
        /* The other json types are represented by a text node.*/
        switch (jsonType) {
        case JSON_NUMBER:
            /* TODO check number */
            /* fall through */
        case JSON_STRING:
            str = Tcl_GetStringFromObj (value, &strl);
            break;
        default:
            str = "";
            strl = 0;
            break;
        }
        textNode = domNewTextNode (doc, str, strl, TEXT_NODE);
        textNode->info = jsonType;
        domAppendChild (rootNode, (domNode *) textNode);
        break;
    }
    return doc;
error:
    tcldom_deleteDoc (interp, doc);
    return NULL;
}

domDocument *
JSON_Parse (
    char *json,    /* Complete text of the json string being parsed */
    char *documentElement, /* name of the root element, may be NULL */
    int   maxnesting,
    char **errStr,

Changes to generic/domjson.h.

31
32
33
34
35
36
37





    char *json,    /* Complete text of the json string being parsed */
    char *documentElement, /* name of the root element, may be NULL */
    int   maxnesting,
    char **errStr,
    domLength *byteIndex
    );













>
>
>
>
>
31
32
33
34
35
36
37
38
39
40
41
42
    char *json,    /* Complete text of the json string being parsed */
    char *documentElement, /* name of the root element, may be NULL */
    int   maxnesting,
    char **errStr,
    domLength *byteIndex
    );

domDocument *
TypedList2DOM (
    Tcl_Interp *interp,
    Tcl_Obj *typedList
    );

Changes to generic/tcldom.c.

6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
    }
    if (objc > 2) {
        if (Tcl_GetIndexFromObj(interp, objv[1], options, "option",
                                0, &index) != TCL_OK) {
            return TCL_ERROR;
        }
        Tcl_ResetResult(interp);
            if (Tcl_GetIndexFromObj(interp, objv[2], jsonTypes, "jsonType",
                                0, &jsonType) != TCL_OK) {
            return TCL_ERROR;
        }
        if (objc == 4) {
            newObjName = objv[3];
        }
    }







|







6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
    }
    if (objc > 2) {
        if (Tcl_GetIndexFromObj(interp, objv[1], options, "option",
                                0, &index) != TCL_OK) {
            return TCL_ERROR;
        }
        Tcl_ResetResult(interp);
        if (Tcl_GetIndexFromObj(interp, objv[2], jsonTypes, "jsonType",
                                0, &jsonType) != TCL_OK) {
            return TCL_ERROR;
        }
        if (objc == 4) {
            newObjName = objv[3];
        }
    }
6928
6929
6930
6931
6932
6933
6934




























6935
6936
6937
6938
6939
6940
6941
        }
        doc = domCreateDocument (NULL, Tcl_GetString(objv[2]));
    } else {
        doc = domCreateDocument (uri, Tcl_GetString(objv[2]));
    }
    return tcldom_returnDocumentObj (interp, doc, newObjName, 1, 0);
}





























/* Helper function to build up the error string message in a central
 * place. Caller must provide byteIndex; line is expected to be > 0 if
 * line/column information is given. */
void tcldom_reportErrorLocation (
    Tcl_Interp *interp,
    int before,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
        }
        doc = domCreateDocument (NULL, Tcl_GetString(objv[2]));
    } 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;
    Tcl_Obj     *newObjName = NULL;

    CheckArgs(2,3,1,"typedList ?newObjVar?");

    if (objc == 3) {
        newObjName = objv[1];
    }

    doc = TypedList2DOM (interp, objv[1]);
    if (doc == NULL) {
        return TCL_ERROR;
    }
    return tcldom_returnDocumentObj(interp, doc, newObjName, 1, 0);
}

/* Helper function to build up the error string message in a central
 * place. Caller must provide byteIndex; line is expected to be > 0 if
 * line/column information is given. */
void tcldom_reportErrorLocation (
    Tcl_Interp *interp,
    int before,
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
    domLength     repllen;
    Tcl_CmdInfo   cmdInfo;
    Tcl_Obj     * mobjv[MAX_REWRITE_ARGS], *newObj, *storedErrMsg;
    Tcl_DString   cleardString;

    static const char *domMethods[] = {
        "createDocument",  "createDocumentNS",   "createNodeCmd",
        "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_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







|













|







7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
    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
7848
7849
7850
7851
7852
7853
7854



7855
7856
7857
7858
7859
7860
7861

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

        case m_createDocumentNode:
            return tcldom_createDocumentNode (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);








>
>
>







7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892

        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);

Changes to tests/domjson.test.

9
10
11
12
13
14
15

16
17
18
19
20
21
22
#    json-5.*: Max nesting
#    json-6.*: asJSON
#    json-7.*: jsonType
#    json-8.*: appendFromScript
#    json-9.*: cloneNode
#    json-10.*: asTclValue
#    json-11.*: asTypedList

# 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.*: 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
3010
3011
3012
3013
3014
3015
3016















        " one two ": "edf"
    }}
    set doc [dom parse -json $json]
    set result [$doc asTypedList]
    $doc delete
    set result
} {OBJECT {{one two} {STRING abc} one\{two {STRING abc} { one two } {STRING edf}}}






















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
        " one two ": "edf"
    }}
    set doc [dom parse -json $json]
    set result [$doc asTypedList]
    $doc delete
    set result
} {OBJECT {{one two} {STRING abc} one\{two {STRING abc} { one two } {STRING edf}}}

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