/* * Copyright 2022 Olaf Wintermann * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #ifndef JSON_H #define JSON_H #include #include #ifdef __cplusplus extern "C" { #endif typedef struct JSONParser JSONParser; typedef struct JSONToken JSONToken; typedef struct JSONValue JSONValue; typedef enum JSONTokenType JSONTokenType; typedef enum JSONValueType JSONValueType; typedef enum JSONLiteralType JSONLiteralType; typedef enum JSONReaderType JSONReaderType; typedef struct JSONArray JSONArray; typedef struct JSONObject JSONObject; typedef struct JSONString JSONString; typedef struct JSONInteger JSONInteger; typedef struct JSONNumber JSONNumber; typedef struct JSONLiteral JSONLiteral; typedef struct JSONObjValue JSONObjValue; typedef struct JSONReadValueStack JSONReadValueStack; enum JSONTokenType { JSON_NO_TOKEN = 0, JSON_TOKEN_ERROR, JSON_TOKEN_BEGIN_ARRAY, JSON_TOKEN_BEGIN_OBJECT, JSON_TOKEN_END_ARRAY, JSON_TOKEN_END_OBJECT, JSON_TOKEN_NAME_SEPARATOR, JSON_TOKEN_VALUE_SEPARATOR, JSON_TOKEN_STRING, JSON_TOKEN_INTEGER, JSON_TOKEN_NUMBER, JSON_TOKEN_LITERAL, JSON_TOKEN_SPACE }; enum JSONValueType { JSON_OBJECT = 0, JSON_ARRAY, JSON_STRING, JSON_INTEGER, JSON_NUMBER, JSON_LITERAL }; enum JSONLiteralType { JSON_NULL = 0, JSON_TRUE, JSON_FALSE }; enum JSONReaderType { JSON_READER_OBJECT_BEGIN, JSON_READER_OBJECT_END, JSON_READER_ARRAY_BEGIN, JSON_READER_ARRAY_END, JSON_READER_STRING, JSON_READER_INTEGER, JSON_READER_NUMBER, JSON_READER_LITERAL }; struct JSONToken { JSONTokenType tokentype; const char *content; size_t length; size_t alloc; }; struct JSONParser { const char *buffer; size_t size; size_t pos; JSONToken uncompleted; int tokenizer_escape; int *states; int nstates; int states_alloc; JSONToken reader_token; JSONReaderType reader_type; int value_ready; char *value_name; size_t value_name_len; char *value_str; size_t value_str_len; int64_t value_int; double value_double; JSONValue **readvalue_stack; int readvalue_nelm; int readvalue_alloc; JSONValue *read_value; int readvalue_initialized; int reader_array_alloc; int error; }; struct JSONArray { JSONValue **array; size_t alloc; size_t size; }; struct JSONObject { JSONObjValue *values; size_t alloc; size_t size; }; struct JSONObjValue { char *name; JSONValue *value; }; struct JSONString { char *string; size_t length; }; struct JSONInteger { int64_t value; }; struct JSONNumber { double value; }; struct JSONLiteral { JSONLiteralType literal; }; struct JSONValue { JSONValueType type; union { JSONArray array; JSONObject object; JSONString string; JSONInteger integer; JSONNumber number; JSONLiteral literal; } value; }; JSONParser* json_parser_new(void); void json_parser_free(JSONParser *p); void json_parser_fill(JSONParser *p, const char *buf, size_t size); JSONToken json_parser_next_token(JSONParser *p); int json_read(JSONParser *p); JSONReaderType json_reader_type(JSONParser *p); const char* json_reader_name(JSONParser *p, size_t *opt_len); const char* json_reader_string(JSONParser *p, size_t *opt_len); int64_t json_reader_int(JSONParser *p); double json_reader_double(JSONParser *p); int json_reader_isnull(JSONParser *p); JSONLiteralType json_reader_literal(JSONParser *p); int json_reader_bool(JSONParser *p); int json_read_value(JSONParser *p, JSONValue **value); JSONValue* json_obj_get(JSONObject *obj, const char *name); JSONValue* json_array_get(JSONArray *array, size_t i); void json_value_free(JSONValue *value); int json_strcmp(JSONValue *jsstr, const char *str); int json_strncmp(JSONValue *jsstr, const char *str, size_t slen); #ifdef __cplusplus } #endif #endif /* JSON_H */