/* * TinyBlocks - tinyblocks-json / include/ast.h * Arvore de valores JSON e API de parse/construcao/emissao. */ #ifndef TB_JSON_AST_H #define TB_JSON_AST_H #include #include #include "lexeme.h" typedef enum { JSON_NULL, JSON_BOOL, JSON_INT, JSON_REAL, JSON_STR, JSON_ARR, JSON_OBJ } JsonKind; typedef struct JsonValue JsonValue; struct JsonValue { JsonKind kind; bool bval; long long ival; double fval; char* str; /* JSON_STR */ JsonValue** items; /* JSON_ARR / JSON_OBJ (valores) */ char** keys; /* JSON_OBJ (chaves, alinhado a items) */ size_t count, cap; }; /* -- Parse --- */ JsonValue* json_parse(const char* text, JsonLexError* err); JsonValue* json_parse_file(const char* path, JsonLexError* err); void json_free(JsonValue* v); /* -- Construcao (assumem a posse dos filhos) --- */ JsonValue* json_null(void); JsonValue* json_bool(bool b); JsonValue* json_int(long long i); JsonValue* json_real(double d); JsonValue* json_str(const char* s); JsonValue* json_arr(void); JsonValue* json_obj(void); void json_arr_add(JsonValue* arr, JsonValue* v); void json_obj_put(JsonValue* obj, const char* key, JsonValue* v); /* ultimo vence */ /* -- Navegacao --- */ JsonValue* json_obj_get(const JsonValue* obj, const char* key); JsonValue* json_arr_at(const JsonValue* arr, size_t i); size_t json_size(const JsonValue* v); const char* json_as_str (const JsonValue* v, const char* def); long long json_as_int (const JsonValue* v, long long def); double json_as_real(const JsonValue* v, double def); bool json_as_bool(const JsonValue* v, bool def); /* -- Emissao (json_to_string devolve malloc; libere com free) --- */ char* json_to_string(const JsonValue* v, bool pretty); bool json_write_file(const JsonValue* v, const char* path, bool pretty); #endif /* TB_JSON_AST_H */