40#include "yyjson/yyjson.h"
41#include "nibble_base64/base64.h"
60int _lite3_json_enc_obj(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t nesting_depth, yyjson_mut_doc *doc, yyjson_mut_val *coll);
61int _lite3_json_enc_arr(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t nesting_depth, yyjson_mut_doc *doc, yyjson_mut_val *coll);
63int _lite3_json_enc_switch(
const unsigned char *buf,
size_t buflen,
size_t nesting_depth, yyjson_mut_doc *doc, yyjson_mut_val **yy_val,
lite3_val *val)
68 *yy_val = yyjson_mut_null(doc);
71 *yy_val = yyjson_mut_bool(doc, lite3_val_bool(val));
74 *yy_val = yyjson_mut_sint(doc, lite3_val_i64(val));
77 *yy_val = yyjson_mut_double(doc, lite3_val_f64(val));
81 const u8 *bytes = lite3_val_bytes(val, &bytes_len);
83 char *b64 = nibble_base64(bytes, (
int)bytes_len, &b64_len);
85 LITE3_PRINT_ERROR(
"FAILED TO CONVERT BYTES TO BASE64\n");
97 *yy_val = yyjson_mut_strncpy(doc, b64, (
size_t)b64_len);
107 *yy_val = yyjson_mut_strn(doc, str, str_len);
110 *yy_val = yyjson_mut_obj(doc);
111 size_t obj_ofs = (size_t)((u8 *)val - buf);
112 if (_lite3_json_enc_obj(buf, buflen, obj_ofs, nesting_depth, doc, *yy_val) < 0)
116 *yy_val = yyjson_mut_arr(doc);
117 size_t arr_ofs = (size_t)((u8 *)val - buf);
118 if (_lite3_json_enc_arr(buf, buflen, arr_ofs, nesting_depth, doc, *yy_val) < 0)
122 LITE3_PRINT_ERROR(
"FAILED TO BUILD JSON DOCUMENT: VALUE TYPE INVALID\n");
134int _lite3_json_enc_obj(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t nesting_depth, yyjson_mut_doc *doc, yyjson_mut_val *coll)
137 LITE3_PRINT_ERROR(
"FAILED TO BUILD JSON DOCUMENT: nesting_depth > LITE3_JSON_NESTING_DEPTH_MAX\n");
148 yyjson_mut_val *yy_val;
151 if ((ret = _lite3_json_enc_switch(buf, buflen, nesting_depth, doc, &yy_val, val)) < 0)
153 if (!yyjson_mut_obj_add(coll, yyjson_mut_str(doc,
LITE3_STR(buf, key)), yy_val)) {
154 LITE3_PRINT_ERROR(
"FAILED TO BUILD JSON DOCUMENT: ADDING KEY-VALUE PAIR FAILED\n");
167int _lite3_json_enc_arr(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t nesting_depth, yyjson_mut_doc *doc, yyjson_mut_val *coll)
170 LITE3_PRINT_ERROR(
"FAILED TO BUILD JSON DOCUMENT: nesting_depth > LITE3_JSON_NESTING_DEPTH_MAX\n");
180 yyjson_mut_val *yy_val;
183 if ((ret = _lite3_json_enc_switch(buf, buflen, nesting_depth, doc, &yy_val, val)) < 0)
185 if (!yyjson_mut_arr_append(coll, yy_val)) {
186 LITE3_PRINT_ERROR(
"FAILED TO BUILD JSON DOCUMENT: APPENDING ARRAY ELEMENT FAILED\n");
194yyjson_mut_doc *_lite3_json_enc_doc(
const unsigned char *buf,
size_t buflen,
size_t ofs)
196 if (_lite3_verify_get(buf, buflen, ofs) < 0)
198 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
201 yyjson_mut_val *root;
202 switch (*(buf + ofs)) {
204 root = yyjson_mut_obj(doc);
205 if (_lite3_json_enc_obj(buf, buflen, ofs, 0, doc, root) < 0)
209 root = yyjson_mut_arr(doc);
210 if (_lite3_json_enc_arr(buf, buflen, ofs, 0, doc, root) < 0)
214 LITE3_PRINT_ERROR(
"INVALID ARGUMENT: EXPECTING ARRAY OR OBJECT TYPE\n");
218 yyjson_mut_doc_set_root(doc, root);
221 yyjson_mut_doc_free(doc);
227 yyjson_mut_doc *doc = _lite3_json_enc_doc(buf, buflen, ofs);
231 yyjson_write_err err;
232 char *json = yyjson_mut_write_opts(doc, YYJSON_WRITE_PRETTY, NULL, &len, &err);
233 yyjson_mut_doc_free(doc);
236 LITE3_PRINT_ERROR(
"FAILED TO WRITE JSON\tyyjson error code: %u msg:%s\n", err.code, err.msg);
240 fwrite(json, 1, len, stdout);
246char *
lite3_json_enc(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *restrict out_len)
248 yyjson_mut_doc *doc = _lite3_json_enc_doc(buf, buflen, ofs);
251 yyjson_write_err err;
252 char *json = yyjson_mut_write_opts(doc, YYJSON_WRITE_NOFLAG, NULL, out_len, &err);
253 yyjson_mut_doc_free(doc);
255 LITE3_PRINT_ERROR(
"FAILED TO WRITE JSON\tyyjson error code: %u msg:%s\n", err.code, err.msg);
262char *
lite3_json_enc_pretty(
const unsigned char *buf,
size_t buflen,
size_t ofs,
size_t *restrict out_len)
264 yyjson_mut_doc *doc = _lite3_json_enc_doc(buf, buflen, ofs);
267 yyjson_write_err err;
268 char *json = yyjson_mut_write_opts(doc, YYJSON_WRITE_PRETTY, NULL, out_len, &err);
269 yyjson_mut_doc_free(doc);
271 LITE3_PRINT_ERROR(
"FAILED TO WRITE JSON\tyyjson error code: %u msg:%s\n", err.code, err.msg);
278int64_t
lite3_json_enc_buf(
const unsigned char *buf,
size_t buflen,
size_t ofs,
char *restrict json_buf,
size_t json_bufsz)
280 yyjson_mut_doc *doc = _lite3_json_enc_doc(buf, buflen, ofs);
283 yyjson_write_err err;
284 size_t ret = yyjson_mut_write_buf(json_buf, json_bufsz, doc, YYJSON_WRITE_NOFLAG, &err);
285 assert(ret <= INT64_MAX);
286 yyjson_mut_doc_free(doc);
288 LITE3_PRINT_ERROR(
"FAILED TO WRITE JSON\tyyjson error code: %u msg:%s\n", err.code, err.msg);
295int64_t lite3_json_enc_buf_pretty(
const unsigned char *buf,
size_t buflen,
size_t ofs,
char *restrict json_buf,
size_t json_bufsz)
297 yyjson_mut_doc *doc = _lite3_json_enc_doc(buf, buflen, ofs);
300 yyjson_write_err err;
301 size_t ret = yyjson_mut_write_buf(json_buf, json_bufsz, doc, YYJSON_WRITE_PRETTY, &err);
302 assert(ret <= INT64_MAX);
303 yyjson_mut_doc_free(doc);
305 LITE3_PRINT_ERROR(
"FAILED TO WRITE JSON\tyyjson error code: %u msg:%s\n", err.code, err.msg);
#define LITE3_JSON_NESTING_DEPTH_MAX
Maximum nesting limit for JSON documents being encoded or decoded.
#define LITE3_ITER_ITEM
Return value of lite3_iter_next(); iterator produced an item, continue;.
int lite3_iter_next(const unsigned char *buf, size_t buflen, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
static int lite3_iter_create(const unsigned char *buf, size_t buflen, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
char * lite3_json_enc_pretty(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
int64_t lite3_json_enc_buf(const unsigned char *buf, size_t buflen, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
char * lite3_json_enc(const unsigned char *buf, size_t buflen, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON string.
lite3_type
enum containing all Lite³ types
#define LITE3_STR(buf, val)
Generational pointer / safe access wrapper.
@ LITE3_TYPE_ARRAY
maps to 'array' type in JSON
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
@ LITE3_TYPE_BYTES
coverted to base64 string in JSON
@ LITE3_TYPE_F64
maps to 'number' type in JSON; underlying datatype: double
@ LITE3_TYPE_OBJECT
maps to 'object' type in JSON
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
static const char * lite3_val_str_n(lite3_val *val, size_t *out_len)
static enum lite3_type lite3_val_type(lite3_val *val)
Returns the value type of *val
Struct containing iterator state.
Struct holding a reference to a string inside a Lite³ buffer.
Struct representing a value inside a Lite³ buffer.