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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#ifndef __NILOREA_AVRO__
29#define __NILOREA_AVRO__
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
40#include "nilorea/n_common.h"
41#include "nilorea/n_str.h"
42#include "nilorea/n_list.h"
43#include "nilorea/n_hash.h"
44#include "nilorea/n_log.h"
45
46#include <cJSON.h>
47
49#define AVRO_MAGIC "Obj\x01"
51#define AVRO_MAGIC_LEN 4
53#define AVRO_SYNC_LEN 16
54
72
74typedef struct AVRO_SCHEMA AVRO_SCHEMA;
75
77typedef struct AVRO_FIELD {
79 char* name;
83
111
113typedef struct AVRO_READER {
115 const unsigned char* data;
117 size_t size;
119 size_t pos;
121
122/* Schema functions */
123
125AVRO_SCHEMA* avro_schema_parse(const char* json_str);
126
128AVRO_SCHEMA* avro_schema_from_cjson(const cJSON* json);
129
131void avro_schema_free(AVRO_SCHEMA** schema);
132
134char* avro_schema_to_json(const AVRO_SCHEMA* schema);
135
136/* Zig-zag varint encoding */
137
139int avro_encode_long(N_STR** dest, int64_t value);
140
142int avro_decode_long(AVRO_READER* reader, int64_t* value);
143
144/* Binary encoding (datum -> N_STR) */
145
147int avro_encode_datum(N_STR** dest, const AVRO_SCHEMA* schema, const cJSON* json);
148
149/* Binary decoding (N_STR -> datum) */
150
152cJSON* avro_decode_datum(AVRO_READER* reader, const AVRO_SCHEMA* schema);
153
154/* Object Container File I/O */
155
157int avro_json_to_file(const char* avro_filename, const char* schema_filename, const char* json_filename);
158
160int avro_file_to_json(const char* avro_filename, const char* schema_filename, const char* json_filename);
161
162/* In-memory N_STR conversions */
163
165N_STR* avro_encode_container(const AVRO_SCHEMA* schema, const cJSON* records);
166
168cJSON* avro_decode_container(const AVRO_SCHEMA* schema, const N_STR* avro_data);
169
171N_STR* avro_nstr_json_to_avro(const N_STR* schema_nstr, const N_STR* json_nstr);
172
174N_STR* avro_nstr_avro_to_json(const N_STR* schema_nstr, const N_STR* avro_nstr);
175
177AVRO_SCHEMA* avro_schema_parse_nstr(const N_STR* schema_nstr);
178
181#ifdef __cplusplus
182}
183#endif
184#endif /* __NILOREA_AVRO__ */
size_t nb_symbols
number of enum symbols
Definition n_avro.h:107
char * name
field name
Definition n_avro.h:79
AVRO_SCHEMA ** union_branches
union branch schemas
Definition n_avro.h:101
char * name
name (for record, enum, fixed)
Definition n_avro.h:89
AVRO_SCHEMA * items
item schema (for array)
Definition n_avro.h:97
AVRO_SCHEMA * values
value schema (for map)
Definition n_avro.h:99
AVRO_FIELD * fields
namespace (for record, enum)
Definition n_avro.h:91
size_t pos
current position
Definition n_avro.h:119
size_t fixed_size
fixed size
Definition n_avro.h:109
AVRO_SCHEMA * schema
field schema
Definition n_avro.h:81
AVRO_TYPE type
schema type
Definition n_avro.h:87
const unsigned char * data
data buffer
Definition n_avro.h:115
size_t nb_branches
number of union branches
Definition n_avro.h:103
char ** symbols
enum symbols
Definition n_avro.h:105
size_t nb_fields
number of fields (for record)
Definition n_avro.h:95
size_t size
total size
Definition n_avro.h:117
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:1266
AVRO_TYPE
Avro schema type enumeration.
Definition n_avro.h:56
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:1199
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:933
AVRO_SCHEMA * avro_schema_from_cjson(const cJSON *json)
Parse an Avro schema from a cJSON object.
Definition n_avro.c:72
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:470
AVRO_SCHEMA * avro_schema_parse_nstr(const N_STR *schema_nstr)
Parse schema from N_STR.
Definition n_avro.c:1259
AVRO_SCHEMA * avro_schema_parse(const char *json_str)
Parse an Avro schema from a JSON string.
Definition n_avro.c:210
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:391
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:1016
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:710
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:1302
char * avro_schema_to_json(const AVRO_SCHEMA *schema)
Convert an Avro schema back to JSON string (caller must free)
Definition n_avro.c:257
void avro_schema_free(AVRO_SCHEMA **schema)
Free an Avro schema.
Definition n_avro.c:221
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:373
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:1135
@ AVRO_ENUM
Definition n_avro.h:66
@ AVRO_MAP
Definition n_avro.h:68
@ AVRO_FIXED
Definition n_avro.h:70
@ AVRO_UNION
Definition n_avro.h:69
@ AVRO_ARRAY
Definition n_avro.h:67
@ AVRO_FLOAT
Definition n_avro.h:61
@ AVRO_DOUBLE
Definition n_avro.h:62
@ AVRO_BOOLEAN
Definition n_avro.h:58
@ AVRO_INT
Definition n_avro.h:59
@ AVRO_STRING
Definition n_avro.h:64
@ AVRO_LONG
Definition n_avro.h:60
@ AVRO_NULL
Definition n_avro.h:57
@ AVRO_BYTES
Definition n_avro.h:63
@ AVRO_RECORD
Definition n_avro.h:65
Avro schema field (for records)
Definition n_avro.h:77
Avro read cursor for decoding.
Definition n_avro.h:113
Avro schema definition.
Definition n_avro.h:85
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
Generic log system.
N_STR and string function declaration.