Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
buffer_api/04-nesting.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
33#include "lite3.h"
34
35
36static unsigned char buf[1024];
37
38int main() {
39 size_t buflen = 0;
40 size_t bufsz = sizeof(buf);
41
42 // Build message
43 if (lite3_init_obj(buf, &buflen, bufsz) < 0
44 || lite3_set_str(buf, &buflen, 0, bufsz, "event", "http_request") < 0
45 || lite3_set_str(buf, &buflen, 0, bufsz, "method", "POST") < 0
46 || lite3_set_i64(buf, &buflen, 0, bufsz, "duration_ms", 47) < 0) {
47 perror("Failed to build message");
48 return 1;
49 }
50 // Set headers
51 size_t headers_ofs;
52 if (lite3_set_obj(buf, &buflen, 0, bufsz, "headers", &headers_ofs) < 0
53 || lite3_set_str(buf, &buflen, headers_ofs, bufsz, "content-type", "application/json") < 0
54 || lite3_set_str(buf, &buflen, headers_ofs, bufsz, "x-request-id", "req_9f8e2a") < 0
55 || lite3_set_str(buf, &buflen, headers_ofs, bufsz, "user-agent", "curl/8.1.2") < 0) {
56 perror("Failed to set headers");
57 return 1;
58 }
59
60 if (lite3_json_print(buf, buflen, 0) < 0) { // Print Lite³ as JSON
61 perror("Failed to print JSON");
62 return 1;
63 }
64
65 // Get user-agent
66 lite3_str user_agent;
67 size_t ofs;
68 if (lite3_get_obj(buf, buflen, 0, "headers", &ofs) < 0
69 || lite3_get_str(buf, buflen, ofs, "user-agent", &user_agent) < 0) {
70 perror("Failed to get user-agent");
71 return 1;
72 }
73 printf("User agent: %s\n", LITE3_STR(buf, user_agent));
74
75 return 0;
76}
#define lite3_get_obj(buf, buflen, ofs, key, out)
Get object by key.
Definition lite3.h:2325
#define lite3_get_str(buf, buflen, ofs, key, out)
Get string value by key.
Definition lite3.h:2284
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
#define lite3_set_obj(buf, inout_buflen, ofs, bufsz, key, out_ofs)
Set object in object.
Definition lite3.h:1059
#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
Lite³ Buffer API Header.
Struct holding a reference to a string inside a Lite³ buffer.
Definition lite3.h:601