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 */
28typedef 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
38typedef struct JSON{
39 JSONterm *elm;
40 str error;
41 int size;
42 int free;
43} JSON;
44
45typedef str json;
46
47mal_export int TYPE_json;
48
49mal_export ssize_t JSONfromString(const char *src, size_t *len, json *x, bool external);
50mal_export ssize_t JSONtoString(str *s, size_t *len, const char *src, bool external);
51
52
53mal_export str JSONstr2json(json *ret, str *j);
54mal_export str JSONjson2str(str *ret, json *j);
55mal_export str JSONjson2text(str *ret, json *arg);
56mal_export str JSONjson2textSeparator(str *ret, json *arg, str *sep);
57mal_export str JSONjson2number(dbl *ret, json *arg);
58mal_export str JSONjson2integer(lng *ret, json *arg);
59
60mal_export str JSONfilter( json *ret, json *js, str *expr);
61mal_export str JSONfilterArray_bte(json *ret, json *j, bte *index);
62mal_export str JSONfilterArrayDefault_bte(json *ret, json *j, bte *index, str *other);
63mal_export str JSONfilterArray_sht(json *ret, json *j, sht *index);
64mal_export str JSONfilterArrayDefault_sht(json *ret, json *j, sht *index, str *other);
65mal_export str JSONfilterArray_int(json *ret, json *j, int *index);
66mal_export str JSONfilterArrayDefault_int(json *ret, json *j, int *index, str *other);
67mal_export str JSONfilterArray_lng(json *ret, json *j, lng *index);
68mal_export str JSONfilterArrayDefault_lng(json *ret, json *j, lng *index, str *other);
69#ifdef HAVE_HGE
70mal_export str JSONfilterArray_hge(json *ret, json *j, hge *index);
71mal_export str JSONfilterArrayDefault_hge(json *ret, json *j, hge *index, str *other);
72#endif
73
74mal_export str JSONisvalid(bit *ret, json *j);
75mal_export str JSONisobject(bit *ret, json *j);
76mal_export str JSONisarray(bit *ret, json *j);
77
78mal_export str JSONlength(int *ret, json *j);
79mal_export str JSONunfold(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
80mal_export str JSONfold(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
81mal_export str JSONkeyTable(bat *ret, json *j);
82mal_export str JSONvalueTable(bat *ret, json *j);
83mal_export str JSONkeyArray(json *ret, json *arg);
84mal_export str JSONvalueArray(json *ret, json *arg);
85
86mal_export str JSONtextString(str *ret, bat *bid);
87mal_export str JSONtextGrouped(bat *ret, bat *bid, bat *gid, bat *ext, bit *flg);
88mal_export str JSONdump(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
89mal_export str JSONprelude(void *ret);
90
91mal_export str JSONrenderobject(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
92mal_export str JSONrenderarray(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
93mal_export str JSONgroupStr(str *ret, const bat *bid);
94mal_export str JSONsubjsoncand(bat *retval, bat *bid, bat *gid, bat *eid, bat *id, bit *skip_nils);
95mal_export str JSONsubjson(bat *retval, bat *bid, bat *gid, bat *eid, bit *skipnils);
96#endif /* JSON_H */
97