Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
Iterators

Introduction

The previous guide was about arrays. This guide will show iterators.

Iterators are an essential functionality for many kinds of applications. Examples include:

  1. range queries
  2. filtering
  3. validation
  4. aggregation
  5. discovering unknown data

Iterators can be created for both objects and arrays to traverse all entries or elements inside. The process consists of two steps:

  1. creating the iterator using lite3_ctx_iter_create()
  2. looping over the items using lite3_ctx_iter_next()

A lite3_iter struct (56 bytes) is used to store the state of an iterator. It can be stack- or heap-allocated.

Example Code

This guide is based on an example inside the Lite³ repository found in examples/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

Output:

[
{
"id": 0,
"benefits": null,
"name": "Boris",
"vip_member": false
},
{
"id": 1,
"benefits": null,
"name": "John",
"vip_member": false
},
{
"id": 2,
"benefits": null,
"name": "Olivia",
"vip_member": false
},
{
"id": 3,
"benefits": null,
"name": "Tanya",
"vip_member": false
},
{
"id": 4,
"benefits": null,
"name": "Paul",
"vip_member": false
},
{
"id": 5,
"benefits": null,
"name": "Sarah",
"vip_member": false
}
]
id: 0 name: Boris vip_member: false benefits: no
id: 1 name: John vip_member: false benefits: no
id: 2 name: Olivia vip_member: false benefits: no
id: 3 name: Tanya vip_member: false benefits: no
id: 4 name: Paul vip_member: false benefits: no
id: 5 name: Sarah vip_member: false benefits: no
Object keys:
key: id value: 5
key: benefits value: null
key: name value: Sarah
key: vip_member value: false

Explanation

We will walk through the example code step-by-step, explaining the use of Lite³ library functions.

Lite³ messages are just bytes, stored contiguously inside a buffer. If you want to allocate these messages inside your own custom allocators, you can using the Buffer API. However in this guide, we will be using the Context API so that memory is managed automatically.

Note that Lite³ is a binary format, but the examples print message data as JSON to stdout for better readability.

Building the message

As explained in the first guide, we use contexts to store Lite³ buffers (see: Context API):

if (!ctx) {
perror("Failed to create lite3_ctx *ctx");
return 1;
}

We will first construct an array of objects:

// Build array
if (lite3_ctx_init_arr(ctx) < 0) {
perror("Failed to initialize array");
return 1;
}
for (int i = 0; i < NAME_COUNT; i++) {
size_t obj_ofs;
if (lite3_ctx_arr_append_obj(ctx, 0, &obj_ofs) < 0
|| lite3_ctx_set_i64(ctx, obj_ofs, "id", (int64_t)i) < 0
|| lite3_ctx_set_bool(ctx, obj_ofs, "vip_member", false)< 0
|| lite3_ctx_set_null(ctx, obj_ofs, "benefits") < 0
|| lite3_ctx_set_str(ctx, obj_ofs, "name", names[i]) < 0) {
perror("Failed to build array");
return 1;
}
}

We then convert Lite³ to JSON to see what this data looks like:

if (lite3_ctx_json_print(ctx, 0) < 0) { // Print Lite³ as JSON
perror("Failed to print JSON");
return 1;
}

Output:

[
{
"id": 0,
"benefits": null,
"name": "Boris",
"vip_member": false
},
{
"id": 1,
"benefits": null,
"name": "John",
"vip_member": false
},
{
"id": 2,
"benefits": null,
"name": "Olivia",
"vip_member": false
},
{
"id": 3,
"benefits": null,
"name": "Tanya",
"vip_member": false
},
{
"id": 4,
"benefits": null,
"name": "Paul",
"vip_member": false
},
{
"id": 5,
"benefits": null,
"name": "Sarah",
"vip_member": false
}
]

Iterator creation

The first step is to create an iterator object like so:

if (lite3_ctx_iter_create(ctx, 0, &iter) < 0) {
perror("Failed to create iterator");
return 1;
}

Here we do a simple stack allocation for the lite3_iter struct, which has a default size of 56 bytes. The lite3_ctx_iter_create() will initialize initial parameters to make it ready for traversal.

Note
It is also possible to heap allocate the lite3_iter struct like so:
lite3_iter *iter = malloc(sizeof(lite3_iter));
free(iter);
Perhaps the lifetime should outlive the current scope, or you are working with a small stack (sometimes you gotta work with what you have 😔).

Iterating over arrays

Now the actual traversal. We print out the values for each object:

size_t val_ofs;
while (lite3_ctx_iter_next(ctx, &iter, NULL, &val_ofs) == LITE3_ITER_ITEM) {
int64_t id;
bool vip_member;
bool benefits = !lite3_ctx_is_null(ctx, val_ofs, "benefits");
lite3_str name;
if (lite3_ctx_get_i64(ctx, val_ofs, "id", &id) < 0
|| lite3_ctx_get_bool(ctx, val_ofs, "vip_member", &vip_member) < 0
|| lite3_ctx_get_str(ctx, val_ofs, "name", &name) < 0) {
perror("Failed to get object");
return 1;
}
printf("id: %li\tname: %s\tvip_member: %s\tbenefits: %s\n",
id,
LITE3_STR(ctx->buf, name),
vip_member ? "true" : "false",
benefits ? "yes" : "no"
);
}

Output:

id: 0 name: Boris vip_member: false benefits: no
id: 1 name: John vip_member: false benefits: no
id: 2 name: Olivia vip_member: false benefits: no
id: 3 name: Tanya vip_member: false benefits: no
id: 4 name: Paul vip_member: false benefits: no
id: 5 name: Sarah vip_member: false benefits: no

There are three possible return values for the lite3_ctx_iter_next() function:

  • LITE3_ITER_ITEM (== 1) on item produced
  • LITE3_ITER_DONE (== 0) on success (no more items)
  • < 0 (error)

In the loop we continuously check for LITE3_ITER_ITEM. The iterator will keep returning this everytime an item is produced successfully, or until there are no more items (LITE3_ITER_DONE). If an error occurs, the return value will be negative (< 0).

Warning
In this example we do not actually check for the error. However it is recommended to do so:
size_t val_ofs;
int ret;
while ((ret = lite3_ctx_iter_next(ctx, &iter, NULL, &val_ofs)) == LITE3_ITER_ITEM) {
// ...
}
if (ret < 0) {
perror("Failed to get iterator item");
return 1;
}
If a message is corrupted, invalid or other wise damaged, it is important to know as early as possible. Otherwise, your iterator could unexpectedly stop halfway and it might 'appear to work' for long enough until your problems enter production.
size_t val_ofs;
lite3_ctx_iter_next(ctx, &iter, NULL, &val_ofs);

Remember that we are iterating over a series of objects inside an array. The 1st parameter is the current context storing our message, the 2nd is a pointer to the iterator object. The 3rd parameter is passed is NULL, as this is an out parameter for an object key.

Object key? But aren't we iterating over an array? Notice that iterator functions do not have 'arr' inside the name. This is because they work on objects as well as arrays. Since arrays do not have keys, this parameter is useless and we just pass NULL.

On each iteration, the offset of the current value (element) is written back to val_ofs (4th parameter). This value offset is very important. It is the exact offset inside the Lite³ buffer at which the current value is located. Since we are iterating over objects, it will be the offset of the current object. Remember from the Nesting guide that we can pass object offsets directly as the 2nd parameter to 'target' them for lookups, insertions etc.?

That is why all the following lookup functions use val_ofs as their 2nd parameter to lookup a field inside the current object:

lite3_ctx_is_null(ctx, val_ofs, "benefits")
lite3_ctx_get_i64(ctx, val_ofs, "id", &id)
lite3_ctx_get_bool(ctx, val_ofs, "vip_member", &vip_member)
lite3_ctx_get_str(ctx, val_ofs, "name", &name)

Every iteration, this val_ofs will change to the next object, then we lookup the same fields on it.

Then each iteration we do a a printout of the values we just read:

printf("id: %li\tname: %s\tvip_member: %s\tbenefits: %s\n",
id,
LITE3_STR(ctx->buf, name),
vip_member ? "true" : "false",
benefits ? "yes" : "no"
);

Output:

id: 0 name: Boris vip_member: false benefits: no

Iterating over objects

With arrays, there are only elements. But objects are slightly different, as now there are keys as well. To show the difference, we will iterate over the key-value pairs inside the last object of the array.

It starts by creating another iterator object 'iter_2':

lite3_iter iter_2;
if (lite3_ctx_iter_create(ctx, val_ofs, &iter_2) < 0) {
perror("Failed to create iterator");
return 1;
}

Then the traversal:

printf("\nObject keys:\n");
size_t val_ofs_2;
while (lite3_ctx_iter_next(ctx, &iter_2, &key, &val_ofs_2) == LITE3_ITER_ITEM) {
lite3_val *val = (lite3_val *)(ctx->buf + val_ofs_2);
printf("key: %s\tvalue: ", LITE3_STR(ctx->buf, key));
switch (val->type) {
printf("%li\n", lite3_val_i64(val));
break;
printf("%s\n", lite3_val_bool(val) ? "true" : "false");
break;
printf("null\n");
break;
printf("%s\n", lite3_val_str(val));
break;
default:
fprintf(stderr, "Invalid object value type\n");
return 1;
}
}

We take advantage of the situation by showcasing the use of lite3_val functions. Since Lite³ is a schemaless format where data can contain several different types (see enum lite3_type), we cannot know beforehand which type a value will be. A generic 'value reference' is required to point to the value, whatever type it may be.

lite3_val fulfils exactly this role, representing an opaque reference inside a Lite³ buffer that may be of any type. To find out, we can check val->type or lite3_val_type(val). This is explained more in detail in the guide about Reading Messages.

This line may at first seem puzzling:

lite3_val *val = (lite3_val *)(ctx->buf + val_ofs_2);

Remember that lite3_ctx_iter_next() always writes back the offset of the current value. If the value turns out to be an object or array, it can be passed directly to other functions as 2nd parameter. But otherwise, it is still just an offset. To take advantage of lite3_val functions, we must obtain an actual pointer. To do this, we add the offset to the buffer pointer (start) of the message. The result is cast to (lite3_val *) and voilà we have ourselves a pointer.

Functions like this can now interpret the actual type:

lite3_val_i64(val)
lite3_val_bool(val)
lite3_val_str(val)

A switch/case statement illustrates differentiating between these types:

printf("key: %s\tvalue: ", LITE3_STR(ctx->buf, key));
switch (val->type) {
printf("%li\n", lite3_val_i64(val));
break;
printf("%s\n", lite3_val_bool(val) ? "true" : "false");
break;
printf("null\n");
break;
printf("%s\n", lite3_val_str(val));
break;
default:
fprintf(stderr, "Invalid object value type\n");
return 1;
}

Output:

key: id value: 5
key: benefits value: null
key: name value: Sarah
key: vip_member value: false

The key string is printed, along with the value interpreted as the proper type.

Warning
lite3_val functions do not perform runtime type checking. It is up to the application to check for the type before interpreting it as such.

Cleaning up

We are good citizens, so we clean up after ourselves:

This destroys the context, freeing all the internal buffers so that the memory is released. When you create contexts, don't forget to destroy them, or else you will be leaking memory.

Conclusion

This was the sixth guide showing iterators.

The next and final guide will be about JSON conversion: Next Guide: JSON conversion