Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_avro.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef __NILOREA_AVRO__
28#define __NILOREA_AVRO__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39#include "nilorea/n_common.h"
40#include "nilorea/n_str.h"
41#include "nilorea/n_list.h"
42#include "nilorea/n_hash.h"
43#include "nilorea/n_log.h"
44
45#include <cJSON.h>
46
48#define AVRO_MAGIC "Obj\x01"
50#define AVRO_MAGIC_LEN 4
52#define AVRO_SYNC_LEN 16
53
71
73typedef struct AVRO_SCHEMA AVRO_SCHEMA;
74
76typedef struct AVRO_FIELD {
78 char* name;
82
110
112typedef struct AVRO_READER {
114 const unsigned char* data;
116 size_t size;
118 size_t pos;
120
121/* ========== Schema functions ========== */
122
124AVRO_SCHEMA* avro_schema_parse(const char* json_str);
125
127AVRO_SCHEMA* avro_schema_from_cjson(const cJSON* json);
128
130void avro_schema_free(AVRO_SCHEMA** schema);
131
133char* avro_schema_to_json(const AVRO_SCHEMA* schema);
134
135/* ========== Zig-zag varint encoding ========== */
136
138int avro_encode_long(N_STR** dest, int64_t value);
139
141int avro_decode_long(AVRO_READER* reader, int64_t* value);
142
143/* ========== Binary encoding (datum -> N_STR) ========== */
144
146int avro_encode_datum(N_STR** dest, const AVRO_SCHEMA* schema, const cJSON* json);
147
148/* ========== Binary decoding (N_STR -> datum) ========== */
149
151cJSON* avro_decode_datum(AVRO_READER* reader, const AVRO_SCHEMA* schema);
152
153/* ========== Object Container File I/O ========== */
154
156int avro_json_to_file(const char* avro_filename, const char* schema_filename, const char* json_filename);
157
159int avro_file_to_json(const char* avro_filename, const char* schema_filename, const char* json_filename);
160
161/* ========== In-memory N_STR conversions ========== */
162
164N_STR* avro_encode_container(const AVRO_SCHEMA* schema, const cJSON* records);
165
167cJSON* avro_decode_container(const AVRO_SCHEMA* schema, const N_STR* avro_data);
168
170N_STR* avro_nstr_json_to_avro(const N_STR* schema_nstr, const N_STR* json_nstr);
171
173N_STR* avro_nstr_avro_to_json(const N_STR* schema_nstr, const N_STR* avro_nstr);
174
176AVRO_SCHEMA* avro_schema_parse_nstr(const N_STR* schema_nstr);
177
180#ifdef __cplusplus
181}
182#endif
183#endif /* __NILOREA_AVRO__ */
size_t nb_symbols
number of enum symbols
Definition n_avro.h:106
char * name
field name
Definition n_avro.h:78
AVRO_SCHEMA ** union_branches
union branch schemas
Definition n_avro.h:100
char * name
name (for record, enum, fixed)
Definition n_avro.h:88
AVRO_SCHEMA * items
item schema (for array)
Definition n_avro.h:96
AVRO_SCHEMA * values
value schema (for map)
Definition n_avro.h:98
AVRO_FIELD * fields
namespace (for record, enum)
Definition n_avro.h:90
size_t pos
current position
Definition n_avro.h:118
size_t fixed_size
fixed size
Definition n_avro.h:108
AVRO_SCHEMA * schema
field schema
Definition n_avro.h:80
AVRO_TYPE type
schema type
Definition n_avro.h:86
const unsigned char * data
data buffer
Definition n_avro.h:114
size_t nb_branches
number of union branches
Definition n_avro.h:102
char ** symbols
enum symbols
Definition n_avro.h:104
size_t nb_fields
number of fields (for record)
Definition n_avro.h:94
size_t size
total size
Definition n_avro.h:116
N_STR * avro_nstr_json_to_avro(const N_STR *schema_nstr, const N_STR *json_nstr)
Convert JSON N_STR to Avro N_STR using schema N_STR (all in-memory)
Definition n_avro.c:1279
AVRO_TYPE
Avro schema type enumeration.
Definition n_avro.h:55
int avro_file_to_json(const char *avro_filename, const char *schema_filename, const char *json_filename)
Read an Avro object container file and produce a JSON file using a schema file.
Definition n_avro.c:1210
N_STR * avro_encode_container(const AVRO_SCHEMA *schema, const cJSON *records)
Encode a cJSON array of records into Avro container format N_STR.
Definition n_avro.c:942
AVRO_SCHEMA * avro_schema_from_cjson(const cJSON *json)
Parse an Avro schema from a cJSON object.
Definition n_avro.c:73
int avro_encode_datum(N_STR **dest, const AVRO_SCHEMA *schema, const cJSON *json)
Encode a cJSON value as Avro binary according to schema.
Definition n_avro.c:475
AVRO_SCHEMA * avro_schema_parse_nstr(const N_STR *schema_nstr)
Parse schema from N_STR.
Definition n_avro.c:1272
AVRO_SCHEMA * avro_schema_parse(const char *json_str)
Parse an Avro schema from a JSON string.
Definition n_avro.c:211
int avro_decode_long(AVRO_READER *reader, int64_t *value)
Decode a zig-zag varint from reader into a 64-bit signed integer.
Definition n_avro.c:394
cJSON * avro_decode_container(const AVRO_SCHEMA *schema, const N_STR *avro_data)
Decode an Avro container format N_STR into a cJSON array of records.
Definition n_avro.c:1025
cJSON * avro_decode_datum(AVRO_READER *reader, const AVRO_SCHEMA *schema)
Decode an Avro binary datum into cJSON according to schema.
Definition n_avro.c:717
N_STR * avro_nstr_avro_to_json(const N_STR *schema_nstr, const N_STR *avro_nstr)
Convert Avro N_STR to JSON N_STR using schema N_STR (all in-memory)
Definition n_avro.c:1315
char * avro_schema_to_json(const AVRO_SCHEMA *schema)
Convert an Avro schema back to JSON string (caller must free)
Definition n_avro.c:258
void avro_schema_free(AVRO_SCHEMA **schema)
Free an Avro schema.
Definition n_avro.c:222
int avro_encode_long(N_STR **dest, int64_t value)
Encode a 64-bit signed integer as zig-zag varint into N_STR.
Definition n_avro.c:376
int avro_json_to_file(const char *avro_filename, const char *schema_filename, const char *json_filename)
Write an Avro object container file from a JSON file and schema file.
Definition n_avro.c:1146
@ AVRO_ENUM
Definition n_avro.h:65
@ AVRO_MAP
Definition n_avro.h:67
@ AVRO_FIXED
Definition n_avro.h:69
@ AVRO_UNION
Definition n_avro.h:68
@ AVRO_ARRAY
Definition n_avro.h:66
@ AVRO_FLOAT
Definition n_avro.h:60
@ AVRO_DOUBLE
Definition n_avro.h:61
@ AVRO_BOOLEAN
Definition n_avro.h:57
@ AVRO_INT
Definition n_avro.h:58
@ AVRO_STRING
Definition n_avro.h:63
@ AVRO_LONG
Definition n_avro.h:59
@ AVRO_NULL
Definition n_avro.h:56
@ AVRO_BYTES
Definition n_avro.h:62
@ AVRO_RECORD
Definition n_avro.h:64
Avro schema field (for records)
Definition n_avro.h:76
Avro read cursor for decoding.
Definition n_avro.h:112
Avro schema definition.
Definition n_avro.h:84
A box including a string and his lenght.
Definition n_str.h:60
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
Generic log system.
N_STR and string function declaration.