Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
context_api/07-json-conversion.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 <stdlib.h>
33
34#include "lite3_context_api.h"
35
36
37int main() {
39 if (!ctx) {
40 perror("Failed to create lite3_ctx *ctx");
41 return 1;
42 }
43
44 // Convert JSON file to Lite³
45 if (lite3_ctx_json_dec_file(ctx, "examples/periodic_table.json") < 0) {
46 perror("Failed to decode JSON document");
47 return 1;
48 }
49
50 // Iterator to find densest element
51 size_t data_ofs;
52 if (lite3_ctx_get_arr(ctx, 0, "data", &data_ofs) < 0) {
53 perror("Failed to get data array");
54 return 1;
55 }
56 lite3_iter iter;
57 if (lite3_ctx_iter_create(ctx, data_ofs, &iter) < 0) {
58 perror("Failed to create iterator");
59 return 1;
60 }
61 size_t el_ofs;
62 size_t el_densest_ofs = 0;
63 double el_densest_kg_per_m3 = 0.0;
64 int ret;
65 while ((ret = lite3_ctx_iter_next(ctx, &iter, NULL, &el_ofs)) == LITE3_ITER_ITEM) {
66 if (lite3_ctx_is_null(ctx, el_ofs, "density_kg_per_m3")) {
67 continue;
68 }
69 double kg_per_m3;
70 if (lite3_ctx_get_f64(ctx, el_ofs, "density_kg_per_m3", &kg_per_m3) < 0) {
71 perror("Failed to get element density");
72 return 1;
73 }
74 if (kg_per_m3 > el_densest_kg_per_m3) {
75 el_densest_ofs = el_ofs;
76 el_densest_kg_per_m3 = kg_per_m3;
77 }
78 }
79 if (ret < 0) {
80 perror("Failed to get iter element");
81 return 1;
82 }
83 if (el_densest_ofs == 0) {
84 perror("Failed to find densest element");
85 return 1;
86 }
87
88 lite3_str name;
89 if (lite3_ctx_get_str(ctx, el_densest_ofs, "name", &name) < 0) {
90 perror("Failed to get densest element name");
91 return 1;
92 }
93 printf("densest element: %s\n\n", LITE3_STR(ctx->buf, name));
94
95 printf("Convert Lite³ to JSON by returned heap pointer (prettified):\n");
96 size_t json_len;
97 char *json = lite3_ctx_json_enc_pretty(ctx, el_densest_ofs, &json_len);
98 if (!json) {
99 perror("Failed encode JSON");
100 return 1;
101 }
102 printf("%s\n\n", json);
103 free(json);
104
105 printf("Convert Lite³ to JSON by writing to buffer (non-prettified):\n");
106 size_t json_buf_size = 1024;
107 char *json_buf = malloc(json_buf_size);
108 int64_t ret_i64;
109 if ((ret_i64 = lite3_ctx_json_enc_buf(ctx, el_densest_ofs, json_buf, json_buf_size)) < 0) {
110 perror("Failed encode JSON");
111 return 1;
112 }
113 size_t json_buf_len = (size_t)ret_i64;
114 printf("%s\n", json_buf);
115 printf("json bytes written: %zu\n", json_buf_len);
116 free(json_buf);
117
119 return 0;
120}
#define lite3_ctx_get_str(ctx, ofs, key, out)
Get string value by key.
#define lite3_ctx_get_f64(ctx, ofs, key, out)
Get floating point value by key.
#define lite3_ctx_get_arr(ctx, ofs, key, out)
Get array by key.
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 int64_t lite3_ctx_json_enc_buf(lite3_ctx *ctx, size_t ofs, char *__restrict json_buf, size_t json_bufsz)
Convert Lite³ to JSON and write to output buffer.
static int lite3_ctx_json_dec_file(lite3_ctx *ctx, const char *__restrict path)
Convert JSON from file path to Lite³
static char * lite3_ctx_json_enc_pretty(lite3_ctx *ctx, size_t ofs, size_t *__restrict out_len)
Convert Lite³ to JSON prettified string.
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_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
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