AOJLS
0.1
CJSONparsing/generatinglibrary
|
AOJLS definitions. More...
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Go to the source code of this file.
Data Structures | |
struct | aojls_serialization_prefs |
Serialization preferences. More... | |
struct | aojls_deserialization_prefs |
Deserialization preferences. More... | |
Macros | |
#define | AOJLS_OBJECT_START_ALLOC_SIZE 16 |
#define | AOJLS_ARRAY_START_ALLOC_SIZE 16 |
Typedefs | |
typedef struct aojls_ctx | aojls_ctx_t |
AOJLS Context object, holding all memory references to JSON values and strings. | |
typedef struct json_value | json_value_t |
Unified type for any JSON value. More... | |
typedef struct json_object | json_object |
JSON object. More... | |
typedef struct json_array | json_array |
JSON array. More... | |
typedef struct json_string | json_string |
JSON string. More... | |
typedef struct json_number | json_number |
JSON number. | |
typedef struct json_boolean | json_boolean |
JSON boolean. | |
typedef struct json_null | json_null |
JSON null. | |
typedef bool(* | writer_function_t) (const char *buffer, size_t len, void *writer_data) |
Custom serialization callback. More... | |
typedef long(* | reader_function_t) (char *buffer, size_t len, void *reader_data) |
Custom deserialization callback. More... | |
Enumerations | |
enum | json_type_t { JS_OBJECT, JS_ARRAY, JS_NUMBER, JS_STRING, JS_BOOL, JS_NULL, INVALID } |
JSON value tags. More... | |
Functions | |
json_type_t | json_get_type (json_value_t *value) |
Returns type for provided value. More... | |
json_object * | json_as_object (json_value_t *value) |
Converts reference to JSON value to JSON object. More... | |
json_array * | json_as_array (json_value_t *value) |
Converts reference to JSON value to JSON array. More... | |
double | json_as_number (json_value_t *value, bool *correct_type) |
Converts reference to numeric double value. More... | |
char * | json_as_string (json_value_t *value) |
Converts reference to string. More... | |
bool | json_as_bool (json_value_t *value, bool *correct_type) |
Converts reference to boolean. More... | |
bool | json_is_null (json_value_t *value) |
Checks if reference is JSON null. More... | |
json_object * | json_make_object (aojls_ctx_t *ctx) |
Creates new empty JSON object. More... | |
json_object * | json_object_add (json_object *o, char *key, json_value_t *value) |
Adds key-value pair to this JSON object. More... | |
json_object * | json_object_nadd (json_object *o, char *key, size_t len, json_value_t *value) |
Adds key-value pair to this JSON object. More... | |
size_t | json_object_numkeys (json_object *o) |
char * | json_object_get_key (json_object *o, size_t i) |
Returns key bound to the iteration position i . More... | |
json_value_t * | json_object_get_object_as_value (json_object *o, char *key) |
Returns JSON value bound to this key. More... | |
json_object * | json_object_get_object (json_object *o, char *key) |
Returns JSON object bound to this key. More... | |
json_array * | json_object_get_array (json_object *o, char *key) |
Returns JSON array bound to this key. More... | |
double | json_object_get_double (json_object *o, char *key, bool *valid) |
Returns double bound to this key. More... | |
double | json_object_get_double_default (json_object *o, char *key, double defval) |
Returns double bound to this key or default value. More... | |
char * | json_object_get_string (json_object *o, char *key) |
Returns string bound to this key. More... | |
char * | json_object_get_string_default (json_object *o, char *key, char *defval) |
Returns string bound to this key or default value. More... | |
bool | json_object_get_bool (json_object *o, char *key, bool *valid) |
Returns boolean bound to this key. More... | |
bool | json_object_get_bool_default (json_object *o, char *key, bool defval) |
bool | json_object_is_null (json_object *o, char *key) |
Returns if null is bound for that key. More... | |
json_array * | json_make_array (aojls_ctx_t *ctx) |
Creates new empty JSON array. More... | |
json_array * | json_array_add (json_array *a, json_value_t *value) |
Adds new JSON value to the JSON array. More... | |
size_t | json_array_size (json_array *a) |
Returns number of elements in this array. More... | |
json_value_t * | json_array_get (json_array *a, size_t i) |
Returns JSON value at position i . More... | |
json_object * | json_array_get_object (json_array *a, size_t i) |
Returns JSON object in JSON array at position i . More... | |
json_array * | json_array_get_array (json_array *a, size_t i) |
Returns JSON array in JSON array at position i . More... | |
double | json_array_get_double (json_array *a, size_t i, bool *valid) |
Returns double value in JSON array at position i . More... | |
double | json_array_get_double_default (json_array *a, size_t i, double defval) |
Returns double value in JSON array at position i or default value. More... | |
char * | json_array_get_string (json_array *a, size_t i) |
Returns string in JSON array at position i . More... | |
char * | json_array_get_string_default (json_array *a, size_t i, char *defval) |
Returns string in JSON array at position i or default. More... | |
bool | json_array_get_bool (json_array *a, size_t i, bool *valid) |
Returns boolean value in JSON array at position i . More... | |
bool | json_array_get_bool_default (json_array *a, size_t i, bool defval) |
Returns boolean value in JSON array at position i or default value. More... | |
bool | json_array_is_null (json_array *a, size_t i) |
Returns whether JSON null is at the position i . More... | |
json_string * | json_from_string (aojls_ctx_t *ctx, char *string) |
Creates JSON string from string . More... | |
json_number * | json_from_number (aojls_ctx_t *ctx, double number) |
Creates JSON number from number . More... | |
json_boolean * | json_from_boolean (aojls_ctx_t *ctx, bool b) |
Creates JSON boolean from b . More... | |
json_null * | json_make_null (aojls_ctx_t *ctx) |
Creates JSON null. More... | |
aojls_ctx_t * | json_make_context () |
Creates new AOJLS context. More... | |
bool | json_context_error_happened (aojls_ctx_t *ctx) |
Whether any error is detected while manipulating with this context or values bound to it. More... | |
json_value_t * | json_context_get_result (aojls_ctx_t *ctx) |
void | json_free_context (aojls_ctx_t *ctx) |
frees the context and all bound values More... | |
char * | aojls_serialize (json_value_t *value, aojls_serialization_prefs *prefs) |
Serializes JSON value. More... | |
aojls_ctx_t * | aojls_deserialize (char *source, size_t len, aojls_deserialization_prefs *prefs) |
Deserialization function. More... | |
AOJLS definitions.
Copyright (c) 2016, Peter Vanusanik All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
typedef struct json_array json_array |
JSON array.
JSON array is an array of JSON values. json_array references are one-way mutabe, ie new value can be inserted, however no value can be removed
typedef struct json_object json_object |
JSON object.
JSON object is a map between string keys and JSON values. In AOLJS you can have multiple identical keys, which will be no problem (however, serialized form might not be valid JSON).
json_object references are one-way mutable, ie new key-value pairs might be defined, but none can be removed.
typedef struct json_string json_string |
JSON string.
immutable, also string value is also tracked by context
typedef struct json_value json_value_t |
Unified type for any JSON value.
All JSON value references can be casted into this.
typedef long(* reader_function_t) (char *buffer, size_t len, void *reader_data) |
Custom deserialization callback.
This callback is called each time deserializer needs to read new data. This callback must return number of bytes actually read in the buffer and it must be <= len, or negative in case of failure. If 0 is returned, deserializer considers that as EOF.
typedef bool(* writer_function_t) (const char *buffer, size_t len, void *writer_data) |
Custom serialization callback.
This callback is called each time serializer needs to write byte output. writer_data is user provided writer state that is passed in the preferences
enum json_type_t |
JSON value tags.
Every JSON value is identified by one of these tags
aojls_ctx_t* aojls_deserialize | ( | char * | source, |
size_t | len, | ||
aojls_deserialization_prefs * | prefs | ||
) |
Deserialization function.
source | string containing JSON data, may be NULL if custom reader is used instead |
len | size of previous string, if applicable |
prefs | preferences used for this deserialization |
char* aojls_serialize | ( | json_value_t * | value, |
aojls_serialization_prefs * | prefs | ||
) |
Serializes JSON value.
Serializes the JSON value according to the preferences specified. Returns serialized string if no writer_function is specified or NULL in case of error or when custom writer_function is specified.
value | to be serialized |
prefs | preferences, may be NULL |
json_array* json_array_add | ( | json_array * | a, |
json_value_t * | value | ||
) |
Adds new JSON value to the JSON array.
a | JSON array |
value | JSON value |
json_value_t* json_array_get | ( | json_array * | a, |
size_t | i | ||
) |
Returns JSON value at position i
.
a | array |
i | position |
json_array* json_array_get_array | ( | json_array * | a, |
size_t | i | ||
) |
Returns JSON array in JSON array at position i
.
a | array |
i | position |
bool json_array_get_bool | ( | json_array * | a, |
size_t | i, | ||
bool * | valid | ||
) |
Returns boolean value in JSON array at position i
.
a | array |
i | position |
valid | will contain true if position is valid and JSON boolean is at that position or false |
bool json_array_get_bool_default | ( | json_array * | a, |
size_t | i, | ||
bool | defval | ||
) |
Returns boolean value in JSON array at position i
or default value.
a | array |
i | position |
defval | returned in case of failure |
double json_array_get_double | ( | json_array * | a, |
size_t | i, | ||
bool * | valid | ||
) |
Returns double value in JSON array at position i
.
a | array |
i | position |
valid | will contain true if position is valid and JSON number is at that position or false |
double json_array_get_double_default | ( | json_array * | a, |
size_t | i, | ||
double | defval | ||
) |
Returns double value in JSON array at position i
or default value.
a | array |
i | position |
defval | returned in case of failure |
json_object* json_array_get_object | ( | json_array * | a, |
size_t | i | ||
) |
Returns JSON object in JSON array at position i
.
a | array |
i | position |
char* json_array_get_string | ( | json_array * | a, |
size_t | i | ||
) |
Returns string in JSON array at position i
.
a | array |
i | position |
char* json_array_get_string_default | ( | json_array * | a, |
size_t | i, | ||
char * | defval | ||
) |
Returns string in JSON array at position i
or default.
a | array |
i | position |
defval | is returned in any case of failure |
bool json_array_is_null | ( | json_array * | a, |
size_t | i | ||
) |
Returns whether JSON null is at the position i
.
a | array |
i | position |
size_t json_array_size | ( | json_array * | a | ) |
Returns number of elements in this array.
a | JSON array |
json_array* json_as_array | ( | json_value_t * | value | ) |
Converts reference to JSON value to JSON array.
If value
is NULL, returns NULL, otherwise returns reference to json_array, if value is JSON array or NULL.
value | JSON value |
bool json_as_bool | ( | json_value_t * | value, |
bool * | correct_type | ||
) |
Converts reference to boolean.
Returns false and sets correct_type
to false if JSON value is invalid or not a boolean, otherwise correct_type
is set to true and value is converted to boolean.
value | JSON value |
checker,if | not NULL, state of conversion is stored there |
double json_as_number | ( | json_value_t * | value, |
bool * | correct_type | ||
) |
Converts reference to numeric double value.
Returns 0 and sets correct_type
to false if JSON value is invalid or not a number, otherwise correct_type
is set to true and value is converted to double.
value | JSON value |
checker,if | not NULL, state of conversion is stored there |
json_object* json_as_object | ( | json_value_t * | value | ) |
Converts reference to JSON value to JSON object.
If value
is NULL, returns NULL, otherwise returns reference to json_object, if value is JSON object or NULL.
value | JSON value |
char* json_as_string | ( | json_value_t * | value | ) |
Converts reference to string.
Returns NULL if string is invalid.
value | JSON value |
bool json_context_error_happened | ( | aojls_ctx_t * | ctx | ) |
Whether any error is detected while manipulating with this context or values bound to it.
json_value_t* json_context_get_result | ( | aojls_ctx_t * | ctx | ) |
this context was used in deserialization, returns result of last deserialization
void json_free_context | ( | aojls_ctx_t * | ctx | ) |
frees the context and all bound values
Frees all the memory used by all bound values in this context, all strings used by those values and then context itself.
json_boolean* json_from_boolean | ( | aojls_ctx_t * | ctx, |
bool | b | ||
) |
Creates JSON boolean from b
.
Failure is marked in ctx
.
ctx | context to which this JSON boolean will be bound |
b | boolean value |
json_number* json_from_number | ( | aojls_ctx_t * | ctx, |
double | number | ||
) |
Creates JSON number from number
.
Failure is marked in ctx
.
ctx | context to which this JSON number will be bound |
number | value |
json_string* json_from_string | ( | aojls_ctx_t * | ctx, |
char * | string | ||
) |
Creates JSON string from string
.
Failure is marked in ctx
.
ctx | context to which this JSON string will be bound |
string | value |
string
may be deallocated/modified after this call json_type_t json_get_type | ( | json_value_t * | value | ) |
Returns type for provided value.
If NULL pointer is passed as value
, returns INVALID. Otherwise returns the type of that JSON value.
value | queried JSON value |
bool json_is_null | ( | json_value_t * | value | ) |
Checks if reference is JSON null.
value | JSON value |
json_array* json_make_array | ( | aojls_ctx_t * | ctx | ) |
Creates new empty JSON array.
Failure is marked in ctx
.
ctx | context to which this JSON array will be bound |
aojls_ctx_t* json_make_context | ( | ) |
Creates new AOJLS context.
All JSON value creating operations require valid context. All JSON value's memory is then tracked by this context and therefore should not be freed explicitedly.
json_null* json_make_null | ( | aojls_ctx_t * | ctx | ) |
Creates JSON null.
Failure is marked in ctx
.
ctx | context to which this JSON boolean will be bound |
json_object* json_make_object | ( | aojls_ctx_t * | ctx | ) |
Creates new empty JSON object.
Failure is marked in ctx
.
ctx | context to which this JSON object will be bound |
json_object* json_object_add | ( | json_object * | o, |
char * | key, | ||
json_value_t * | value | ||
) |
Adds key-value pair to this JSON object.
o | JSON object |
key | null terminated string value to be bound to this key |
json_array* json_object_get_array | ( | json_object * | o, |
char * | key | ||
) |
Returns JSON array bound to this key.
bool json_object_get_bool | ( | json_object * | o, |
char * | key, | ||
bool * | valid | ||
) |
Returns boolean bound to this key.
o | JSON object |
key | key |
valid | will contain true if position is valid and JSON boolean is at that position or false |
double json_object_get_double | ( | json_object * | o, |
char * | key, | ||
bool * | valid | ||
) |
Returns double bound to this key.
o | JSON object |
key | key |
valid | will contain true if position is valid and JSON number is at that position or false |
double json_object_get_double_default | ( | json_object * | o, |
char * | key, | ||
double | defval | ||
) |
Returns double bound to this key or default value.
o | JSON object |
key | key |
defval | default value |
defval
in case of an error, if there is no such key or if key points to other than JSON number JSON value. char* json_object_get_key | ( | json_object * | o, |
size_t | i | ||
) |
Returns key bound to the iteration position i
.
o | JSON object |
i | iterator, i <= number of keys in JSON object |
json_object* json_object_get_object | ( | json_object * | o, |
char * | key | ||
) |
Returns JSON object bound to this key.
json_value_t* json_object_get_object_as_value | ( | json_object * | o, |
char * | key | ||
) |
Returns JSON value bound to this key.
O(N) complexity where N is number of keys in this JSON object. Keys are compared via strcmp.
o | JSON object |
key |
char* json_object_get_string | ( | json_object * | o, |
char * | key | ||
) |
Returns string bound to this key.
o | JSON object |
key | key |
char* json_object_get_string_default | ( | json_object * | o, |
char * | key, | ||
char * | defval | ||
) |
Returns string bound to this key or default value.
o | JSON object |
key | key |
defval | default value |
defval
in case of an error, if there is no such key or if key points to other than JSON string JSON value. bool json_object_is_null | ( | json_object * | o, |
char * | key | ||
) |
Returns if null is bound for that key.
o | JSON object |
key | key |
json_object* json_object_nadd | ( | json_object * | o, |
char * | key, | ||
size_t | len, | ||
json_value_t * | value | ||
) |
Adds key-value pair to this JSON object.
o | JSON object |
key | string |
len | size of string key value to be bound to this key |
size_t json_object_numkeys | ( | json_object * | o | ) |