Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Added -indent tabs to asJSON also. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | tabs |
Files: | files | file ages | folders |
SHA3-256: |
4f33810032ac3530aa225777c11a1eee |
User & Date: | rolf 2020-07-22 23:42:39 |
Context
2020-07-23
| ||
01:00 | Worked on the asXML documentation. Closed-Leaf check-in: 66cae3c794 user: rolf tags: tabs | |
2020-07-22
| ||
23:42 | Added -indent tabs to asJSON also. check-in: 4f33810032 user: rolf tags: tabs | |
00:49 | Added "tabs" as possible value to asXML -indent and do the indentation with tabs, if given. check-in: cf00a7c899 user: rolf tags: tabs | |
Changes
Changes to generic/tcldom.c.
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 .... 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 .... 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 .... 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 .... 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 .... 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 .... 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 .... 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 .... 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 .... 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 |
| \---------------------------------------------------------------------------*/ static Tcl_VarTraceProc tcldom_docTrace; static Tcl_CmdDeleteProc tcldom_docCmdDeleteProc; static void tcldom_treeAsJSON(Tcl_Obj *jstring, domNode *node, Tcl_Channel channel, int indent, int level, int inside); #ifdef TCL_THREADS static int tcldom_EvalLocked(Tcl_Interp* interp, Tcl_Obj** objv, domDocument* doc, int flag); static int tcldom_RegisterDocShared(domDocument* doc); ................................................................................ static void tcldom_childsAsJSON ( Tcl_Obj *jstring, domNode *node, /* Must be an ELEMENT_NODE */ Tcl_Channel channel, int indent, int level, int inside ) { domNode *child, *nextChild; int i, effectivParentType = 0; int first = 1; ................................................................................ } if (nextChild) { effectivParentType = JSON_ARRAY; } else { /* Exactly one 'relevant' child node, a text node; * serialize it as simple token value. */ tcldom_treeAsJSON (jstring, child, channel, indent, level, JSON_ARRAY); return; } } } switch (effectivParentType) { case JSON_ARRAY: ................................................................................ first = 0; level++; } else { writeChars(jstring, channel, ",", 1); } if (indent > -1) { writeChars(jstring, channel, "\n", 1); if (first) level++; for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } tcldom_treeAsJSON (jstring, child, channel, indent, level, JSON_ARRAY); child = child->nextSibling; while (child && child->nodeType != TEXT_NODE && child->nodeType != ELEMENT_NODE) { child = child->nextSibling; } } if (indent > -1 && first == 0) { writeChars(jstring, channel, "\n", 1); level--; for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } writeChars(jstring, channel, "]",1); break; case JSON_OBJECT: writeChars(jstring, channel, "{",1); while (child) { ................................................................................ first = 0; level++; } else { writeChars(jstring, channel, ",", 1); } if (indent > -1) { writeChars(jstring, channel, "\n", 1); if (first) level++; for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } tcldom_treeAsJSON (jstring, child, channel, indent, level, JSON_OBJECT); child = child->nextSibling; /* Inside of a JSON_OBJECT, only element childs make * semantically sense. */ while (child && child->nodeType != ELEMENT_NODE) { child = child->nextSibling; } } if (indent > -1 && first == 0) { writeChars(jstring, channel, "\n", 1); level--; for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } writeChars(jstring, channel, "}",1); break; default: break; } ................................................................................ \---------------------------------------------------------------------------*/ static void tcldom_treeAsJSON ( Tcl_Obj *jstring, domNode *node, /* Must not be NULL */ Tcl_Channel channel, int indent, int level, int inside ) { domTextNode *textNode; int i, seenDP, seenE; unsigned char c; ................................................................................ switch (inside) { case JSON_OBJECT: /* Write the member name and recurse to the childs for the * value. */ tcldom_AppendEscapedJSON (jstring, channel, node->nodeName, -1); writeChars (jstring, channel, ":", 1); tcldom_childsAsJSON (jstring, node, channel, indent, level, inside); break; case JSON_ARRAY: /* Since we're already inside of an array, the element can only be interpreted as a container for a nested JSON object or array. */ tcldom_childsAsJSON (jstring, node, channel, indent, level, inside); break; case JSON_START: tcldom_childsAsJSON (jstring, node, channel, indent, level, inside); break; } return; default: /* Any other node types (COMMENT_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE) are ignored. */ return; ................................................................................ domNode *node, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[] ) { char *channelId; int optionIndex, mode, indent = -1; Tcl_Obj *resultPtr; Tcl_Channel chan = (Tcl_Channel) NULL; static const char *asJSONOptions[] = { "-channel", "-indent", NULL }; ................................................................................ return TCL_ERROR; } if (strcmp("none", Tcl_GetString(objv[3]))==0) { indent = -1; } else if (strcmp("no", Tcl_GetString(objv[3]))==0) { indent = -1; } else if (Tcl_GetIntFromObj(interp, objv[3], &indent) != TCL_OK) { SetResult( "indent must be an integer (0..8) or 'no'/'none'"); return TCL_ERROR; } else if (indent < 0 || indent > 8) { SetResult( "indent must be an integer (0..8) or 'no'/'none'"); return TCL_ERROR; ................................................................................ objc -= 2; objv += 2; break; } } resultPtr = Tcl_NewStringObj("", 0); tcldom_treeAsJSON(resultPtr, node, chan, indent, 0, JSON_START); Tcl_AppendResult(interp, Tcl_GetString(resultPtr), NULL); Tcl_DecrRefCount(resultPtr); return TCL_OK; } /*---------------------------------------------------------------------------- | cdataSectionElements |
< | > | | | > > > > | > | > | > > > > | > | | > > > > | > | > | > > > > | > > > > > | | | | > > > > > | > |
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 .... 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 .... 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 .... 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 .... 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 .... 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 .... 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 .... 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 .... 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 .... 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 |
| \---------------------------------------------------------------------------*/ static Tcl_VarTraceProc tcldom_docTrace; static Tcl_CmdDeleteProc tcldom_docCmdDeleteProc; static void tcldom_treeAsJSON(Tcl_Obj *jstring, domNode *node, Tcl_Channel channel, int indent, int outputFlags, int level, int inside); #ifdef TCL_THREADS static int tcldom_EvalLocked(Tcl_Interp* interp, Tcl_Obj** objv, domDocument* doc, int flag); static int tcldom_RegisterDocShared(domDocument* doc); ................................................................................ static void tcldom_childsAsJSON ( Tcl_Obj *jstring, domNode *node, /* Must be an ELEMENT_NODE */ Tcl_Channel channel, int indent, int outputFlags, int level, int inside ) { domNode *child, *nextChild; int i, effectivParentType = 0; int first = 1; ................................................................................ } if (nextChild) { effectivParentType = JSON_ARRAY; } else { /* Exactly one 'relevant' child node, a text node; * serialize it as simple token value. */ tcldom_treeAsJSON (jstring, child, channel, indent, outputFlags, level, JSON_ARRAY); return; } } } switch (effectivParentType) { case JSON_ARRAY: ................................................................................ first = 0; level++; } else { writeChars(jstring, channel, ",", 1); } if (indent > -1) { writeChars(jstring, channel, "\n", 1); if (outputFlags & SERIALIZE_INDENT_WITH_TAB) { for (i = 0; i < level; i++) { writeChars(jstring, channel, "\t", 1); } } else { for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } } tcldom_treeAsJSON (jstring, child, channel, indent, outputFlags, level, JSON_ARRAY); child = child->nextSibling; while (child && child->nodeType != TEXT_NODE && child->nodeType != ELEMENT_NODE) { child = child->nextSibling; } } if (indent > -1 && first == 0) { writeChars(jstring, channel, "\n", 1); level--; if (outputFlags & SERIALIZE_INDENT_WITH_TAB) { for (i = 0; i < level; i++) { writeChars(jstring, channel, "\t", 1); } } else { for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } } writeChars(jstring, channel, "]",1); break; case JSON_OBJECT: writeChars(jstring, channel, "{",1); while (child) { ................................................................................ first = 0; level++; } else { writeChars(jstring, channel, ",", 1); } if (indent > -1) { writeChars(jstring, channel, "\n", 1); if (outputFlags & SERIALIZE_INDENT_WITH_TAB) { for (i = 0; i < level; i++) { writeChars(jstring, channel, "\t", 1); } } else { for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } } tcldom_treeAsJSON (jstring, child, channel, indent, outputFlags, level, JSON_OBJECT); child = child->nextSibling; /* Inside of a JSON_OBJECT, only element childs make * semantically sense. */ while (child && child->nodeType != ELEMENT_NODE) { child = child->nextSibling; } } if (indent > -1 && first == 0) { writeChars(jstring, channel, "\n", 1); level--; if (outputFlags & SERIALIZE_INDENT_WITH_TAB) { for (i = 0; i < level; i++) { writeChars(jstring, channel, "\t", 1); } } else { for (i = 0; i < level; i++) { writeChars(jstring, channel, " ", indent); } } } writeChars(jstring, channel, "}",1); break; default: break; } ................................................................................ \---------------------------------------------------------------------------*/ static void tcldom_treeAsJSON ( Tcl_Obj *jstring, domNode *node, /* Must not be NULL */ Tcl_Channel channel, int indent, int outputFlags, int level, int inside ) { domTextNode *textNode; int i, seenDP, seenE; unsigned char c; ................................................................................ switch (inside) { case JSON_OBJECT: /* Write the member name and recurse to the childs for the * value. */ tcldom_AppendEscapedJSON (jstring, channel, node->nodeName, -1); writeChars (jstring, channel, ":", 1); if (indent > -1 || outputFlags & SERIALIZE_INDENT_WITH_TAB) { writeChars (jstring, channel, " ", 1); } tcldom_childsAsJSON (jstring, node, channel, indent, outputFlags, level, inside); break; case JSON_ARRAY: /* Since we're already inside of an array, the element can only be interpreted as a container for a nested JSON object or array. */ tcldom_childsAsJSON (jstring, node, channel, indent, outputFlags, level, inside); break; case JSON_START: tcldom_childsAsJSON (jstring, node, channel, indent, outputFlags, level, inside); break; } return; default: /* Any other node types (COMMENT_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE) are ignored. */ return; ................................................................................ domNode *node, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[] ) { char *channelId; int optionIndex, mode, outputFlags = 0, indent = -1; Tcl_Obj *resultPtr; Tcl_Channel chan = (Tcl_Channel) NULL; static const char *asJSONOptions[] = { "-channel", "-indent", NULL }; ................................................................................ return TCL_ERROR; } if (strcmp("none", Tcl_GetString(objv[3]))==0) { indent = -1; } else if (strcmp("no", Tcl_GetString(objv[3]))==0) { indent = -1; } else if (strcmp("tabs", Tcl_GetString(objv[3]))==0) { /* User wants indentation */ indent = 0; outputFlags |= SERIALIZE_INDENT_WITH_TAB; } else if (Tcl_GetIntFromObj(interp, objv[3], &indent) != TCL_OK) { SetResult( "indent must be an integer (0..8) or 'no'/'none'"); return TCL_ERROR; } else if (indent < 0 || indent > 8) { SetResult( "indent must be an integer (0..8) or 'no'/'none'"); return TCL_ERROR; ................................................................................ objc -= 2; objv += 2; break; } } resultPtr = Tcl_NewStringObj("", 0); tcldom_treeAsJSON(resultPtr, node, chan, indent, outputFlags, 0, JSON_START); Tcl_AppendResult(interp, Tcl_GetString(resultPtr), NULL); Tcl_DecrRefCount(resultPtr); return TCL_OK; } /*---------------------------------------------------------------------------- | cdataSectionElements |
Changes to tests/domjson.test.
386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
test json-6.5 {asJSON - serialization of control characters} { set doc [dom parse -json "\"a\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\u0009\\u000A\\u000B\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F\\u0020b\""] set result [$doc asJSON] $doc delete set result } {"a\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f b"} test json-7.1 {jsonType} { set doc [dom parse {<j>foo</j>}] set root [$doc documentElement] set result [list] lappend result [$root asJSON] lappend result [$root jsonType] |
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
test json-6.5 {asJSON - serialization of control characters} { set doc [dom parse -json "\"a\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\u0009\\u000A\\u000B\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F\\u0020b\""] set result [$doc asJSON] $doc delete set result } {"a\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f b"} test json-6.6 {asJSON -indent} { set doc [dom parse -json {{"a":{"aa":[1,2,3,4,"abc"]},"b":"bvalue"}}] set result [$doc asJSON -indent 2] $doc delete set result } {{ "a": { "aa": [ 1, 2, 3, 4, "abc" ] }, "b": "bvalue" }} test json-6.7 {asJSON -indent} { set doc [dom parse -json {{"a":{"aa":[1,2,3,4,"abc"]},"b":"bvalue"}}] set result [$doc asJSON -indent tabs] $doc delete set result } {{ "a": { "aa": [ 1, 2, 3, 4, "abc" ] }, "b": "bvalue" }} test json-7.1 {jsonType} { set doc [dom parse {<j>foo</j>}] set root [$doc documentElement] set result [list] lappend result [$root asJSON] lappend result [$root jsonType] |