Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
buffer_api/06-iterators.c
1/*
2 Lite³: A JSON-Compatible Zero-Copy Serialization Format
3
4 Copyright © 2025 Elias de Jong <elias@fastserial.com>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23
24 __ __________________ ____
25 _ ___ ___/ /___(_)_/ /_______|_ /
26 _ _____/ / __/ /_ __/ _ \_/_ <
27 ___ __/ /___/ / / /_ / __/____/
28 /_____/_/ \__/ \___/
29*/
30#include <stdio.h>
31#include <string.h>
32#include <stdbool.h>
33
34#include "lite3.h"
35
36
37static unsigned char buf[1024];
38
39#define NAME_COUNT 6
40
41const char names[NAME_COUNT][10] = {
42 "Boris",
43 "John",
44 "Olivia",
45 "Tanya",
46 "Paul",
47 "Sarah",
48};
49
50int main() {
51 size_t buflen = 0;
52 size_t bufsz = sizeof(buf);
53
54 // Build array
55 if (lite3_init_arr(buf, &buflen, bufsz) < 0) {
56 perror("Failed to initialize array");
57 return 1;
58 }
59 for (int i = 0; i < NAME_COUNT; i++) {
60 size_t obj_ofs;
61 if (lite3_arr_append_obj(buf, &buflen, 0, bufsz, &obj_ofs) < 0
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
64 || lite3_set_null(buf, &buflen, obj_ofs, bufsz, "benefits") < 0
65 || lite3_set_str(buf, &buflen, obj_ofs, bufsz, "name", names[i]) < 0) {
66 perror("Failed to build array");
67 return 1;
68 }
69 }
70 if (lite3_json_print(buf, buflen, 0) < 0) { // Print Lite³ as JSON
71 perror("Failed to print JSON");
72 return 1;
73 }
74
75 // Iterate over array objects
76 lite3_iter iter;
77 if (lite3_iter_create(buf, buflen, 0, &iter) < 0) {
78 perror("Failed to create iterator");
79 return 1;
80 }
81 size_t val_ofs;
82 while (lite3_iter_next(buf, buflen, &iter, NULL, &val_ofs) == LITE3_ITER_ITEM) {
83 int64_t id;
84 bool vip_member;
85 bool benefits = !lite3_is_null(buf, buflen, val_ofs, "benefits");
86 lite3_str name;
87 if (lite3_get_i64(buf, buflen, val_ofs, "id", &id) < 0
88 || lite3_get_bool(buf, buflen, val_ofs, "vip_member", &vip_member) < 0
89 || lite3_get_str(buf, buflen, val_ofs, "name", &name) < 0) {
90 perror("Failed to get object");
91 return 1;
92 }
93 printf("id: %li\tname: %s\tvip_member: %s\tbenefits: %s\n",
94 id,
95 LITE3_STR(buf, name),
96 vip_member ? "true" : "false",
97 benefits ? "yes" : "no"
98 );
99 }
100
101 // Iterate over object key-value pairs
102 lite3_iter iter_2;
103 if (lite3_iter_create(buf, buflen, val_ofs, &iter_2) < 0) {
104 perror("Failed to create iterator");
105 return 1;
106 }
107 printf("\nObject keys:\n");
108 lite3_str key;
109 size_t val_ofs_2;
110 while (lite3_iter_next(buf, buflen, &iter_2, &key, &val_ofs_2) == LITE3_ITER_ITEM) {
111
112 lite3_val *val = (lite3_val *)(buf + val_ofs_2);
113 printf("key: %s\tvalue: ", LITE3_STR(buf, key));
114
115 switch (val->type) {
116 case LITE3_TYPE_I64:
117 printf("%li\n", lite3_val_i64(val));
118 break;
119 case LITE3_TYPE_BOOL:
120 printf("%s\n", lite3_val_bool(val) ? "true" : "false");
121 break;
122 case LITE3_TYPE_NULL:
123 printf("null\n");
124 break;
126 printf("%s\n", lite3_val_str(val));
127 break;
128 default:
129 fprintf(stderr, "Invalid object value type\n");
130 return 1;
131 }
132 }
133
134 return 0;
135}
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.
Definition lite3.h:1366
#define lite3_get_bool(buf, buflen, ofs, key, out)
Get boolean value by key.
Definition lite3.h:2139
#define lite3_get_str(buf, buflen, ofs, key, out)
Get string value by key.
Definition lite3.h:2284
#define lite3_get_i64(buf, buflen, ofs, key, out)
Get integer value by key.
Definition lite3.h:2174
#define LITE3_ITER_ITEM
Return value of lite3_iter_next(); iterator produced an item, continue;.
Definition lite3.h:2616
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.
Definition lite3.c:333
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.
Definition lite3.h:2644
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.
Definition lite3.h:821
#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value)
Set boolean in object.
Definition lite3.h:852
#define lite3_set_str(buf, inout_buflen, ofs, bufsz, key, str)
Set string in object.
Definition lite3.h:986
#define lite3_set_i64(buf, inout_buflen, ofs, bufsz, key, value)
Set integer in object.
Definition lite3.h:884
#define LITE3_STR(buf, val)
Generational pointer / safe access wrapper.
Definition lite3.h:666
@ LITE3_TYPE_STRING
maps to 'string' type in JSON
Definition lite3.h:516
@ LITE3_TYPE_BOOL
maps to 'boolean' type in JSON; underlying datatype: bool
Definition lite3.h:512
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
Definition lite3.h:513
@ LITE3_TYPE_NULL
maps to 'null' type in JSON
Definition lite3.h:511
#define lite3_is_null(buf, buflen, ofs, key)
Find value by key and test for null type.
Definition lite3.h:1874
Lite³ Buffer API Header.
Struct containing iterator state.
Definition lite3.h:2626
Struct holding a reference to a string inside a Lite³ buffer.
Definition lite3.h:601
Struct representing a value inside a Lite³ buffer.
Definition lite3.h:531