6 n__avro_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_avro.h

Avro binary format encoding/decoding with JSON conversion. More...

Structures

Name
struct AVRO_SCHEMA
Avro schema definition.
struct AVRO_READER
Avro read cursor for decoding.
struct AVRO_FIELD
Avro schema field (for records).

Detailed Description

Avro binary format encoding/decoding with JSON conversion.

Author: Castagnier Mickael

Version: 1.0

Date: 11/03/2026

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __NILOREA_AVRO__
#define __NILOREA_AVRO__

#ifdef __cplusplus
extern "C" {
#endif


#include "nilorea/n_common.h"
#include "nilorea/n_str.h"
#include "nilorea/n_list.h"
#include "nilorea/n_hash.h"
#include "nilorea/n_log.h"

#include <cJSON.h>

#define AVRO_MAGIC "Obj\x01"
#define AVRO_MAGIC_LEN 4
#define AVRO_SYNC_LEN 16

typedef enum AVRO_TYPE {
    AVRO_NULL = 0,
    AVRO_BOOLEAN,
    AVRO_INT,
    AVRO_LONG,
    AVRO_FLOAT,
    AVRO_DOUBLE,
    AVRO_BYTES,
    AVRO_STRING,
    AVRO_RECORD,
    AVRO_ENUM,
    AVRO_ARRAY,
    AVRO_MAP,
    AVRO_UNION,
    AVRO_FIXED
} AVRO_TYPE;

typedef struct AVRO_SCHEMA AVRO_SCHEMA;

typedef struct AVRO_FIELD {
    char* name;
    AVRO_SCHEMA* schema;
} AVRO_FIELD;

struct AVRO_SCHEMA {
    AVRO_TYPE type;
    char* name;
    char* namespace;
    AVRO_FIELD* fields;
    size_t nb_fields;
    AVRO_SCHEMA* items;
    AVRO_SCHEMA* values;
    AVRO_SCHEMA** union_branches;
    size_t nb_branches;
    char** symbols;
    size_t nb_symbols;
    size_t fixed_size;
};

typedef struct AVRO_READER {
    const unsigned char* data;
    size_t size;
    size_t pos;
} AVRO_READER;

/* Schema functions */

AVRO_SCHEMA* avro_schema_parse(const char* json_str);

AVRO_SCHEMA* avro_schema_from_cjson(const cJSON* json);

void avro_schema_free(AVRO_SCHEMA** schema);

char* avro_schema_to_json(const AVRO_SCHEMA* schema);

/* Zig-zag varint encoding */

int avro_encode_long(N_STR** dest, int64_t value);

int avro_decode_long(AVRO_READER* reader, int64_t* value);

/* Binary encoding (datum -> N_STR) */

int avro_encode_datum(N_STR** dest, const AVRO_SCHEMA* schema, const cJSON* json);

/* Binary decoding (N_STR -> datum) */

cJSON* avro_decode_datum(AVRO_READER* reader, const AVRO_SCHEMA* schema);

/* Object Container File I/O */

int avro_json_to_file(const char* avro_filename, const char* schema_filename, const char* json_filename);

int avro_file_to_json(const char* avro_filename, const char* schema_filename, const char* json_filename);

/* In-memory N_STR conversions */

N_STR* avro_encode_container(const AVRO_SCHEMA* schema, const cJSON* records);

cJSON* avro_decode_container(const AVRO_SCHEMA* schema, const N_STR* avro_data);

N_STR* avro_nstr_json_to_avro(const N_STR* schema_nstr, const N_STR* json_nstr);

N_STR* avro_nstr_avro_to_json(const N_STR* schema_nstr, const N_STR* avro_nstr);

AVRO_SCHEMA* avro_schema_parse_nstr(const N_STR* schema_nstr);


#ifdef __cplusplus
}
#endif
#endif /* __NILOREA_AVRO__ */