JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
jstools.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "stdbool.h"
4 #include "stdint.h"
5 #include "stdio.h"
6 
10 typedef struct {
11  struct JST_ArrayItem_ ** items;
12  unsigned count;
13  struct JST_Element_ * parent;
14 } JST_Array;
15 
19 extern const JST_Array JST_Array_Zero;
20 
24 typedef struct {
25  struct JST_Pair_ ** items;
26  unsigned count;
27  struct JST_Element_ * parent;
28  struct JST_Pair_ * first;
29 } JST_Object;
30 
34 extern const JST_Object JST_Object_Zero;
35 
40 typedef union {
43  bool boolean;
44  int64_t integer;
45  double dbl;
46  char * string;
47 } JST_Value;
48 
52 extern const JST_Value JST_Value_Zero;
53 
57 typedef enum {
66 
70 typedef struct JST_Element_ {
73 } JST_Element;
74 
78 extern const JST_Element JST_Element_Zero;
79 
83 typedef struct JST_ArrayItem_ {
87 
89 
93 typedef struct JST_Pair_ {
96  char * name;
97  struct JST_Pair_ * next;
98 } JST_Pair;
99 
103 extern const JST_Pair JST_Pair_Zero;
104 
108 typedef enum {
129 } JST_Error;
130 
134 typedef bool (* JST_Visitor )( const char * name, unsigned index, bool pre, const JST_Element * element, void * user_context );
135 
139 typedef struct {
140  unsigned line;
141  char context[80];
142  unsigned pos;
144 
149 
159 JST_Error JST_load_from_file( const char * filepath, JST_Element * root, JST_SyntaxError * error );
160 
170 JST_Error JST_load_from_stream( FILE * stream, JST_Element * root, JST_SyntaxError * error );
171 
178 JST_Error JST_save_to_stream( FILE * stream, const JST_Element * root, unsigned indent );
179 
186 JST_Error JST_save_to_file( const char * filepath, const JST_Element * root, unsigned indent );
187 
193 JST_Error JST_save_to_stream_compact( FILE * stream, const JST_Element * root );
194 
200 JST_Error JST_save_to_file_compact( const char * filepath, const JST_Element * root );
201 
210 JST_Error JST_serialize( JST_Element * root, char ** dest, unsigned indent );
211 
219 JST_Error JST_serialize_compact( JST_Element * root, char ** dest );
220 
228 JST_Error JST_get( const char * jsonpath, JST_Element * root, JST_Element ** dest );
229 
238 JST_Error JST_add_property( JST_Object * object, unsigned index, const char * name, const JST_Element * value );
239 
240 JST_Error JST_remove_property( JST_Object * object, const char * name );
241 
242 JST_Error JST_add_item( JST_Array * array, const JST_Element * value );
243 
244 JST_Error JST_remove_item( JST_Array * array, unsigned index );
245 
246 JST_Error JST_replace_item( JST_Array * array, unsigned index, const JST_Element * value );
247 
248 JST_Error JST_walk( const JST_Element * root, JST_Visitor visitor, void * user_context );
249 
250 JST_Error JST_delete_pair( JST_Pair * element );
251 
253 
255 
257 
struct JST_Pair_ ** items
Array of JST_Pair, ordered by JST_Pair.name to ease search with bsearch()
Definition: jstools.h:25
char * name
This property's name.
Definition: jstools.h:96
'false' token was expected
Definition: jstools.h:120
const JST_Object JST_Object_Zero
Constant defined to initialize safely a variable of type JST_Object.
Definition: JST_load.c:17
token is not a colon
Definition: jstools.h:123
only ", \, /, b, f, n, r, t, u may be escaped
Definition: jstools.h:114
const JST_ArrayItem JST_ArrayItem_Zero
Definition: JST_load.c:20
A JSON Value may be an object, an array, a boolean, a number or a string.
Definition: jstools.h:40
JST_Error JST_delete_object(JST_Object *object)
Definition: JST_delete.c:15
syntax error or 'null'
Definition: jstools.h:118
'true' token was expected
Definition: jstools.h:119
const JST_Array JST_Array_Zero
Constant defined to initialize safely a variable of type JST_Array.
Definition: JST_load.c:16
token is not a number
Definition: jstools.h:122
JST_Error JST_remove_property(JST_Object *object, const char *name)
Definition: JST_update.c:124
const JST_SyntaxError JST_SyntaxError_Zero
Constant defined to initialize safely a variable of type JST_SyntaxError.
Definition: JST_load.c:22
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
A JSON array.
Definition: jstools.h:10
JST_Element element
Definition: jstools.h:85
An array item has a parent and a typed value.
Definition: jstools.h:83
JST_Error JST_remove_item(JST_Array *array, unsigned index)
Definition: JST_update.c:140
A type field has been set to JST_NONE.
Definition: jstools.h:111
char * string
Definition: jstools.h:46
JST_Error JST_save_to_stream_compact(FILE *stream, const JST_Element *root)
Save the JSON tree to a stream, as a single line of text, without whitespaces.
Definition: JST_save.c:112
ending quote has not been found
Definition: jstools.h:113
const JST_Element JST_Element_Zero
Constant defined to initialize safely a variable of type JST_Element.
Definition: JST_load.c:19
syntax error or 'false'
Definition: jstools.h:117
A JSON object, a sorted set of named-value pairs.
Definition: jstools.h:24
JST_Object object
Definition: jstools.h:41
struct JST_ArrayItem_ JST_ArrayItem
An array item has a parent and a typed value.
'null' token was expected
Definition: jstools.h:121
index is out of range
Definition: jstools.h:127
bool boolean
Definition: jstools.h:43
syntax error or 'true'
Definition: jstools.h:116
JST_Value value
Definition: jstools.h:72
JST_Error JST_add_property(JST_Object *object, unsigned index, const char *name, const JST_Element *value)
Add a named-value pair to an object.
Definition: JST_update.c:60
JST_Error JST_get(const char *jsonpath, JST_Element *root, JST_Element **dest)
Search an element by its path.
Definition: JST_get.c:10
JST_Error JST_delete_pair(JST_Pair *element)
Definition: JST_delete.c:5
JST_Visitor has returned false to interrupt the tree's iteration.
Definition: jstools.h:128
const JST_Pair JST_Pair_Zero
Constant defined to initialize safely a variable of type JST_Pair.
Definition: JST_load.c:21
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
struct JST_Pair_ * first
A linked list is mandatory to preserve the properties's order.
Definition: jstools.h:28
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
An element is a typed value.
Definition: jstools.h:70
Information returned by JST_load() in case of error.
Definition: jstools.h:139
JST_Error JST_save_to_stream(FILE *stream, const JST_Element *root, unsigned indent)
Save the JSON tree to a stream, pretty-printed.
Definition: JST_save.c:47
JST_Array array
Definition: jstools.h:42
bool(* JST_Visitor)(const char *name, unsigned index, bool pre, const JST_Element *element, void *user_context)
Visitor in depth first with 'pre' set to true then backtrack with 'pre' set to false.
Definition: jstools.h:134
double dbl
Definition: jstools.h:45
JST_Object * parent
This property's owner.
Definition: jstools.h:94
struct JST_Pair_ * next
A linked list is mandatory to preserve the properties's order.
Definition: jstools.h:97
int64_t integer
Definition: jstools.h:44
struct JST_Element_ * parent
of type JST_ArrayItem or JST_Pair, case selector is parent->type
Definition: jstools.h:27
JST_Array * parent
Definition: jstools.h:84
JST_Error JST_add_item(JST_Array *array, const JST_Element *value)
Definition: JST_update.c:89
unsigned line
Last line's index (from 1)
Definition: jstools.h:140
Path syntax is (<name>|[<integer>])(.<name>|[<integer>])*.
Definition: jstools.h:125
JST_Error JST_load_from_stream(FILE *stream, JST_Element *root, JST_SyntaxError *error)
Reads the input stream, allocates the node of the corresponding tree, than frees the input buffer...
Definition: JST_load.c:533
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
JST_Error JST_save_to_file(const char *filepath, const JST_Element *root, unsigned indent)
Save the JSON tree to a file, pretty-printed.
Definition: JST_save.c:56
JST_Error JST_serialize(JST_Element *root, char **dest, unsigned indent)
Serialize the JSON tree to a string, pretty-printed.
JST_ValueType
The JSON Value types.
Definition: jstools.h:57
const JST_Value JST_Value_Zero
Constant defined to initialize safely a variable of type JST_Value.
Definition: JST_load.c:18
JST_Error JST_save_to_file_compact(const char *filepath, const JST_Element *root)
Save the JSON tree to a file, as a single line of text, without whitespaces.
Definition: JST_save.c:120
JST_Error JST_serialize_compact(JST_Element *root, char **dest)
Serialize the JSON tree to a string as a single line of text, without whitespaces.
struct JST_Pair_ JST_Pair
A object attribute item has a parent and is a named-typed-value pair.
struct JST_Element_ JST_Element
An element is a typed value.
JST_Error JST_delete_array(JST_Array *array)
Definition: JST_delete.c:40
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
JST_Error JST_load_from_file(const char *filepath, JST_Element *root, JST_SyntaxError *error)
Reads the input file, allocates the node of the corresponding tree, than frees the input buffer...
Definition: JST_load.c:573
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
JST_ValueType type
Definition: jstools.h:71
struct JST_Element_ * parent
of type JST_ArrayItem or JST_Pair, case selector is parent->type
Definition: jstools.h:13
JST_Error JST_delete_array_item(JST_ArrayItem *array)
Definition: JST_delete.c:31
JST_Element element
The value associated with the name.
Definition: jstools.h:95
JST_Error JST_walk(const JST_Element *root, JST_Visitor visitor, void *user_context)
Definition: JST_walk.c:45
JST_Error JST_replace_item(JST_Array *array, unsigned index, const JST_Element *value)
Definition: JST_update.c:114
JST_get() has no result.
Definition: jstools.h:126
unexpected token
Definition: jstools.h:124
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26
unsigned pos
Position of error in context.
Definition: jstools.h:142