c0
C0DATA — structured data using ASCII C0 control codes (C)
Loading...
Searching...
No Matches
c0_json.h
Go to the documentation of this file.
1/* c0_json.h — JSON ⇄ C0DATA conversion.
2 *
3 * An intermediate value tree (c0_json_value) mirrors the Crystal reference: every
4 * scalar is a string, objects keep insertion order. c0_to_value / c0_from_value
5 * are the structural conversions; c0_to_json / c0_from_json add a
6 * dependency-free JSON text codec. Export never guesses: a positional record
7 * is an array, a keyed record an object, a group with a header row an array
8 * of objects, a nested level the same shapes one level down, a reference
9 * {"$ref": "group/record/field"}. The round-trip is stringly typed: JSON
10 * numbers, booleans and null become strings, and every leaf serializes back
11 * out as a quoted JSON string.
12 */
13#ifndef C0_JSON_H
14#define C0_JSON_H
15
16#include "c0.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
27
29
30typedef struct {
31 char *key;
32 size_t key_len;
35
47
48/* --- Value constructors (all malloc'd; free the root with c0_json_free) --- */
49c0_json_value *c0_json_str(const char *s, size_t len);
53int c0_json_object_push(c0_json_value *obj, const char *key, size_t key_len, c0_json_value *val);
55
56/* --- Structural conversion --- */
57
60c0_json_value *c0_to_value(const uint8_t *buf, size_t len, c0_error *err);
61
65uint8_t *c0_from_value(const c0_json_value *v, const char *group_name, size_t *out_len);
66
67/* --- JSON text --- */
68
71c0_json_value *c0_json_parse(const char *json, size_t len);
72
75char *c0_json_print(const c0_json_value *v, size_t *out_len);
76
78char *c0_to_json(const uint8_t *buf, size_t len, size_t *out_len, c0_error *err);
79
82uint8_t *c0_from_json(const char *json, size_t len, const char *group_name, size_t *out_len);
83
84#ifdef __cplusplus
85}
86#endif
87
88#endif /* C0_JSON_H */
c0_json_value * c0_json_parse(const char *json, size_t len)
Parse JSON text into a value tree (numbers/bools become strings, null the empty string).
c0_json_kind
Definition c0_json.h:22
@ C0_JSON_OBJECT
Definition c0_json.h:25
@ C0_JSON_STR
Definition c0_json.h:23
@ C0_JSON_ARRAY
Definition c0_json.h:24
uint8_t * c0_from_json(const char *json, size_t len, const char *group_name, size_t *out_len)
JSON text → C0DATA compact bytes (malloc'd).
int c0_json_array_push(c0_json_value *arr, c0_json_value *item)
takes ownership of item
void c0_json_free(c0_json_value *v)
int c0_json_object_push(c0_json_value *obj, const char *key, size_t key_len, c0_json_value *val)
copies key, owns val
uint8_t * c0_from_value(const c0_json_value *v, const char *group_name, size_t *out_len)
A value tree as C0DATA compact bytes (malloc'd; caller frees).
char * c0_json_print(const c0_json_value *v, size_t *out_len)
Serialize a value tree to pretty JSON (2-space indent), NUL-terminated and malloc'd (caller frees).
c0_json_value * c0_to_value(const uint8_t *buf, size_t len, c0_error *err)
A value tree from C0DATA bytes (the first document's body).
c0_json_value * c0_json_str(const char *s, size_t len)
c0_json_value * c0_json_object(void)
char * c0_to_json(const uint8_t *buf, size_t len, size_t *out_len, c0_error *err)
C0DATA bytes → pretty JSON string (malloc'd, NUL-terminated).
c0_json_value * c0_json_array(void)
Error detail: the code, the byte offset it was found at, and a static message.
Definition c0.h:91
Definition c0_json.h:30
c0_json_value * val
Definition c0_json.h:33
size_t key_len
Definition c0_json.h:32
char * key
Definition c0_json.h:31
Definition c0_json.h:36
char * s
STR: malloc'd, not NUL-terminated (s_len authoritative).
Definition c0_json.h:38
size_t cap_pairs
Definition c0_json.h:45
c0_json_value ** items
ARRAY.
Definition c0_json.h:40
size_t s_len
Definition c0_json.h:39
size_t n_pairs
Definition c0_json.h:44
size_t n_items
Definition c0_json.h:41
c0_json_kind kind
Definition c0_json.h:37
c0_json_pair * pairs
OBJECT.
Definition c0_json.h:43
size_t cap_items
Definition c0_json.h:42