Changeset: 2ed428dc675c for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=2ed428dc675c
Added Files:
        monetdb5/modules/atoms/Tests/json06.stable.err
        monetdb5/modules/atoms/Tests/json06.stable.out
Modified Files:
        monetdb5/modules/atoms/Tests/All
        monetdb5/modules/atoms/json_atom.c
        monetdb5/modules/atoms/json_atom.h
        monetdb5/modules/atoms/json_atom.mal
        sql/test/json/Tests/miniexample.sql
Branch: default
Log Message:

Add the json_keys function
It returns a JSON array with the top level keys in a json object.


diffs (210 lines):

diff --git a/monetdb5/modules/atoms/Tests/All b/monetdb5/modules/atoms/Tests/All
--- a/monetdb5/modules/atoms/Tests/All
+++ b/monetdb5/modules/atoms/Tests/All
@@ -21,7 +21,8 @@ json02
 json03
 json04
 json05
+json06
 jsonrender
 
 uuid00
-#curl00
\ No newline at end of file
+#curl00
diff --git a/monetdb5/modules/atoms/Tests/json06.stable.err 
b/monetdb5/modules/atoms/Tests/json06.stable.err
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/json06.stable.err
@@ -0,0 +1,29 @@
+stderr of test 'json06` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 23:31:57 >  
+# 23:31:57 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=38640" "--set" 
"mapi_usock=/var/tmp/mtest-5537/.s.monetdb.38640" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms"
 "json06.mal"
+# 23:31:57 >  
+
+# builtin opt  gdk_dbpath = 
/export/scratch1/mk/current//Linux/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = yes
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 38640
+# cmdline opt  mapi_usock = /var/tmp/mtest-5537/.s.monetdb.38640
+# cmdline opt  monet_prompt = 
+# cmdline opt  mal_listing = 2
+# cmdline opt  gdk_dbpath = 
/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms
+
+# 23:31:57 >  
+# 23:31:57 >  "Done."
+# 23:31:57 >  
+
diff --git a/monetdb5/modules/atoms/Tests/json06.stable.out 
b/monetdb5/modules/atoms/Tests/json06.stable.out
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/json06.stable.out
@@ -0,0 +1,32 @@
+stdout of test 'json06` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 23:31:57 >  
+# 23:31:57 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=38640" "--set" 
"mapi_usock=/var/tmp/mtest-5537/.s.monetdb.38640" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms"
 "json06.mal"
+# 23:31:57 >  
+
+# MonetDB 5 server v11.16.0
+# This is an unreleased version
+# Serving database 'mTests_monetdb5_modules_atoms', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically 
linked
+# Found 15.591 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2013 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://vienna.ins.cwi.nl:38640/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-5537/.s.monetdb.38640
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+function user.main():void;
+# pgexample components 
+    js := json.new("{\"f1\":1,\"f2\":true,\"f3\":\"Hi I''m \\\"Daisy\\\"\"}");
+    bk := json.keys(js);
+    io.print(bk);
+end main;
+[ "[\"f1\",\"f2\",\"f3\"]" ]
+
+# 23:31:57 >  
+# 23:31:57 >  "Done."
+# 23:31:57 >  
+
diff --git a/monetdb5/modules/atoms/json_atom.c 
b/monetdb5/modules/atoms/json_atom.c
--- a/monetdb5/modules/atoms/json_atom.c
+++ b/monetdb5/modules/atoms/json_atom.c
@@ -1029,6 +1029,79 @@ JSONnames(int *ret, json *js)
        GDKfree(result);
        return msg;
 }
+str
+JSONkeys(json *ret, json *js)
+{
+       char *namebegin, *nameend;
+       char *msg = MAL_SUCCEED;
+       char *result = NULL;
+       size_t l = 0, lim;
+       char *j = *js;
+
+       skipblancs;
+       if (*j != '{')
+               throw(MAL, "json.filter", "JSON object expected");
+
+       // the result is an array of values
+       result = (char *) GDKmalloc(BUFSIZ);
+       if (result == 0)
+               throw(MAL, "json.keys", MAL_MALLOC_FAIL);
+       lim = BUFSIZ;
+
+       result[0]='[';
+       result[1]= 0;
+       l=1;
+       for (j++; *j && *j != '}'; j++) {
+               skipblancs;
+               if (*j == '}')
+                       break;
+               if (*j != '"') {
+                       msg = createException(MAL, "json.keys", "Name 
expected");
+                       goto wrapup;
+               }
+               if( l > 1){
+                       result[l++] = ',';
+                       result[l] = 0;
+               }
+               namebegin = j + 1;
+               msg = JSONstringParser(j + 1, &j);
+               if (msg)
+                       goto wrapup;
+               nameend = j - 1;
+               if (l + (size_t)(nameend-namebegin) + 5 > lim){
+                       result = GDKrealloc(result, lim += BUFSIZ);
+                       if ( result == NULL)
+                               goto wrapup;
+               }
+               strncpy(result+l, namebegin-1, nameend - namebegin+2);
+               l += nameend - namebegin+2;
+               result[l] = 0;
+
+               skipblancs;
+               if (*j != ':') {
+                       msg = createException(MAL, "json.keys", "Value 
expected");
+                       goto wrapup;
+               }
+               j++;
+               skipblancs;
+               msg = JSONvalueParser(j, &j);
+               if (msg)
+                       goto wrapup;
+               skipblancs;
+               if (*j == '}')
+                       break;
+               if (*j != ',')
+                       msg = createException(MAL, "json.keys", "',' expected");
+       }
+       result[l]=']';
+       result[l+1]= 0;
+       l=1;
+       *ret = result;
+       return MAL_SUCCEED;
+wrapup:
+       GDKfree(result);
+       return msg;
+}
 
 static str
 JSONarrayvalues(int *ret, BAT *bn, char *j)
diff --git a/monetdb5/modules/atoms/json_atom.h 
b/monetdb5/modules/atoms/json_atom.h
--- a/monetdb5/modules/atoms/json_atom.h
+++ b/monetdb5/modules/atoms/json_atom.h
@@ -62,6 +62,7 @@ json_export str JSONnest(Client cntxt, M
 json_export str JSONnames(int *ret, json *j);
 json_export str JSONvalues(int *ret, json *j);
 json_export str JSONprelude(int *ret);
+json_export str JSONkeys(json *ret, json *arg);
 
 json_export str JSONrenderobject(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
 json_export str JSONrenderarray(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci);
diff --git a/monetdb5/modules/atoms/json_atom.mal 
b/monetdb5/modules/atoms/json_atom.mal
--- a/monetdb5/modules/atoms/json_atom.mal
+++ b/monetdb5/modules/atoms/json_atom.mal
@@ -103,6 +103,10 @@ pattern nest(k:bat[:oid,:dbl]):json
 address JSONnest
 comment "Combine the value list into a single json array object.";
 
+command keys(val:json):str
+address JSONkeys
+comment "Expands the outermost JSON object keys into an array.";
+
 command names(val:json):bat[:oid,:str]
 address JSONnames
 comment "Expands the outermost JSON object names.";
diff --git a/sql/test/json/Tests/miniexample.sql 
b/sql/test/json/Tests/miniexample.sql
--- a/sql/test/json/Tests/miniexample.sql
+++ b/sql/test/json/Tests/miniexample.sql
@@ -1,7 +1,5 @@
 
 --create type json external name json;
-create function json_filter(js json, nme string)
-returns json external name json.filter;
 
 create table minitable(j json);
 
@@ -37,4 +35,5 @@ select json_filter(j,'name'), json_filte
 select json_keys(j) from minitable;
 select json_names(j) from minitable;
 select json_values(j) from minitable;
+
 drop table minitable;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to