Lite³
A JSON-Compatible Zero-Copy Serialization Format
Loading...
Searching...
No Matches
buffer_api/02-reading-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];
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, "title", "C Programming Language, 2nd Edition") < 0
46 || lite3_set_str(buf, &buflen, 0, bufsz, "language", "en") < 0
47 || lite3_set_f64(buf, &buflen, 0, bufsz, "price_usd", 60.30) < 0
48 || lite3_set_i64(buf, &buflen, 0, bufsz, "pages", 272) < 0
49 || lite3_set_bool(buf, &buflen, 0, bufsz, "in_stock", true) < 0
50 || lite3_set_null(buf, &buflen, 0, bufsz, "reviews") < 0) {
51 perror("Failed to build message");
52 return 1;
53 }
54 printf("buflen: %zu\n", buflen);
55 if (lite3_json_print(buf, buflen, 0) < 0) { // Print Lite³ as JSON
56 perror("Failed to print JSON");
57 return 1;
58 }
59
60 lite3_str title, language;
61 double price_usd;
62 int64_t pages;
63 bool in_stock;
64 if (lite3_get_str(buf, buflen, 0, "title", &title) < 0
65 || lite3_get_str(buf, buflen, 0, "language", &language) < 0
66 || lite3_get_f64(buf, buflen, 0, "price_usd", &price_usd) < 0
67 || lite3_get_i64(buf, buflen, 0, "pages", &pages) < 0
68 || lite3_get_bool(buf, buflen, 0, "in_stock", &in_stock) < 0) {
69 perror("Failed to read message");
70 return 1;
71 }
72 printf("\ntitle: %s\n", LITE3_STR(buf, title));
73 printf("language: %s\n", LITE3_STR(buf, language));
74 printf("price_usd: %f\n", price_usd);
75 printf("pages: %li\n", pages);
76 printf("in_stock: %s\n\n", in_stock ? "true" : "false");
77
78 if (lite3_is_null(buf, buflen, 0, "reviews")) {
79 printf("No reviews to display.\n");
80 }
81
82 printf("\nTitle field exists: %s\n", lite3_exists(buf, buflen, 0, "title") ? "true" : "false");
83 printf("Price field exists: %s\n", lite3_exists(buf, buflen, 0, "price_usd") ? "true" : "false");
84 printf("ISBN field exists: %s\n", lite3_exists(buf, buflen, 0, "isbn") ? "true" : "false");
85
86 enum lite3_type title_type = lite3_get_type(buf, buflen, 0, "title");
87 printf("\nTitle is string type: %s\n", title_type == LITE3_TYPE_STRING ? "true" : "false");
88 printf("Title is integer type: %s\n", title_type == LITE3_TYPE_I64 ? "true" : "false");
89
90 lite3_val *price_val;
91 if (lite3_get(buf, buflen, 0, "price_usd", &price_val) < 0) {
92 perror("Failed to get price_usd");
93 return 1;
94 }
95 printf("\nPrice is string type: %s\n", lite3_val_is_str(price_val) ? "true" : "false");
96 printf("Price is double type: %s\n", lite3_val_is_f64(price_val) ? "true" : "false");
97 if (price_val->type == LITE3_TYPE_F64) {
98 printf("price_val value: %f\n", lite3_val_f64(price_val));
99 printf("price_val type size: %zu\n", lite3_val_type_size(price_val));
100 }
101
102 uint32_t entry_count;
103 if (lite3_count(buf, buflen, 0, &entry_count) < 0) {
104 perror("Failed to get entry count");
105 return 1;
106 }
107 printf("\nObject entries: %u\n", entry_count);
108
109 return 0;
110}
#define lite3_get(buf, buflen, ofs, key, out)
Get value from object.
Definition lite3.h:2116
#define lite3_get_bool(buf, buflen, ofs, key, out)
Get boolean value by key.
Definition lite3.h:2139
#define lite3_get_f64(buf, buflen, ofs, key, out)
Get floating point value by key.
Definition lite3.h:2209
#define lite3_get_str(buf, buflen, ofs, key, out)
Get string value by key.
Definition lite3.h:2284
#define lite3_get_i64(buf, buflen, ofs, key, out)
Get integer value by key.
Definition lite3.h:2174
int lite3_json_print(const unsigned char *buf, size_t buflen, size_t ofs)
Print Lite³ buffer as JSON to stdout
#define lite3_set_null(buf, inout_buflen, ofs, bufsz, key)
Set null in object.
Definition lite3.h:821
#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
lite3_type
enum containing all Lite³ types
Definition lite3.h:510
#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_F64
maps to 'number' type in JSON; underlying datatype: double
Definition lite3.h:514
@ LITE3_TYPE_I64
maps to 'number' type in JSON; underlying datatype: int64_t
Definition lite3.h:513
#define lite3_exists(buf, buflen, ofs, key)
Attempt to find a key.
Definition lite3.h:1820
#define lite3_get_type(buf, buflen, ofs, key)
Find value by key and return value type.
Definition lite3.h:1754
#define lite3_is_null(buf, buflen, ofs, key)
Find value by key and test for null type.
Definition lite3.h:1874
static int lite3_count(unsigned char *buf, size_t buflen, size_t ofs, uint32_t *out)
Write back the number of object entries or array elements.
Definition lite3.h:1844
static size_t lite3_val_type_size(lite3_val *val)
Returns the size of the value type.
Definition lite3.h:2709
Lite³ Buffer API Header.
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