2012-10-16 19:56:54 +02:00
/*
2019-07-11 21:23:37 +09:00
SPDX - License - Identifier : MIT
2021-08-05 20:24:57 +02:00
Parson 1.2 .0 ( http : //kgabis.github.com/parson/ )
2021-04-07 15:23:01 -05:00
Copyright ( c ) 2012 - 2021 Krzysztof Gabis
2016-04-23 12:11:25 +02:00
2012-10-16 19:56:54 +02:00
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 :
2016-04-23 12:11:25 +02:00
2012-10-16 19:56:54 +02:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
2016-04-23 12:11:25 +02:00
2012-10-16 19:56:54 +02:00
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 .
*/
2012-11-03 19:52:49 +01:00
2012-10-16 19:56:54 +02:00
# ifndef parson_parson_h
# define parson_parson_h
# ifdef __cplusplus
extern " C "
{
2016-04-23 12:11:25 +02:00
# endif
2021-08-05 20:24:57 +02:00
# define PARSON_VERSION_MAJOR 1
# define PARSON_VERSION_MINOR 2
# define PARSON_VERSION_PATCH 0
# define PARSON_VERSION_STRING "1.2.0"
2016-04-23 12:11:25 +02:00
# include <stddef.h> /* size_t */
2012-11-03 19:52:49 +01:00
/* Types and enums */
2012-10-16 19:56:54 +02:00
typedef struct json_object_t JSON_Object ;
2012-11-03 08:19:28 -04:00
typedef struct json_array_t JSON_Array ;
typedef struct json_value_t JSON_Value ;
2012-10-16 19:56:54 +02:00
2014-10-07 21:11:29 +02:00
enum json_value_type {
JSONError = - 1 ,
2012-11-03 08:19:28 -04:00
JSONNull = 1 ,
JSONString = 2 ,
JSONNumber = 3 ,
JSONObject = 4 ,
JSONArray = 5 ,
2012-10-18 15:25:11 +02:00
JSONBoolean = 6
2014-10-07 21:11:29 +02:00
} ;
typedef int JSON_Value_Type ;
2016-04-23 12:11:25 +02:00
2014-10-07 21:11:29 +02:00
enum json_result_t {
JSONSuccess = 0 ,
JSONFailure = - 1
} ;
typedef int JSON_Status ;
2016-04-23 12:11:25 +02:00
2015-06-01 22:26:31 +01:00
typedef void * ( * JSON_Malloc_Function ) ( size_t ) ;
typedef void ( * JSON_Free_Function ) ( void * ) ;
/* Call only once, before calling any other function from parson API. If not called, malloc and free
from stdlib will be used for all allocations */
void json_set_allocation_functions ( JSON_Malloc_Function malloc_fun , JSON_Free_Function free_fun ) ;
2016-04-23 12:11:25 +02:00
2018-11-26 20:12:16 +01:00
/* Sets if slashes should be escaped or not when serializing JSON. By default slashes are escaped.
This function sets a global setting and is not thread safe . */
void json_set_escape_slashes ( int escape_slashes ) ;
2012-10-16 19:56:54 +02:00
/* Parses first JSON value in a file, returns NULL in case of error */
2014-10-07 21:11:29 +02:00
JSON_Value * json_parse_file ( const char * filename ) ;
2012-10-16 19:56:54 +02:00
2013-11-30 20:22:16 +01:00
/* Parses first JSON value in a file and ignores comments (/ * * / and //),
returns NULL in case of error */
2014-10-07 21:11:29 +02:00
JSON_Value * json_parse_file_with_comments ( const char * filename ) ;
2016-04-23 12:11:25 +02:00
2012-10-16 19:56:54 +02:00
/* Parses first JSON value in a string, returns NULL in case of error */
2014-10-07 21:11:29 +02:00
JSON_Value * json_parse_string ( const char * string ) ;
2012-10-16 19:56:54 +02:00
2013-11-30 20:22:16 +01:00
/* Parses first JSON value in a string and ignores comments (/ * * / and //),
returns NULL in case of error */
2014-10-07 21:11:29 +02:00
JSON_Value * json_parse_string_with_comments ( const char * string ) ;
2016-04-23 12:11:25 +02:00
2014-10-07 21:11:29 +02:00
/* Serialization */
2015-07-03 19:12:48 +01:00
size_t json_serialization_size ( const JSON_Value * value ) ; /* returns 0 on fail */
2014-10-07 21:11:29 +02:00
JSON_Status json_serialize_to_buffer ( const JSON_Value * value , char * buf , size_t buf_size_in_bytes ) ;
JSON_Status json_serialize_to_file ( const JSON_Value * value , const char * filename ) ;
char * json_serialize_to_string ( const JSON_Value * value ) ;
2015-06-22 16:53:19 +02:00
/* Pretty serialization */
2015-07-03 19:12:48 +01:00
size_t json_serialization_size_pretty ( const JSON_Value * value ) ; /* returns 0 on fail */
2015-06-22 16:53:19 +02:00
JSON_Status json_serialize_to_buffer_pretty ( const JSON_Value * value , char * buf , size_t buf_size_in_bytes ) ;
JSON_Status json_serialize_to_file_pretty ( const JSON_Value * value , const char * filename ) ;
char * json_serialize_to_string_pretty ( const JSON_Value * value ) ;
void json_free_serialized_string ( char * string ) ; /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */
2014-10-07 21:11:29 +02:00
/* Comparing */
int json_value_equals ( const JSON_Value * a , const JSON_Value * b ) ;
2016-04-23 12:11:25 +02:00
2014-10-07 21:11:29 +02:00
/* Validation
2016-04-23 12:11:25 +02:00
This is * NOT * JSON Schema . It validates json by checking if object have identically
2014-10-07 21:11:29 +02:00
named fields with matching types .
2016-04-23 12:11:25 +02:00
For example schema { " name " : " " , " age " : 0 } will validate
2014-10-07 21:11:29 +02:00
{ " name " : " Joe " , " age " : 25 } and { " name " : " Joe " , " age " : 25 , " gender " : " m " } ,
but not { " name " : " Joe " } or { " name " : " Joe " , " age " : " Cucumber " } .
In case of arrays , only first value in schema is checked against all values in tested array .
Empty objects ( { } ) validate all objects , empty arrays ( [ ] ) validate all arrays ,
null validates values of every type .
*/
JSON_Status json_validate ( const JSON_Value * schema , const JSON_Value * value ) ;
2016-04-23 12:11:25 +02:00
2014-10-07 21:11:29 +02:00
/*
2016-04-23 12:15:13 +02:00
* JSON Object
2014-10-07 21:11:29 +02:00
*/
2012-11-03 19:52:49 +01:00
JSON_Value * json_object_get_value ( const JSON_Object * object , const char * name ) ;
const char * json_object_get_string ( const JSON_Object * object , const char * name ) ;
2020-04-16 12:55:56 -07:00
size_t json_object_get_string_len ( const JSON_Object * object , const char * name ) ; /* doesn't account for last null character */
2012-11-03 19:52:49 +01:00
JSON_Object * json_object_get_object ( const JSON_Object * object , const char * name ) ;
JSON_Array * json_object_get_array ( const JSON_Object * object , const char * name ) ;
2014-10-07 21:11:29 +02:00
double json_object_get_number ( const JSON_Object * object , const char * name ) ; /* returns 0 on fail */
int json_object_get_boolean ( const JSON_Object * object , const char * name ) ; /* returns -1 on fail */
2012-10-16 19:56:54 +02:00
/* dotget functions enable addressing values with dot notation in nested objects,
2012-11-03 19:52:49 +01:00
just like in structs or c + + / java / c # objects ( e . g . objectA . objectB . value ) .
Because valid names in JSON can contain dots , some values may be inaccessible
this way . */
JSON_Value * json_object_dotget_value ( const JSON_Object * object , const char * name ) ;
const char * json_object_dotget_string ( const JSON_Object * object , const char * name ) ;
2020-04-16 12:55:56 -07:00
size_t json_object_dotget_string_len ( const JSON_Object * object , const char * name ) ; /* doesn't account for last null character */
2012-11-03 19:52:49 +01:00
JSON_Object * json_object_dotget_object ( const JSON_Object * object , const char * name ) ;
JSON_Array * json_object_dotget_array ( const JSON_Object * object , const char * name ) ;
2014-10-07 21:11:29 +02:00
double json_object_dotget_number ( const JSON_Object * object , const char * name ) ; /* returns 0 on fail */
int json_object_dotget_boolean ( const JSON_Object * object , const char * name ) ; /* returns -1 on fail */
2012-11-03 19:52:49 +01:00
2012-11-07 22:51:03 +01:00
/* Functions to get available names */
2016-04-23 12:11:25 +02:00
size_t json_object_get_count ( const JSON_Object * object ) ;
const char * json_object_get_name ( const JSON_Object * object , size_t index ) ;
2016-04-23 11:59:34 +02:00
JSON_Value * json_object_get_value_at ( const JSON_Object * object , size_t index ) ;
2016-12-29 23:50:20 +01:00
JSON_Value * json_object_get_wrapping_value ( const JSON_Object * object ) ;
2016-04-23 12:11:25 +02:00
2016-07-05 12:23:17 +02:00
/* Functions to check if object has a value with a specific name. Returned value is 1 if object has
* a value and 0 if it doesn ' t . dothas functions behave exactly like dotget functions . */
int json_object_has_value ( const JSON_Object * object , const char * name ) ;
int json_object_has_value_of_type ( const JSON_Object * object , const char * name , JSON_Value_Type type ) ;
int json_object_dothas_value ( const JSON_Object * object , const char * name ) ;
int json_object_dothas_value_of_type ( const JSON_Object * object , const char * name , JSON_Value_Type type ) ;
2016-04-23 12:11:25 +02:00
/* Creates new name-value pair or frees and replaces old value with a new one.
2015-07-15 22:29:14 +01:00
* json_object_set_value does not copy passed value so it shouldn ' t be freed afterwards . */
2014-10-07 21:11:29 +02:00
JSON_Status json_object_set_value ( JSON_Object * object , const char * name , JSON_Value * value ) ;
JSON_Status json_object_set_string ( JSON_Object * object , const char * name , const char * string ) ;
2020-04-16 12:55:56 -07:00
JSON_Status json_object_set_string_with_len ( JSON_Object * object , const char * name , const char * string , size_t len ) ; /* length shouldn't include last null character */
2014-10-07 21:11:29 +02:00
JSON_Status json_object_set_number ( JSON_Object * object , const char * name , double number ) ;
JSON_Status json_object_set_boolean ( JSON_Object * object , const char * name , int boolean ) ;
JSON_Status json_object_set_null ( JSON_Object * object , const char * name ) ;
2015-07-15 22:29:14 +01:00
/* Works like dotget functions, but creates whole hierarchy if necessary.
* json_object_dotset_value does not copy passed value so it shouldn ' t be freed afterwards . */
2014-10-07 21:11:29 +02:00
JSON_Status json_object_dotset_value ( JSON_Object * object , const char * name , JSON_Value * value ) ;
JSON_Status json_object_dotset_string ( JSON_Object * object , const char * name , const char * string ) ;
2020-04-16 12:55:56 -07:00
JSON_Status json_object_dotset_string_with_len ( JSON_Object * object , const char * name , const char * string , size_t len ) ; /* length shouldn't include last null character */
2014-10-07 21:11:29 +02:00
JSON_Status json_object_dotset_number ( JSON_Object * object , const char * name , double number ) ;
JSON_Status json_object_dotset_boolean ( JSON_Object * object , const char * name , int boolean ) ;
JSON_Status json_object_dotset_null ( JSON_Object * object , const char * name ) ;
/* Frees and removes name-value pair */
JSON_Status json_object_remove ( JSON_Object * object , const char * name ) ;
/* Works like dotget function, but removes name-value pair only on exact match. */
JSON_Status json_object_dotremove ( JSON_Object * object , const char * key ) ;
/* Removes all name-value pairs in object */
JSON_Status json_object_clear ( JSON_Object * object ) ;
2016-04-23 12:11:25 +02:00
/*
* JSON Array
2014-10-07 21:11:29 +02:00
*/
2012-11-03 19:52:49 +01:00
JSON_Value * json_array_get_value ( const JSON_Array * array , size_t index ) ;
const char * json_array_get_string ( const JSON_Array * array , size_t index ) ;
2020-04-16 12:55:56 -07:00
size_t json_array_get_string_len ( const JSON_Array * array , size_t index ) ; /* doesn't account for last null character */
2012-11-03 19:52:49 +01:00
JSON_Object * json_array_get_object ( const JSON_Array * array , size_t index ) ;
JSON_Array * json_array_get_array ( const JSON_Array * array , size_t index ) ;
2014-10-07 21:11:29 +02:00
double json_array_get_number ( const JSON_Array * array , size_t index ) ; /* returns 0 on fail */
int json_array_get_boolean ( const JSON_Array * array , size_t index ) ; /* returns -1 on fail */
2012-11-03 08:19:28 -04:00
size_t json_array_get_count ( const JSON_Array * array ) ;
2016-12-29 23:50:20 +01:00
JSON_Value * json_array_get_wrapping_value ( const JSON_Array * array ) ;
2018-09-06 13:30:52 -07:00
2014-10-07 21:11:29 +02:00
/* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist.
* Order of values in array may change during execution . */
JSON_Status json_array_remove ( JSON_Array * array , size_t i ) ;
/* Frees and removes from array value at given index and replaces it with given one.
2016-04-23 12:11:25 +02:00
* Does nothing and returns JSONFailure if index doesn ' t exist .
2015-07-15 22:29:14 +01:00
* json_array_replace_value does not copy passed value so it shouldn ' t be freed afterwards . */
2014-10-07 21:11:29 +02:00
JSON_Status json_array_replace_value ( JSON_Array * array , size_t i , JSON_Value * value ) ;
JSON_Status json_array_replace_string ( JSON_Array * array , size_t i , const char * string ) ;
2020-04-16 12:55:56 -07:00
JSON_Status json_array_replace_string_with_len ( JSON_Array * array , size_t i , const char * string , size_t len ) ; /* length shouldn't include last null character */
2014-10-07 21:11:29 +02:00
JSON_Status json_array_replace_number ( JSON_Array * array , size_t i , double number ) ;
JSON_Status json_array_replace_boolean ( JSON_Array * array , size_t i , int boolean ) ;
JSON_Status json_array_replace_null ( JSON_Array * array , size_t i ) ;
/* Frees and removes all values from array */
JSON_Status json_array_clear ( JSON_Array * array ) ;
2015-07-15 22:29:14 +01:00
/* Appends new value at the end of array.
* json_array_append_value does not copy passed value so it shouldn ' t be freed afterwards . */
2014-10-07 21:11:29 +02:00
JSON_Status json_array_append_value ( JSON_Array * array , JSON_Value * value ) ;
JSON_Status json_array_append_string ( JSON_Array * array , const char * string ) ;
2020-04-16 12:55:56 -07:00
JSON_Status json_array_append_string_with_len ( JSON_Array * array , const char * string , size_t len ) ; /* length shouldn't include last null character */
2014-10-07 21:11:29 +02:00
JSON_Status json_array_append_number ( JSON_Array * array , double number ) ;
JSON_Status json_array_append_boolean ( JSON_Array * array , int boolean ) ;
JSON_Status json_array_append_null ( JSON_Array * array ) ;
2016-04-23 12:11:25 +02:00
2014-10-07 21:11:29 +02:00
/*
* JSON Value
*/
JSON_Value * json_value_init_object ( void ) ;
JSON_Value * json_value_init_array ( void ) ;
JSON_Value * json_value_init_string ( const char * string ) ; /* copies passed string */
2020-04-16 12:55:56 -07:00
JSON_Value * json_value_init_string_with_len ( const char * string , size_t length ) ; /* copies passed string, length shouldn't include last null character */
2014-10-07 21:11:29 +02:00
JSON_Value * json_value_init_number ( double number ) ;
JSON_Value * json_value_init_boolean ( int boolean ) ;
JSON_Value * json_value_init_null ( void ) ;
JSON_Value * json_value_deep_copy ( const JSON_Value * value ) ;
void json_value_free ( JSON_Value * value ) ;
2012-10-16 19:56:54 +02:00
2012-11-03 19:52:49 +01:00
JSON_Value_Type json_value_get_type ( const JSON_Value * value ) ;
JSON_Object * json_value_get_object ( const JSON_Value * value ) ;
JSON_Array * json_value_get_array ( const JSON_Value * value ) ;
const char * json_value_get_string ( const JSON_Value * value ) ;
2020-04-16 12:55:56 -07:00
size_t json_value_get_string_len ( const JSON_Value * value ) ; /* doesn't account for last null character */
2012-11-03 19:52:49 +01:00
double json_value_get_number ( const JSON_Value * value ) ;
int json_value_get_boolean ( const JSON_Value * value ) ;
2016-12-29 23:50:20 +01:00
JSON_Value * json_value_get_parent ( const JSON_Value * value ) ;
2014-10-07 21:11:29 +02:00
/* Same as above, but shorter */
JSON_Value_Type json_type ( const JSON_Value * value ) ;
JSON_Object * json_object ( const JSON_Value * value ) ;
JSON_Array * json_array ( const JSON_Value * value ) ;
const char * json_string ( const JSON_Value * value ) ;
2020-04-16 12:55:56 -07:00
size_t json_string_len ( const JSON_Value * value ) ; /* doesn't account for last null character */
2014-10-07 21:11:29 +02:00
double json_number ( const JSON_Value * value ) ;
int json_boolean ( const JSON_Value * value ) ;
2016-04-23 12:11:25 +02:00
2012-10-16 19:56:54 +02:00
# ifdef __cplusplus
}
# endif
# endif