37static unsigned char buf[1024];
41const char names[NAME_COUNT][10] = {
52 size_t bufsz =
sizeof(buf);
55 if (lite3_init_arr(buf, &buflen, bufsz) < 0) {
56 perror(
"Failed to initialize array");
59 for (
int i = 0; i < NAME_COUNT; i++) {
62 ||
lite3_set_i64(buf, &buflen, obj_ofs, bufsz,
"id", (int64_t)i) < 0
63 ||
lite3_set_bool(buf, &buflen, obj_ofs, bufsz,
"vip_member",
false) < 0
65 ||
lite3_set_str(buf, &buflen, obj_ofs, bufsz,
"name", names[i]) < 0) {
66 perror(
"Failed to build array");
71 perror(
"Failed to print JSON");
78 perror(
"Failed to create iterator");
85 bool benefits = !
lite3_is_null(buf, buflen, val_ofs,
"benefits");
88 ||
lite3_get_bool(buf, buflen, val_ofs,
"vip_member", &vip_member) < 0
90 perror(
"Failed to get object");
93 printf(
"id: %li\tname: %s\tvip_member: %s\tbenefits: %s\n",
96 vip_member ?
"true" :
"false",
97 benefits ?
"yes" :
"no"
104 perror(
"Failed to create iterator");
107 printf(
"\nObject keys:\n");
113 printf(
"key: %s\tvalue: ",
LITE3_STR(buf, key));
117 printf(
"%li\n", lite3_val_i64(val));
120 printf(
"%s\n", lite3_val_bool(val) ?
"true" :
"false");
126 printf(
"%s\n", lite3_val_str(val));
129 fprintf(stderr,
"Invalid object value type\n");
static int lite3_arr_append_obj(unsigned char *buf, size_t *__restrict inout_buflen, size_t ofs, size_t bufsz, size_t *__restrict out_ofs)
Append object to array.
#define lite3_get_bool(buf, buflen, ofs, key, out)
Get boolean value by key.
#define lite3_get_str(buf, buflen, ofs, key, out)
Get string value by key.
#define lite3_get_i64(buf, buflen, ofs, key, out)
Get integer value by key.
#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.
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
#define lite3_set_null(buf, inout_buflen, ofs, bufsz, key)
Set null in object.
#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value)
Set boolean in object.
#define lite3_set_str(buf, inout_buflen, ofs, bufsz, key, str)
Set string in object.
#define lite3_set_i64(buf, inout_buflen, ofs, bufsz, key, value)
Set integer in object.
#define LITE3_STR(buf, val)
Generational pointer / safe access wrapper.
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
#define lite3_is_null(buf, buflen, ofs, key)
Find value by key and test for null type.
Struct containing iterator state.
Struct holding a reference to a string inside a Lite³ buffer.
Struct representing a value inside a Lite³ buffer.