|
|
|
@ -1,17 +1,17 @@ |
|
|
|
|
/*
|
|
|
|
|
Parson ( http://kgabis.github.com/parson/ )
|
|
|
|
|
Copyright (c) 2012 - 2014 Krzysztof Gabis |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
@ -27,10 +27,11 @@ |
|
|
|
|
#ifdef __cplusplus |
|
|
|
|
extern "C" |
|
|
|
|
{ |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#include <stddef.h> /* size_t */ |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#include <stddef.h> /* size_t */ |
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
/* Types and enums */ |
|
|
|
|
typedef struct json_object_t JSON_Object; |
|
|
|
|
typedef struct json_array_t JSON_Array; |
|
|
|
@ -46,29 +47,36 @@ enum json_value_type { |
|
|
|
|
JSONBoolean = 6 |
|
|
|
|
}; |
|
|
|
|
typedef int JSON_Value_Type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum json_result_t { |
|
|
|
|
JSONSuccess = 0, |
|
|
|
|
JSONFailure = -1 |
|
|
|
|
}; |
|
|
|
|
typedef int JSON_Status; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct serializationContext |
|
|
|
|
{ |
|
|
|
|
FILE *file; |
|
|
|
|
int identation; |
|
|
|
|
}SerContext; |
|
|
|
|
|
|
|
|
|
/* Parses first JSON value in a file, returns NULL in case of error */ |
|
|
|
|
JSON_Value * json_parse_file(const char *filename); |
|
|
|
|
|
|
|
|
|
/* Parses first JSON value in a file and ignores comments (/ * * / and //),
|
|
|
|
|
returns NULL in case of error */ |
|
|
|
|
JSON_Value * json_parse_file_with_comments(const char *filename); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parses first JSON value in a string, returns NULL in case of error */ |
|
|
|
|
JSON_Value * json_parse_string(const char *string); |
|
|
|
|
|
|
|
|
|
/* Parses first JSON value in a string and ignores comments (/ * * / and //),
|
|
|
|
|
returns NULL in case of error */ |
|
|
|
|
JSON_Value * json_parse_string_with_comments(const char *string); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Serialization */ |
|
|
|
|
size_t json_serialization_size(const JSON_Value *value); |
|
|
|
|
JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); |
|
|
|
|
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); |
|
|
|
@ -76,11 +84,11 @@ void json_free_serialized_string(char *string); /* frees string from json |
|
|
|
|
|
|
|
|
|
/* Comparing */ |
|
|
|
|
int json_value_equals(const JSON_Value *a, const JSON_Value *b); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Validation
|
|
|
|
|
This is *NOT* JSON Schema. It validates json by checking if object have identically
|
|
|
|
|
This is *NOT* JSON Schema. It validates json by checking if object have identically |
|
|
|
|
named fields with matching types. |
|
|
|
|
For example schema {"name":"", "age":0} will validate
|
|
|
|
|
For example schema {"name":"", "age":0} will validate |
|
|
|
|
{"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. |
|
|
|
@ -88,7 +96,7 @@ int json_value_equals(const JSON_Value *a, const JSON_Value *b); |
|
|
|
|
null validates values of every type. |
|
|
|
|
*/ |
|
|
|
|
JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* JSON Object |
|
|
|
|
*/ |
|
|
|
@ -113,7 +121,7 @@ int json_object_dotget_boolean(const JSON_Object *object, const char * |
|
|
|
|
/* Functions to get available names */ |
|
|
|
|
size_t json_object_get_count(const JSON_Object *object); |
|
|
|
|
const char * json_object_get_name (const JSON_Object *object, size_t index); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Creates new name-value pair or frees and replaces old value with new one. */ |
|
|
|
|
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); |
|
|
|
@ -136,9 +144,9 @@ 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); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*JSON Array
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*JSON Array |
|
|
|
|
*/ |
|
|
|
|
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); |
|
|
|
@ -147,7 +155,7 @@ JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); |
|
|
|
|
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 */ |
|
|
|
|
size_t json_array_get_count (const JSON_Array *array); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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); |
|
|
|
@ -169,7 +177,7 @@ JSON_Status json_array_append_string(JSON_Array *array, const char *string); |
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*JSON Value |
|
|
|
|
*/ |
|
|
|
@ -196,7 +204,7 @@ JSON_Array * json_array (const JSON_Value *value); |
|
|
|
|
const char * json_string (const JSON_Value *value); |
|
|
|
|
double json_number (const JSON_Value *value); |
|
|
|
|
int json_boolean(const JSON_Value *value); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|