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

Break the nested JSON structure


diffs (truncated from 620 to 300 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
@@ -18,4 +18,5 @@ xml100
 json00
 json01
 json02
+json03
 jsonrender
diff --git a/monetdb5/modules/atoms/Tests/json01.stable.out 
b/monetdb5/modules/atoms/Tests/json01.stable.out
--- a/monetdb5/modules/atoms/Tests/json01.stable.out
+++ b/monetdb5/modules/atoms/Tests/json01.stable.out
@@ -53,7 +53,7 @@ end main;
 [ "[]" ]
 [ "[1]" ]
 [ "[2]" ]
-[ "[3," ]
+[ "[1]" ]
 [ "[{\"f12\":3}]" ]
 [ "[{\"f12\":3}]" ]
 [ "[1]" ]
diff --git a/monetdb5/modules/atoms/Tests/json03.mal 
b/monetdb5/modules/atoms/Tests/json03.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/json03.mal
@@ -0,0 +1,10 @@
+js:= calc.json("[{ \"category\": \"reference\", \"author\": \"Nigel Rees\", 
\"title\": \"Sayings of the Century\", \"price\": 8.95 }, { \"category\": 
\"fiction\", \"author\": \"Evelyn Waugh\", \"title\": \"Sword of Honour\", 
\"price\": 12.99 }, { \"category\": \"fiction\", \"author\": \"Herman 
Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 
8.99 }, { \"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", 
\"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 
22.99 }, { \"color\": \"red\", \"price\": 19.95 }]");
+
+
+io.print(js);
+
+(k,v):= json.pairs(js);
+io.print(k,v);
+
+(o,k,v):= json.pairs(js);
+io.print(o,k,v);
diff --git a/monetdb5/modules/atoms/Tests/json03.stable.err 
b/monetdb5/modules/atoms/Tests/json03.stable.err
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/json03.stable.err
@@ -0,0 +1,29 @@
+stderr of test 'json03` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 19:03:08 >  
+# 19:03:08 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=38853" "--set" 
"mapi_usock=/var/tmp/mtest-430/.s.monetdb.38853" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms"
 "json03.mal"
+# 19:03:08 >  
+
+# 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 = 38853
+# cmdline opt  mapi_usock = /var/tmp/mtest-430/.s.monetdb.38853
+# cmdline opt  monet_prompt = 
+# cmdline opt  mal_listing = 2
+# cmdline opt  gdk_dbpath = 
/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms
+
+# 19:03:08 >  
+# 19:03:08 >  "Done."
+# 19:03:08 >  
+
diff --git a/monetdb5/modules/atoms/Tests/json03.stable.out 
b/monetdb5/modules/atoms/Tests/json03.stable.out
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/json03.stable.out
@@ -0,0 +1,82 @@
+stdout of test 'json03` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 19:03:08 >  
+# 19:03:08 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=38853" "--set" 
"mapi_usock=/var/tmp/mtest-430/.s.monetdb.38853" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch1/mk/current//Linux/var/MonetDB/mTests_monetdb5_modules_atoms"
 "json03.mal"
+# 19:03:08 >  
+
+# 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.629 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:38853/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-430/.s.monetdb.38853
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+function user.main():void;
+    js := calc.json("[{ \"category\": \"reference\", \"author\": \"Nigel 
Rees\", \"title\": \"Sayings of the Century\", \"price\": 8.95 }, { 
\"category\": \"fiction\", \"author\": \"Evelyn Waugh\", \"title\": \"Sword of 
Honour\", \"price\": 12.99 }, { \"category\": \"fiction\", \"author\": \"Herman 
Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 
8.99 }, { \"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", 
\"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 
22.99 }, { \"color\": \"red\", \"price\": 19.95 }]");
+    io.print(js);
+    (k,v) := json.pairs(js);
+    io.print(k,v);
+    (o,k,v) := json.pairs(js);
+    io.print(o,k,v);
+end main;
+[ "[{ \"category\": \"reference\", \"author\": \"Nigel Rees\", \"title\": 
\"Sayings of the Century\", \"price\": 8.95 }, { \"category\": \"fiction\", 
\"author\": \"Evelyn Waugh\", \"title\": \"Sword of Honour\", \"price\": 12.99 
}, { \"category\": \"fiction\", \"author\": \"Herman Melville\", \"title\": 
\"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 8.99 }, { \"category\": 
\"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the 
Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99 }, { \"color\": \"red\", 
\"price\": 19.95 }]" ]
+#---------------------------------------------------------#
+# h    t               t                                 # name
+# void str             json                              # type
+#---------------------------------------------------------#
+[ 0@0,   "category",     "\"reference\""                 ]
+[ 1@0,   "author",       "\"Nigel Rees\""                ]
+[ 2@0,   "title",        "\"Sayings of the Century\""    ]
+[ 3@0,   "price",        "8.95 "                         ]
+[ 4@0,   "category",     "\"fiction\""                   ]
+[ 5@0,   "author",       "\"Evelyn Waugh\""              ]
+[ 6@0,   "title",        "\"Sword of Honour\""           ]
+[ 7@0,   "price",        "12.99 "                        ]
+[ 8@0,   "category",     "\"fiction\""                   ]
+[ 9@0,   "author",       "\"Herman Melville\""           ]
+[ 10@0,          "title",        "\"Moby Dick\""                 ]
+[ 11@0,          "isbn",         "\"0-553-21311-3\""             ]
+[ 12@0,          "price",        "8.99 "                         ]
+[ 13@0,          "category",     "\"fiction\""                   ]
+[ 14@0,          "author",       "\"J. R. R. Tolkien\""          ]
+[ 15@0,          "title",        "\"The Lord of the Rings\""     ]
+[ 16@0,          "isbn",         "\"0-395-19395-8\""             ]
+[ 17@0,          "price",        "22.99 "                        ]
+[ 18@0,          "color",        "\"red\""                       ]
+[ 19@0,          "price",        "19.95 "                        ]
+#-----------------------------------------------------------------#
+# h    t       t               t                                 # name
+# void oid     str             json                              # type
+#-----------------------------------------------------------------#
+[ 0@0,   0@0,    "category",     "\"reference\""                 ]
+[ 1@0,   0@0,    "author",       "\"Nigel Rees\""                ]
+[ 2@0,   0@0,    "title",        "\"Sayings of the Century\""    ]
+[ 3@0,   0@0,    "price",        "8.95 "                         ]
+[ 4@0,   1@0,    "category",     "\"fiction\""                   ]
+[ 5@0,   1@0,    "author",       "\"Evelyn Waugh\""              ]
+[ 6@0,   1@0,    "title",        "\"Sword of Honour\""           ]
+[ 7@0,   1@0,    "price",        "12.99 "                        ]
+[ 8@0,   2@0,    "category",     "\"fiction\""                   ]
+[ 9@0,   2@0,    "author",       "\"Herman Melville\""           ]
+[ 10@0,          2@0,    "title",        "\"Moby Dick\""                 ]
+[ 11@0,          2@0,    "isbn",         "\"0-553-21311-3\""             ]
+[ 12@0,          2@0,    "price",        "8.99 "                         ]
+[ 13@0,          3@0,    "category",     "\"fiction\""                   ]
+[ 14@0,          3@0,    "author",       "\"J. R. R. Tolkien\""          ]
+[ 15@0,          3@0,    "title",        "\"The Lord of the Rings\""     ]
+[ 16@0,          3@0,    "isbn",         "\"0-395-19395-8\""             ]
+[ 17@0,          3@0,    "price",        "22.99 "                        ]
+[ 18@0,          4@0,    "color",        "\"red\""                       ]
+[ 19@0,          4@0,    "price",        "19.95 "                        ]
+
+# 19:03:08 >  
+# 19:03:08 >  "Done."
+# 19:03:08 >  
+
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
@@ -136,14 +136,6 @@ str JSONisarray(int *ret, json *j)
        return MAL_SUCCEED;
 }
 
-str JSONpairs(int *key, int *val, json *j)
-{
-       (void) key;
-       (void) val;
-       (void) j;
-       return MAL_SUCCEED;
-}
-
 str JSONprelude(int *ret)
 {
        (void) ret;
@@ -585,6 +577,249 @@ wrapup:;
        *ret = result;
        return msg;
 }
+
+str JSONpairs(int *key, int *val, json *js)
+{
+       BAT *bk, *bv;
+       char  *namebegin,*nameend;
+       char  *valuebegin,*valueend;
+       char *msg= MAL_SUCCEED;
+       char *result = NULL;
+       int l,lim, nesting=0;
+       char *j = *js;
+
+       bk = BATnew(TYPE_void,TYPE_str,64);
+       if ( bk == NULL)
+               throw(MAL,"json.names",MAL_MALLOC_FAIL);
+       BATseqbase(bk,0);
+       bk->hsorted = 1;
+       bk->hrevsorted = 0;
+       bk->H->nonil =1;
+       bk->tsorted = 1;
+       bk->trevsorted = 0;
+       bk->T->nonil = 1;
+
+       bv = BATnew(TYPE_void,TYPE_json,64);
+       if ( bv == NULL){
+               BBPreleaseref(bk->batCacheid);
+               throw(MAL,"json.names",MAL_MALLOC_FAIL);
+       }
+       BATseqbase(bv,0);
+       bv->hsorted = 1;
+       bv->hrevsorted = 0;
+       bv->H->nonil =1;
+       bv->tsorted = 1;
+       bv->trevsorted = 0;
+       bv->T->nonil = 1;
+
+       skipblancs;
+       while( *j == '['){
+               nesting++;
+               j++;
+       }
+       if ( *j != '{' )
+               throw(MAL,"json.filter","JSON object expected");
+       
+       // the result is an array of values
+       result = (char *) GDKmalloc(BUFSIZ);
+       if ( result == 0){
+               BBPreleaseref(bk->batCacheid);
+               BBPreleaseref(bv->batCacheid);
+               throw(MAL,"json.names",MAL_MALLOC_FAIL);
+       }
+       lim = BUFSIZ;
+
+       for( j++; *j && *j != '}'; j++){
+               skipblancs;
+               if (*j == ']'){
+                       break;
+               }
+               if (*j == '}')
+                       break;
+               if (*j != '"'){
+                       msg = createException(MAL,"json.names","Name expected");
+                       goto wrapup;
+               }
+               namebegin = j+1;
+               msg = JSONstringParser(j+1, &j);
+               if ( msg)
+                       goto wrapup;
+               nameend = j-1;
+               l = nameend - namebegin;
+               if ( l + 2 > lim )
+                       result = GDKrealloc(result, lim += BUFSIZ);
+               strncpy(result,namebegin,nameend-namebegin);
+               result[l] = 0;
+               BUNappend(bk, result, FALSE);
+
+               skipblancs;
+               if ( *j != ':'){
+                       msg = createException(MAL,"json.names","Value 
expected");
+                       goto wrapup;
+               }
+               j++;
+               skipblancs;
+               valuebegin = j;
+               msg = JSONvalueParser(j,&j);
+               if ( msg)
+                       goto wrapup;
+               valueend = j;
+               l= valueend - valuebegin;
+               if ( l + 2 > lim )
+                       result = GDKrealloc(result, lim += BUFSIZ);
+               strncpy(result,valuebegin,l);
+               result[l] = 0;
+               BUNappend(bv, result, FALSE);
+
+               skipblancs;
+               if (*j == '}'){
+                       if(nesting ){
+                               while (*j && *j != '{' && *j != ']') j++;
+                               if ( *j != '{') j--;
+                       } 
+                       continue;
+               }
+               if (*j != ',')
+                       msg = createException(MAL,"json.names","',' expected");
+       }
+wrapup:;
+       BBPkeepref(*key= bk->batCacheid);
+       BBPkeepref(*val= bv->batCacheid);
+       GDKfree(result);
+       return msg;
+}
+
+str JSONpairsGrouped(int *grp, int *key, int *val, json *js)
+{
+       BAT *bk, *bv, *bg;
+       char  *namebegin,*nameend;
+       char  *valuebegin,*valueend;
+       char *msg= MAL_SUCCEED;
+       char *result = NULL;
+       int l,lim, nesting=0;
+       char *j = *js;
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to