Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
buffer_api/01-building-messages.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], rx[1024];
38
39int main() {
40 size_t buflen = 0;
41 size_t bufsz = sizeof(buf);
42
43 // Build message
44 if (lite3_init_obj(buf, &buflen, bufsz) < 0
45 || lite3_set_str(buf, &buflen, 0, bufsz, "event", "lap_complete") < 0
46 || lite3_set_i64(buf, &buflen, 0, bufsz, "lap", 55) < 0
47 || lite3_set_f64(buf, &buflen, 0, bufsz, "time_sec", 88.427) < 0) {
48 perror("Failed to build message");
49 return 1;
50 }
51 printf("buflen: %zu\n", buflen);
52 if (lite3_json_print(buf, buflen, 0) < 0) { // Print Lite³ as JSON
53 perror("Failed to print JSON");
54 return 1;
55 }
56
57 printf("\nUpdating lap count\n");
58 if (lite3_set_i64(buf, &buflen, 0, bufsz, "lap", 56) < 0) {
59 perror("Failed to update lap count");
60 return 1;
61 }
62
63 printf("Data to send:\n");
64 printf("buflen: %zu\n", buflen);
65 if (lite3_json_print(buf, buflen, 0) < 0) {
66 perror("Failed to print JSON");
67 return 1;
68 }
69
70 // Transmit
71 size_t rx_buflen = buflen;
72 size_t rx_bufsz = sizeof(rx);
73 memcpy(rx, buf, buflen);
74
75 // Mutate (zero-copy, no parsing)
76 printf("\nVerifying fastest lap\n");
77 if (lite3_set_str(rx, &rx_buflen, 0, rx_bufsz, "verified", "race_control") < 0
78 || lite3_set_bool(rx, &rx_buflen, 0, rx_bufsz, "fastest_lap", true) < 0) {
79 perror("Failed to verify lap");
80 return 1;
81 }
82
83 printf("Modified data:\n");
84 printf("rx_buflen: %zu\n", rx_buflen);
85 if (lite3_json_print(rx, rx_buflen, 0) < 0) {
86 perror("Failed to print JSON");
87 return 1;
88 }
89
90 // Ready to send:
91 // send(sock, rx, rx_buflen, 0);
92
93 return 0;
94}
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
#define lite3_set_bool(buf, inout_buflen, ofs, bufsz, key, value)
Set boolean in object.
Definition lite3.h:852
#define lite3_set_f64(buf, inout_buflen, ofs, bufsz, key, value)
Set floating point in object.
Definition lite3.h:916
#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
Lite³ Buffer API Header.