#ifndef WEASELJSON_H #define WEASELJSON_H #ifdef __cplusplus extern "C" { #endif struct WeaselJsonCallbacks { void (*on_begin_object)(void *userdata); void (*on_end_object)(void *userdata); /** The string data provided has already been unescaped. If `done` is false, * this string may be incomplete and there will be another call with more data */ void (*on_string_data)(void *userdata, const char *buf, int len, int done); void (*on_begin_array)(void *userdata); void (*on_end_array)(void *userdata); /*If `done` is false, this number may be incomplete and there will be another * call with more data*/ void (*on_number_data)(void *userdata, const char *buf, int len, int done); void (*on_true_literal)(void *userdata); void (*on_false_literal)(void *userdata); void (*on_null_literal)(void *userdata); }; enum WeaselJsonStatus { /** Accept input */ WeaselJson_OK, /** Consumed all available input. Call WeaselJsonParser_parse with more data to provide more input, or call with length 0 to indicate end of data */ WeaselJson_AGAIN, /** Invalid json */ WeaselJson_REJECT, /** json is too deeply nested */ WeaselJson_OVERFLOW, }; typedef struct WeaselJsonParser WeaselJsonParser; /** Create a parser. Increasing stack size increases memory usage but also * increases the depth of nested json accepted. `callbacks` and `userdata` must * outlive the returned parser. Returns null if there's insufficient available * memory */ WeaselJsonParser *WeaselJsonParser_create(int stackSize, const WeaselJsonCallbacks *callbacks, void *userdata); /** Restore the parser to its newly-created state */ void WeaselJsonParser_reset(WeaselJsonParser *parser); /** Destroy the parser */ void WeaselJsonParser_destroy(WeaselJsonParser *parser); /** Incrementally parse `len` more bytes starting at `buf`. `buf` may be * modified. Call with `len` 0 to indicate end of data. `buf` may be null if * `len` is 0 */ WeaselJsonStatus WeaselJsonParser_parse(WeaselJsonParser *parser, char *buf, int len); #ifdef __cplusplus } #endif #endif