35int main(
int argc,
char* argv[]) {
40 if (strcmp(argv[1],
"encode") == 0) {
41 n_log(
LOG_INFO,
"Encoding JSON to Avro: %s + %s -> %s", argv[3], argv[2], argv[4]);
48 }
else if (strcmp(argv[1],
"decode") == 0) {
49 n_log(
LOG_INFO,
"Decoding Avro to JSON: %s + %s -> %s", argv[3], argv[2], argv[4]);
57 n_log(
LOG_ERR,
"Unknown command: %s (use 'encode' or 'decode')", argv[1]);
67 const char* schema_json =
69 " \"type\": \"record\","
70 " \"name\": \"User\","
71 " \"namespace\": \"com.example\","
73 " {\"name\": \"name\", \"type\": \"string\"},"
74 " {\"name\": \"age\", \"type\": \"int\"},"
75 " {\"name\": \"email\", \"type\": [\"null\", \"string\"]},"
76 " {\"name\": \"score\", \"type\": \"double\"},"
77 " {\"name\": \"active\", \"type\": \"boolean\"}"
82 const char* json_data =
85 " \"name\": \"Alice\","
87 " \"email\": \"alice@example.com\","
99 " \"name\": \"Charlie\","
101 " \"email\": \"charlie@example.com\","
149 nstr_to_file(schema_nstr, (
char*)
"/tmp/test_avro_schema.json");
150 nstr_to_file(json_nstr, (
char*)
"/tmp/test_avro_input.json");
152 if (
avro_json_to_file(
"/tmp/test_output.avro",
"/tmp/test_avro_schema.json",
"/tmp/test_avro_input.json") == TRUE) {
155 if (
avro_file_to_json(
"/tmp/test_output.avro",
"/tmp/test_avro_schema.json",
"/tmp/test_avro_output.json") == TRUE) {
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)
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.
AVRO_SCHEMA * avro_schema_parse_nstr(const N_STR *schema_nstr)
Parse schema from N_STR.
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)
char * avro_schema_to_json(const AVRO_SCHEMA *schema)
Convert an Avro schema back to JSON string (caller must free)
void avro_schema_free(AVRO_SCHEMA **schema_ptr)
Free an Avro schema.
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.
#define Free(__ptr)
Free Handler to get errors.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_INFO
informational
size_t written
size of the written data inside the string
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
A box including a string and his lenght.
Avro binary format encoding/decoding with JSON conversion.
Common headers and low-level functions & define.
N_STR and string function declaration.