Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
context_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_context_api.h"
35
36
37#define NAME_COUNT 6
38
39const char names[NAME_COUNT][10] = {
40 "Boris",
41 "John",
42 "Olivia",
43 "Tanya",
44 "Paul",
45 "Sarah",
46};
47
48int main() {
50 if (!ctx) {
51 perror("Failed to create lite3_ctx *ctx");
52 return 1;
53 }
54
55 // Build array
56 if (lite3_ctx_init_arr(ctx) < 0) {
57 perror("Failed to initialize array");
58 return 1;
59 }
60 for (int i = 0; i < NAME_COUNT; i++) {
61 size_t obj_ofs;
62 if (lite3_ctx_arr_append_obj(ctx, 0, &obj_ofs) < 0
63 || lite3_ctx_set_i64(ctx, obj_ofs, "id", (int64_t)i) < 0
64 || lite3_ctx_set_bool(ctx, obj_ofs, "vip_member", false)< 0
65 || lite3_ctx_set_null(ctx, obj_ofs, "benefits") < 0
66 || lite3_ctx_set_str(ctx, obj_ofs, "name", names[i]) < 0) {
67 perror("Failed to build array");
68 return 1;
69 }
70 }
71 if (lite3_ctx_json_print(ctx, 0) < 0) { // Print Lite³ as JSON
72 perror("Failed to print JSON");
73 return 1;
74 }
75
76 // Iterate over array objects
77 lite3_iter iter;
78 if (lite3_ctx_iter_create(ctx, 0, &iter) < 0) {
79 perror("Failed to create iterator");
80 return 1;
81 }
82 size_t val_ofs;
83 while (lite3_ctx_iter_next(ctx, &iter, NULL, &val_ofs) == LITE3_ITER_ITEM) {
84 int64_t id;
85 bool vip_member;
86 bool benefits = !lite3_ctx_is_null(ctx, val_ofs, "benefits");
87 lite3_str name;
88 if (lite3_ctx_get_i64(ctx, val_ofs, "id", &id) < 0
89 || lite3_ctx_get_bool(ctx, val_ofs, "vip_member", &vip_member) < 0
90 || lite3_ctx_get_str(ctx, val_ofs, "name", &name) < 0) {
91 perror("Failed to get object");
92 return 1;
93 }
94 printf("id: %li\tname: %s\tvip_member: %s\tbenefits: %s\n",
95 id,
96 LITE3_STR(ctx->buf, name),
97 vip_member ? "true" : "false",
98 benefits ? "yes" : "no"
99 );
100 }
101
102 // Iterate over object key-value pairs
103 lite3_iter iter_2;
104 if (lite3_ctx_iter_create(ctx, val_ofs, &iter_2) < 0) {
105 perror("Failed to create iterator");
106 return 1;
107 }
108 printf("\nObject keys:\n");
109 lite3_str key;
110 size_t val_ofs_2;
111 while (lite3_ctx_iter_next(ctx, &iter_2, &key, &val_ofs_2) == LITE3_ITER_ITEM) {
112
113 lite3_val *val = (lite3_val *)(ctx->buf + val_ofs_2);
114 printf("key: %s\tvalue: ", LITE3_STR(ctx->buf, key));
115
116 switch (val->type) {
117 case LITE3_TYPE_I64:
118 printf("%li\n", lite3_val_i64(val));
119 break;
120 case LITE3_TYPE_BOOL:
121 printf("%s\n", lite3_val_bool(val) ? "true" : "false");
122 break;
123 case LITE3_TYPE_NULL:
124 printf("null\n");
125 break;
127 printf("%s\n", lite3_val_str(val));
128 break;
129 default:
130 fprintf(stderr, "Invalid object value type\n");
131 return 1;
132 }
133 }
134
136 return 0;
137}
static int lite3_ctx_arr_append_obj(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_ofs)
Append object to array.
#define lite3_ctx_get_str(ctx, ofs, key, out)
Get string value by key.
#define lite3_ctx_get_bool(ctx, ofs, key, out)
Get boolean value by key.
#define lite3_ctx_get_i64(ctx, ofs, key, out)
Get integer value by key.
static int lite3_ctx_init_arr(lite3_ctx *ctx)
Initialize a Lite³ context as an array.
static int lite3_ctx_iter_create(lite3_ctx *ctx, size_t ofs, lite3_iter *out)
Create a lite3 iterator for the given object or array.
static int lite3_ctx_iter_next(lite3_ctx *ctx, lite3_iter *iter, lite3_str *out_key, size_t *out_val_ofs)
Get the next item from a lite3 iterator.
static int lite3_ctx_json_print(lite3_ctx *ctx, size_t ofs)
Print Lite³ buffer as JSON to stdout
static lite3_ctx * lite3_ctx_create(void)
Create context with minimum size.
void lite3_ctx_destroy(lite3_ctx *ctx)
Destroy context.
Definition ctx_api.c:229
#define lite3_ctx_set_i64(ctx, ofs, key, value)
Set integer in object.
#define lite3_ctx_set_null(ctx, ofs, key)
Set null in object.
#define lite3_ctx_set_bool(ctx, ofs, key, value)
Set boolean in object.
#define lite3_ctx_set_str(ctx, ofs, key, str)
Set string in object.
#define lite3_ctx_is_null(ctx, ofs, key)
Find value by key and test for null type.
#define LITE3_ITER_ITEM
Return value of lite3_iter_next(); iterator produced an item, continue;.
Definition lite3.h:2616
#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
Lite³ Context API Header.
Lite³ context struct.
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