/ Check-in [269892ab]
Login

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

Overview
Comment:Add the json_quote() function to the JSON1 extension.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 269892abf6e59c417729669cc764d1f237e093fd
User & Date: drh 2016-07-23 19:34:53
Context
2016-07-23
20:27
Use sqlite3_trace_v2() instead of sqlite3_trace() in the shell. check-in: c8e0539b user: drh tags: trunk
19:34
Add the json_quote() function to the JSON1 extension. check-in: 269892ab user: drh tags: trunk
19:18
Small tweak to the btree balancer for improved tree balance. check-in: 8817dedb user: drh tags: trunk
2016-06-17
13:01
Add the json_quote() function to the JSON1 extension. Closed-Leaf check-in: 2c3714ae user: drh tags: json_quote
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/json1.c.

  1206   1206     sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
  1207   1207   }
  1208   1208   #endif /* SQLITE_DEBUG */
  1209   1209   
  1210   1210   /****************************************************************************
  1211   1211   ** Scalar SQL function implementations
  1212   1212   ****************************************************************************/
         1213  +
         1214  +/*
         1215  +** Implementation of the json_QUOTE(VALUE) function.  Return a JSON value
         1216  +** corresponding to the SQL value input.  Mostly this means putting 
         1217  +** double-quotes around strings and returning the unquoted string "null"
         1218  +** when given a NULL input.
         1219  +*/
         1220  +static void jsonQuoteFunc(
         1221  +  sqlite3_context *ctx,
         1222  +  int argc,
         1223  +  sqlite3_value **argv
         1224  +){
         1225  +  JsonString jx;
         1226  +
         1227  +  jsonInit(&jx, ctx);
         1228  +  jsonAppendValue(&jx, argv[0]);
         1229  +  jsonResult(&jx);
         1230  +  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
         1231  +}
  1213   1232   
  1214   1233   /*
  1215   1234   ** Implementation of the json_array(VALUE,...) function.  Return a JSON
  1216   1235   ** array that contains all values given in arguments.  Or if any argument
  1217   1236   ** is a BLOB, throw an error.
  1218   1237   */
  1219   1238   static void jsonArrayFunc(
................................................................................
  2120   2139       { "json",                 1, 0,   jsonRemoveFunc        },
  2121   2140       { "json_array",          -1, 0,   jsonArrayFunc         },
  2122   2141       { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
  2123   2142       { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
  2124   2143       { "json_extract",        -1, 0,   jsonExtractFunc       },
  2125   2144       { "json_insert",         -1, 0,   jsonSetFunc           },
  2126   2145       { "json_object",         -1, 0,   jsonObjectFunc        },
         2146  +    { "json_quote",           1, 0,   jsonQuoteFunc         },
  2127   2147       { "json_remove",         -1, 0,   jsonRemoveFunc        },
  2128   2148       { "json_replace",        -1, 0,   jsonReplaceFunc       },
  2129   2149       { "json_set",            -1, 1,   jsonSetFunc           },
  2130   2150       { "json_type",            1, 0,   jsonTypeFunc          },
  2131   2151       { "json_type",            2, 0,   jsonTypeFunc          },
  2132   2152       { "json_valid",           1, 0,   jsonValidFunc         },
  2133   2153   

Changes to test/json101.test.

   351    351     INSERT INTO t8(a) VALUES('abc' || char(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35) || 'xyz');
   352    352     UPDATE t8 SET b=json_array(a);
   353    353     SELECT b FROM t8;
   354    354   } {{["abc\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 !\"#xyz"]}}
   355    355   do_execsql_test json-8.2 {
   356    356     SELECT a=json_extract(b,'$[0]') FROM t8;
   357    357   } {1}
          358  +
          359  +# The json_quote() function transforms an SQL value into a JSON value.
          360  +# String values are quoted and interior quotes are escaped.  NULL values
          361  +# are rendered as the unquoted string "null".
          362  +#
          363  +do_execsql_test json-9.1 {
          364  +  SELECT json_quote('abc"xyz');
          365  +} {{"abc\"xyz"}}
          366  +do_execsql_test json-9.2 {
          367  +  SELECT json_quote(3.14159);
          368  +} {3.14159}
          369  +do_execsql_test json-9.3 {
          370  +  SELECT json_quote(12345);
          371  +} {12345}
          372  +do_execsql_test json-9.4 {
          373  +  SELECT json_quote(null);
          374  +} {"null"}
          375  +do_catchsql_test json-9.5 {
          376  +  SELECT json_quote(x'30313233');
          377  +} {1 {JSON cannot hold BLOB values}}
          378  +do_catchsql_test json-9.6 {
          379  +  SELECT json_quote(123,456)
          380  +} {1 {wrong number of arguments to function json_quote()}}
          381  +do_catchsql_test json-9.7 {
          382  +  SELECT json_quote()
          383  +} {1 {wrong number of arguments to function json_quote()}}
          384  +
          385  +
          386  +
   358    387   
   359    388   
   360    389   finish_test