Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Improved detection of invalid typed lists. Fixed XML well-formed error in documentation. Improved asTypedList in case of value only json documents. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | asTypedList |
Files: | files | file ages | folders |
SHA3-256: |
5d007b5c736d6f0f673862445fe99247 |
User & Date: | rolf 2024-09-20 11:59:03 |
Context
2024-09-20
| ||
16:57 | Fixes and an improvement to the error messages in case of invalid typed list. check-in: c54d4cb644 user: rolf tags: asTypedList | |
11:59 | Improved detection of invalid typed lists. Fixed XML well-formed error in documentation. Improved asTypedList in case of value only json documents. check-in: 5d007b5c73 user: rolf tags: asTypedList | |
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 | |
Changes
Changes to doc/dom.xml.
︙ | ︙ | |||
410 411 412 413 414 415 416 | <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 | | | 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | <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> </commanddef> <commanddef> <command><cmd>dom</cmd> <method>createNodeCmd</method> <m>?-returnNodeCmd?</m> <m>?-tagName name?</m> <m>?-jsonType jsonType?</m> <m>?-namespace URI?</m> <m>(element|comment|text|cdata|pi)Node</m> <m>commandName</m></command> <desc>This method creates Tcl commands, which in turn create tDOM nodes. Tcl commands created by this command are only |
︙ | ︙ |
Changes to doc/domNode.xml.
︙ | ︙ | |||
671 672 673 674 675 676 677 | <commanddef> <command><m>@attrName</m></command> <desc>Returns the value of the attribute <m>attrName</m>. Short cut for <m>getAttribute</m>.</desc> </commanddef> <commanddef> | | | 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 | <commanddef> <command><m>@attrName</m></command> <desc>Returns the value of the attribute <m>attrName</m>. Short cut for <m>getAttribute</m>.</desc> </commanddef> <commanddef> <command><method>jsonType</method> <m>?(OBJECT|ARRAY|NONE)|(STRING|NUMBER|TRUE|FALSE|NULL|NONE)?</m></command> <desc>Only element and text nodes may have a JSON type and only this types of nodes support the <m>jsonType</m> method; the other node types return error if called with this method. Returns the jsonType of the node. If the optional argument is given, the JSON type of the node is set to the given type and returned. Valid type arguments for element nodes are OBJECT, ARRAY and NONE. Valid type arguments for text nodes are |
︙ | ︙ |
Changes to generic/domjson.c.
︙ | ︙ | |||
73 74 75 76 77 78 79 80 81 82 | int nestingDepth; int maxnesting; char *arrItemElm; char *buf; domLength len; } JSONParse; #define errReturn(i,j) {jparse->state = j; return (i);} | > > < | 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | int nestingDepth; int maxnesting; char *arrItemElm; char *buf; domLength len; } JSONParse; #define SetResult(str) Tcl_ResetResult(interp); \ Tcl_SetStringObj(Tcl_GetObjResult(interp), (str), -1) #define errReturn(i,j) {jparse->state = j; return (i);} /* #define DEBUG */ #ifdef DEBUG # define DBG(x) x #else # define DBG(x) #endif |
︙ | ︙ | |||
247 248 249 250 251 252 253 | i += clen; } } /* Parse a single JSON value which begins at json[i]. Return the index * of the first character past the end of the value parsed. */ | | | 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | i += clen; } } /* Parse a single JSON value which begins at json[i]. Return the index * of the first character past the end of the value parsed. */ static domLength jsonParseValue ( domNode *parent, char *json, domLength i, JSONParse *jparse ) { char c, save; |
︙ | ︙ | |||
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | return j; } 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) { | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > < | < < | < < | > > > > > > < | < > > > > > > > > > > > > | 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 | return j; } else if (c == '\0') { return 0; /* End of input */ } else { errReturn(i,JSON_SYNTAX_ERR); } } /* Helper function which checks if a given string is a JSON number. */ int isJSONNumber ( char *num, domLength numlen ) { domLength i; int seenDP, seenE; unsigned char c; if (numlen == 0) return 0; seenDP = 0; seenE = 0; i = 0; c = num[0]; if (!(c == '-' || (c>='0' && c<='9'))) return 0; if (c<='0') { i = (c == '-' ? i+1 : i); if (i+1 < numlen) { if (num[i] == '0' && num[i+1] >= '0' && num[i+1] <= '9') { return 0; } } } i = 1; for (; i < numlen; i++) { c = num[i]; if (c >= '0' && c <= '9') continue; if (c == '.') { if (num[i-1] == '-') return 0; if (seenDP) return 0; seenDP = 1; continue; } if (c == 'e' || c == 'E') { if (num[i-1] < '0') return 0; if (seenE) return 0; seenDP = seenE = 1; c = num[i+1]; if (c == '+' || c == '-') { i++; c = num[i+1]; } if (c < '0' || c > '9') return 0; continue; } break; } /* Catches a plain '-' without following digits */ if (num[i-1] < '0') return 0; /* Catches trailing chars */ if (i < numlen) return 0; return 1; } 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. */ SetResult ("Invalid list format: Empty list."); return -1; } Tcl_ListObjIndex (interp, list, 1, typeValue); s = Tcl_GetStringFromObj (symbol, &slen); if (strcmp (s, "STRING") == 0) { if (*typeValue == NULL) { SetResult ("Invalid list format: Missing value for STRING."); return -1; } return JSON_STRING; } else if (strcmp (s, "OBJECT") == 0) { if (*typeValue == NULL) { SetResult ("Invalid list format: Missing value for OBJECT."); return -1; } return JSON_OBJECT; } else if (strcmp (s, "NUMBER") == 0) { if (*typeValue == NULL) { SetResult ("Invalid list format: Missing value for NUMBER."); return -1; } s = Tcl_GetStringFromObj (*typeValue, &slen); if (!isJSONNumber (s, slen)) { /* Invalid JSON nummber TODO: appropriate error msg */ SetResult ("Invalid list format: Not a valid NUMBER value."); return -1; } return JSON_NUMBER; } else if (strcmp (s, "ARRAY") == 0) { if (*typeValue == NULL) { SetResult ("Invalid list format: Missing value for ARRAY."); return -1; } return JSON_ARRAY; } else if (strcmp (s, "TRUE") == 0) { if (*typeValue != NULL) { SetResult ("Invalid list format: No value expected for NULL."); } return JSON_TRUE; } else if (strcmp (s, "FALSE") == 0) { if (*typeValue != NULL) { SetResult ("Invalid list format: No value expected for NULL."); } return JSON_FALSE; } else if (strcmp (s, "NULL") == 0) { if (*typeValue != NULL) { SetResult ("Invalid list format: No value expected for NULL."); } return JSON_NULL; } else { if (*typeValue != NULL) { SetResult ("Invalid list format."); } return -1; } } static int TypedList2DOMWorker ( Tcl_Interp *interp, |
︙ | ︙ | |||
562 563 564 565 566 567 568 | 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: | < < | 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | 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: case JSON_STRING: str = Tcl_GetStringFromObj (pdetail, &strl); break; default: str = ""; strl = 0; break; |
︙ | ︙ | |||
608 609 610 611 612 613 614 | 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: | < < < < < < < | | < < < < < | < < < < > > | < < < < < < < < < | 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 | 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: 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; default: /* Every "text node" JSON values are either done directly by * TypedList2DOM() or inline in the OBJECT and ARRAY cases in * this function. */ /* TODO: appropriate error msg */ return TCL_ERROR; } return TCL_OK; } domDocument * TypedList2DOM ( Tcl_Interp *interp, |
︙ | ︙ | |||
677 678 679 680 681 682 683 | jsonType = getJSONTypeFromList (interp, typedList, &value); if (jsonType < 0) { return NULL; } doc = domCreateDoc (NULL, 0); rootNode = doc->rootNode; | | | | < | < < < < < | < < < | | < < < | 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 | jsonType = getJSONTypeFromList (interp, typedList, &value); if (jsonType < 0) { return NULL; } doc = domCreateDoc (NULL, 0); rootNode = doc->rootNode; if (jsonType < 3) { /* JSON_OBJECT or JSON_ARRAY */ rootNode->info = jsonType; if (TypedList2DOMWorker (interp, rootNode, value) != TCL_OK) { goto error; } } else { /* The other json types are represented by a text node.*/ if (jsonType > 5) { /* JSON_STRING or JSON_NUMBER */ str = Tcl_GetStringFromObj (value, &strl); } else { str = ""; strl = 0; } textNode = domNewTextNode (doc, str, strl, TEXT_NODE); textNode->info = jsonType; domAppendChild (rootNode, (domNode *) textNode); } return doc; error: tcldom_deleteDoc (interp, doc); return NULL; } |
︙ | ︙ |
Changes to generic/domjson.h.
︙ | ︙ | |||
36 37 38 39 40 41 42 | ); domDocument * TypedList2DOM ( Tcl_Interp *interp, Tcl_Obj *typedList ); | > > > > > > | 36 37 38 39 40 41 42 43 44 45 46 47 48 | ); domDocument * TypedList2DOM ( Tcl_Interp *interp, Tcl_Obj *typedList ); int isJSONNumber ( char *num, domLength numlen ); |
Changes to generic/tcldom.c.
︙ | ︙ | |||
3132 3133 3134 3135 3136 3137 3138 | writeChars(jstring, channel, "}",1); break; default: break; } } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 | writeChars(jstring, channel, "}",1); break; default: break; } } /*---------------------------------------------------------------------------- | tcldom_treeAsJSON | \---------------------------------------------------------------------------*/ static void tcldom_treeAsJSON ( |
︙ | ︙ | |||
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 | Tcl_IncrRefCount (c.array); Tcl_IncrRefCount (c.null); Tcl_IncrRefCount (c.true); Tcl_IncrRefCount (c.false); Tcl_IncrRefCount (c.number); Tcl_IncrRefCount (c.string); Tcl_SetObjResult (interp, tcldom_treeAsTypedListWorker (node, &c)); Tcl_DecrRefCount (c.object); Tcl_DecrRefCount (c.array); Tcl_DecrRefCount (c.null); Tcl_DecrRefCount (c.true); | > > > > > > > > | 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 | Tcl_IncrRefCount (c.array); Tcl_IncrRefCount (c.null); Tcl_IncrRefCount (c.true); Tcl_IncrRefCount (c.false); Tcl_IncrRefCount (c.number); Tcl_IncrRefCount (c.string); if (node->nodeType == ELEMENT_NODE && node->info == 0 && node->firstChild->nodeType == TEXT_NODE) { /* Either a value only json document or a OBJECT propertiy * element node.*/ node = node->firstChild; } Tcl_SetObjResult (interp, tcldom_treeAsTypedListWorker (node, &c)); Tcl_DecrRefCount (c.object); Tcl_DecrRefCount (c.array); Tcl_DecrRefCount (c.null); Tcl_DecrRefCount (c.true); |
︙ | ︙ |
Changes to tests/domjson.test.
︙ | ︙ | |||
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 | }} 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}} | > > > > > > > > > > > > > > > > | 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 | }} 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-11.4 {asTypedList} { set result "" foreach json { {"string foo"} null true false -1.23 } { 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}} |
︙ | ︙ | |||
3038 3039 3040 3041 3042 3043 3044 | } error "Invalid turnaround." } test json-12.3 {createDocumentFromTypedList} { createFromTypedList {OBJECT {Titel {STRING Wirtschaftsinformatik} Keywords {ARRAY {{STRING Introduction} {STRING Basics}}} Year {NUMBER 2022}}} } {} | > > > > > > > > > > > > > > > | > > > > > > > | 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 3083 | } 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 } set result } {1 1 1 1 1} |