Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
context_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_context_api.h"
34
35
36int main() {
38 if (!ctx) {
39 perror("Failed to create lite3_ctx *ctx");
40 return 1;
41 }
42
43 // Build message
44 if (lite3_ctx_init_obj(ctx) < 0
45 || lite3_ctx_set_str(ctx, 0, "event", "http_request") < 0
46 || lite3_ctx_set_str(ctx, 0, "method", "POST") < 0
47 || lite3_ctx_set_i64(ctx, 0, "duration_ms", 47) < 0) {
48 perror("Failed to build message");
49 return 1;
50 }
51 // Set headers
52 size_t headers_ofs;
53 if (lite3_ctx_set_obj(ctx, 0, "headers", &headers_ofs) < 0
54 || lite3_ctx_set_str(ctx, headers_ofs, "content-type", "application/json") < 0
55 || lite3_ctx_set_str(ctx, headers_ofs, "x-request-id", "req_9f8e2a") < 0
56 || lite3_ctx_set_str(ctx, headers_ofs, "user-agent", "curl/8.1.2") < 0) {
57 perror("Failed to set headers");
58 return 1;
59 }
60
61 if (lite3_ctx_json_print(ctx, 0) < 0) { // Print Lite³ as JSON
62 perror("Failed to print JSON");
63 return 1;
64 }
65
66 // Get user-agent
67 lite3_str user_agent;
68 size_t ofs;
69 if (lite3_ctx_get_obj(ctx, 0, "headers", &ofs) < 0
70 || lite3_ctx_get_str(ctx, ofs, "user-agent", &user_agent) < 0) {
71 perror("Failed to get user-agent");
72 return 1;
73 }
74 printf("User agent: %s\n", LITE3_STR(ctx->buf, user_agent));
75
77 return 0;
78}
#define lite3_ctx_get_str(ctx, ofs, key, out)
Get string value by key.
#define lite3_ctx_get_obj(ctx, ofs, key, out)
Get object by key.
static int lite3_ctx_init_obj(lite3_ctx *ctx)
Initialize a Lite³ context as an object.
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_obj(ctx, ofs, key, out_ofs)
Set object in object.
#define lite3_ctx_set_i64(ctx, ofs, key, value)
Set integer in object.
#define lite3_ctx_set_str(ctx, ofs, key, str)
Set string in object.
#define LITE3_STR(buf, val)
Generational pointer / safe access wrapper.
Definition lite3.h:666
Lite³ Context API Header.
Lite³ context struct.
Struct holding a reference to a string inside a Lite³ buffer.
Definition lite3.h:601