Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Back out the json_check() routine. Instead, throw an error if the input to a json function (other than json_valid()) is not valid JSON. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
dc9ce7b18cbe23d065317757234ef9fb |
User & Date: | drh 2015-08-28 20:07:40.320 |
Context
2015-08-29
| ||
00:54 | Change the json1.c module so that it throws an error if any of the JSON selector paths are malformed. (check-in: 3aa0855fd4 user: drh tags: trunk) | |
2015-08-28
| ||
20:07 | Back out the json_check() routine. Instead, throw an error if the input to a json function (other than json_valid()) is not valid JSON. (check-in: dc9ce7b18c user: drh tags: trunk) | |
16:41 | Fix compiler warnings in rbu code. (check-in: 0fdc36fe35 user: dan tags: trunk) | |
Changes
Changes to ext/misc/json1.c.
︙ | ︙ | |||
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ** ** For the time being, all JSON is stored as pure text. (We might add ** a JSONB type in the future which stores a binary encoding of JSON in ** a BLOB, but there is no support for JSONB in the current implementation. ** This implementation parses JSON text at 250 MB/s, so it is hard to see ** how JSONB might improve on that.) */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #include <assert.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <stdarg.h> | > > | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ** ** For the time being, all JSON is stored as pure text. (We might add ** a JSONB type in the future which stores a binary encoding of JSON in ** a BLOB, but there is no support for JSONB in the current implementation. ** This implementation parses JSON text at 250 MB/s, so it is hard to see ** how JSONB might improve on that.) */ #if !defined(_SQLITEINT_H_) #include "sqlite3ext.h" #endif SQLITE_EXTENSION_INIT1 #include <assert.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #include <stdarg.h> |
︙ | ︙ | |||
400 401 402 403 404 405 406 407 408 409 410 411 412 413 | j = 1; } jsonAppendChar(pOut, '}'); break; } } } /* ** Make the JsonNode the return value of the function. */ static void jsonReturn( JsonNode *pNode, /* Node to return */ sqlite3_context *pCtx, /* Return value for this function */ | > > > > > > > > > > > > > > | 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 | j = 1; } jsonAppendChar(pOut, '}'); break; } } } /* ** Return a JsonNode and all its descendents as a JSON string. */ static void jsonReturnJson( JsonNode *pNode, /* Node to return */ sqlite3_context *pCtx, /* Return value for this function */ sqlite3_value **aReplace /* Array of replacement values */ ){ JsonString s; jsonInit(&s, pCtx); jsonRenderNode(pNode, &s, aReplace); jsonResult(&s); } /* ** Make the JsonNode the return value of the function. */ static void jsonReturn( JsonNode *pNode, /* Node to return */ sqlite3_context *pCtx, /* Return value for this function */ |
︙ | ︙ | |||
505 506 507 508 509 510 511 | zOut[j] = 0; sqlite3_result_text(pCtx, zOut, j, sqlite3_free); } break; } case JSON_ARRAY: case JSON_OBJECT: { | < < | < | 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | zOut[j] = 0; sqlite3_result_text(pCtx, zOut, j, sqlite3_free); } break; } case JSON_ARRAY: case JSON_OBJECT: { jsonReturnJson(pNode, pCtx, aReplace); break; } } } /* ** Create a new JsonNode instance based on the arguments and append that |
︙ | ︙ | |||
713 714 715 716 717 718 719 | i = jsonParseValue(pParse, 0); if( pParse->oom ) i = -1; if( i>0 ){ while( isspace(zJson[i]) ) i++; if( zJson[i] ) i = -1; } if( i<0 ){ | > > | > > > > | 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | i = jsonParseValue(pParse, 0); if( pParse->oom ) i = -1; if( i>0 ){ while( isspace(zJson[i]) ) i++; if( zJson[i] ) i = -1; } if( i<0 ){ if( pCtx!=0 ){ if( pParse->oom ){ sqlite3_result_error_nomem(pCtx); }else{ sqlite3_result_error(pCtx, "malformed JSON", -1); } } jsonParseReset(pParse); return 1; } return 0; } /* Mark node i of pParse as being a child of iParent. Call recursively |
︙ | ︙ | |||
956 957 958 959 960 961 962 | static void jsonTest1Func( sqlite3_context *ctx, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; | | | 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | static void jsonTest1Func( sqlite3_context *ctx, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; jsonReturnJson(x.aNode, ctx, 0); jsonParseReset(&x); } /* ** The json_nodecount(JSON) function returns the number of nodes in the ** input JSON string. */ |
︙ | ︙ | |||
1029 1030 1031 1032 1033 1034 1035 | zPath = (const char*)sqlite3_value_text(argv[1]); if( zPath==0 ) return; if( zPath[0]!='$' ) return; zPath++; }else{ zPath = 0; } | | | | | | | | | | | | | < | < < < < < < < < < < < < < < < < < < < < < < | 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | zPath = (const char*)sqlite3_value_text(argv[1]); if( zPath==0 ) return; if( zPath[0]!='$' ) return; zPath++; }else{ zPath = 0; } if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return; if( x.nNode ){ JsonNode *pNode = x.aNode; if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0); if( pNode->eType==JSON_ARRAY ){ assert( (pNode->jnFlags & JNODE_APPEND)==0 ); for(i=1; i<=pNode->n; n++){ i += jsonNodeSize(&pNode[i]); } } } jsonParseReset(&x); sqlite3_result_int64(ctx, n); } /* ** json_extract(JSON, PATH) ** ** Return the element described by PATH. Return NULL if JSON is not ** valid JSON or if there is no PATH element or if PATH is malformed. |
︙ | ︙ | |||
1162 1163 1164 1165 1166 1167 1168 | zPath = (const char*)sqlite3_value_text(argv[i]); if( zPath==0 ) continue; if( zPath[0]!='$' ) continue; pNode = jsonLookup(&x, 0, &zPath[1], 0); if( pNode ) pNode->jnFlags |= JNODE_REMOVE; } if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ | | | 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 | zPath = (const char*)sqlite3_value_text(argv[i]); if( zPath==0 ) continue; if( zPath[0]!='$' ) continue; pNode = jsonLookup(&x, 0, &zPath[1], 0); if( pNode ) pNode->jnFlags |= JNODE_REMOVE; } if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ jsonReturnJson(x.aNode, ctx, 0); } } jsonParseReset(&x); } /* ** json_replace(JSON, PATH, VALUE, ...) |
︙ | ︙ | |||
1210 1211 1212 1213 1214 1215 1216 | pNode->jnFlags |= jnFlags; pNode->iVal = i+1; } } if( x.aNode[0].jnFlags & JNODE_REPLACE ){ sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); }else{ | | | 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | pNode->jnFlags |= jnFlags; pNode->iVal = i+1; } } if( x.aNode[0].jnFlags & JNODE_REPLACE ){ sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); }else{ jsonReturnJson(x.aNode, ctx, argv); } } jsonParseReset(&x); } /* ** json_set(JSON, PATH, VALUE, ...) |
︙ | ︙ | |||
1270 1271 1272 1273 1274 1275 1276 | pNode->jnFlags |= jnFlags; pNode->iVal = i+1; } } if( x.aNode[0].jnFlags & JNODE_REPLACE ){ sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); }else{ | | | 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 | pNode->jnFlags |= jnFlags; pNode->iVal = i+1; } } if( x.aNode[0].jnFlags & JNODE_REPLACE ){ sqlite3_result_value(ctx, argv[x.aNode[0].iVal]); }else{ jsonReturnJson(x.aNode, ctx, argv); } } jsonSetDone: jsonParseReset(&x); } /* |
︙ | ︙ | |||
1324 1325 1326 1327 1328 1329 1330 | sqlite3_context *ctx, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ int rc = 0; | | | 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 | sqlite3_context *ctx, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ int rc = 0; if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 && x.nNode>0 ){ rc = 1; } jsonParseReset(&x); sqlite3_result_int(ctx, rc); } |
︙ | ︙ | |||
1798 1799 1800 1801 1802 1803 1804 | int nArg; int flag; void (*xFunc)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "json_array", -1, 0, jsonArrayFunc }, { "json_array_length", 1, 0, jsonArrayLengthFunc }, { "json_array_length", 2, 0, jsonArrayLengthFunc }, | < | 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 | int nArg; int flag; void (*xFunc)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "json_array", -1, 0, jsonArrayFunc }, { "json_array_length", 1, 0, jsonArrayLengthFunc }, { "json_array_length", 2, 0, jsonArrayLengthFunc }, { "json_extract", 2, 0, jsonExtractFunc }, { "json_insert", -1, 0, jsonSetFunc }, { "json_object", -1, 0, jsonObjectFunc }, { "json_remove", -1, 0, jsonRemoveFunc }, { "json_replace", -1, 0, jsonReplaceFunc }, { "json_set", -1, 1, jsonSetFunc }, { "json_type", 1, 0, jsonTypeFunc }, |
︙ | ︙ |
Changes to test/json101.test.
︙ | ︙ | |||
59 60 61 62 63 64 65 | do_execsql_test json1-3.3 { SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); } {text} do_execsql_test json1-3.4 { SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b'); } {object} | < < < < < < < | 59 60 61 62 63 64 65 66 | do_execsql_test json1-3.3 { SELECT json_type(json_set('{"a":1,"b":2}','$.b','{"x":3,"y":4}'),'$.b'); } {text} do_execsql_test json1-3.4 { SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b'); } {object} finish_test |