| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #ifndef JSON_H |
| 10 | #define JSON_H |
| 11 | |
| 12 | #include "gdk.h" |
| 13 | #include "mal.h" |
| 14 | #include "mal_client.h" |
| 15 | #include "mal_instruction.h" |
| 16 | #include "mal_exception.h" |
| 17 | |
| 18 | #define JSON_OBJECT 1 |
| 19 | #define JSON_ARRAY 2 |
| 20 | #define JSON_ELEMENT 3 |
| 21 | #define JSON_VALUE 4 |
| 22 | #define JSON_STRING 5 |
| 23 | #define JSON_NUMBER 6 |
| 24 | #define JSON_BOOL 7 |
| 25 | #define JSON_NULL 8 |
| 26 | |
| 27 | /* The JSON index structure is meant for short lived versions */ |
| 28 | typedef struct JSONterm { |
| 29 | short kind; |
| 30 | char *name; /* exclude the quotes */ |
| 31 | size_t namelen; |
| 32 | const char *value; /* start of string rep */ |
| 33 | size_t valuelen; |
| 34 | int child, next, tail; /* next offsets allow you to walk array/object chains and append quickly */ |
| 35 | /* An array or object item has a number of components */ |
| 36 | } JSONterm; |
| 37 | |
| 38 | typedef struct JSON{ |
| 39 | JSONterm *elm; |
| 40 | str error; |
| 41 | int size; |
| 42 | int free; |
| 43 | } JSON; |
| 44 | |
| 45 | typedef str json; |
| 46 | |
| 47 | mal_export int TYPE_json; |
| 48 | |
| 49 | mal_export ssize_t JSONfromString(const char *src, size_t *len, json *x, bool external); |
| 50 | mal_export ssize_t JSONtoString(str *s, size_t *len, const char *src, bool external); |
| 51 | |
| 52 | |
| 53 | mal_export str JSONstr2json(json *ret, str *j); |
| 54 | mal_export str JSONjson2str(str *ret, json *j); |
| 55 | mal_export str JSONjson2text(str *ret, json *arg); |
| 56 | mal_export str JSONjson2textSeparator(str *ret, json *arg, str *sep); |
| 57 | mal_export str JSONjson2number(dbl *ret, json *arg); |
| 58 | mal_export str JSONjson2integer(lng *ret, json *arg); |
| 59 | |
| 60 | mal_export str JSONfilter( json *ret, json *js, str *expr); |
| 61 | mal_export str JSONfilterArray_bte(json *ret, json *j, bte *index); |
| 62 | mal_export str JSONfilterArrayDefault_bte(json *ret, json *j, bte *index, str *other); |
| 63 | mal_export str JSONfilterArray_sht(json *ret, json *j, sht *index); |
| 64 | mal_export str JSONfilterArrayDefault_sht(json *ret, json *j, sht *index, str *other); |
| 65 | mal_export str JSONfilterArray_int(json *ret, json *j, int *index); |
| 66 | mal_export str JSONfilterArrayDefault_int(json *ret, json *j, int *index, str *other); |
| 67 | mal_export str JSONfilterArray_lng(json *ret, json *j, lng *index); |
| 68 | mal_export str JSONfilterArrayDefault_lng(json *ret, json *j, lng *index, str *other); |
| 69 | #ifdef HAVE_HGE |
| 70 | mal_export str JSONfilterArray_hge(json *ret, json *j, hge *index); |
| 71 | mal_export str JSONfilterArrayDefault_hge(json *ret, json *j, hge *index, str *other); |
| 72 | #endif |
| 73 | |
| 74 | mal_export str JSONisvalid(bit *ret, json *j); |
| 75 | mal_export str JSONisobject(bit *ret, json *j); |
| 76 | mal_export str JSONisarray(bit *ret, json *j); |
| 77 | |
| 78 | mal_export str JSONlength(int *ret, json *j); |
| 79 | mal_export str JSONunfold(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci); |
| 80 | mal_export str JSONfold(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci); |
| 81 | mal_export str JSONkeyTable(bat *ret, json *j); |
| 82 | mal_export str JSONvalueTable(bat *ret, json *j); |
| 83 | mal_export str JSONkeyArray(json *ret, json *arg); |
| 84 | mal_export str JSONvalueArray(json *ret, json *arg); |
| 85 | |
| 86 | mal_export str JSONtextString(str *ret, bat *bid); |
| 87 | mal_export str JSONtextGrouped(bat *ret, bat *bid, bat *gid, bat *ext, bit *flg); |
| 88 | mal_export str JSONdump(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci); |
| 89 | mal_export str JSONprelude(void *ret); |
| 90 | |
| 91 | mal_export str JSONrenderobject(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci); |
| 92 | mal_export str JSONrenderarray(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci); |
| 93 | mal_export str JSONgroupStr(str *ret, const bat *bid); |
| 94 | mal_export str JSONsubjsoncand(bat *retval, bat *bid, bat *gid, bat *eid, bat *id, bit *skip_nils); |
| 95 | mal_export str JSONsubjson(bat *retval, bat *bid, bat *gid, bat *eid, bit *skipnils); |
| 96 | #endif /* JSON_H */ |
| 97 | |