JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
Data Structures | Typedefs | Enumerations | Functions | Variables
jstools.h File Reference
#include "stdbool.h"
#include "stdint.h"
#include "stdio.h"
Include dependency graph for jstools.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  JST_Array
 A JSON array. More...
 
struct  JST_Object
 A JSON object, a sorted set of named-value pairs. More...
 
union  JST_Value
 A JSON Value may be an object, an array, a boolean, a number or a string. More...
 
struct  JST_Element_
 An element is a typed value. More...
 
struct  JST_ArrayItem_
 An array item has a parent and a typed value. More...
 
struct  JST_Pair_
 A object attribute item has a parent and is a named-typed-value pair. More...
 
struct  JST_SyntaxError
 Information returned by JST_load() in case of error. More...
 

Typedefs

typedef struct JST_Element_ JST_Element
 An element is a typed value. More...
 
typedef struct JST_ArrayItem_ JST_ArrayItem
 An array item has a parent and a typed value. More...
 
typedef struct JST_Pair_ JST_Pair
 A object attribute item has a parent and is a named-typed-value pair. More...
 
typedef 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. More...
 

Enumerations

enum  JST_ValueType {
  JST_OBJECT, JST_ARRAY, JST_BOOLEAN, JST_INTEGER,
  JST_DOUBLE, JST_STRING, JST_NULL
}
 The JSON Value types. More...
 
enum  JST_Error {
  JST_ERR_NONE, JST_ERR_NULL_ARGUMENT, JST_ERR_NULL_TYPE, JST_ERR_ERRNO,
  JST_ERR_STRING_NOT_ENDED, JST_ERR_STRING_BAD_ESCAPE_SEQUENCE, JST_ERR_STRING_CONTROL_CHAR_MUST_BE_ESCAPED, JST_ERR_SEPARATOR_EXPECTED_AFTER_TRUE,
  JST_ERR_SEPARATOR_EXPECTED_AFTER_FALSE, JST_ERR_SEPARATOR_EXPECTED_AFTER_NULL, JST_ERR_TRUE_EXPECTED, JST_ERR_FALSE_EXPECTED,
  JST_ERR_NULL_EXPECTED, JST_ERR_NUMBER_EXPECTED, JST_ERR_COLON_EXPECTED, JST_ERR_UNEXPECTED_TOKEN,
  JST_ERR_PATH_SYNTAX, JST_ERR_NOT_FOUND, JST_ERR_ARRAY_INDEX_OUT_OF_RANGE, JST_ERR_INTERRUPTED
}
 When things goes wrong, an error information is given. More...
 

Functions

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. More...
 
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. More...
 
JST_Error JST_save_to_stream (FILE *stream, const JST_Element *root, unsigned indent)
 Save the JSON tree to a stream, pretty-printed. More...
 
JST_Error JST_save_to_file (const char *filepath, const JST_Element *root, unsigned indent)
 Save the JSON tree to a file, pretty-printed. More...
 
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. More...
 
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. More...
 
JST_Error JST_serialize (JST_Element *root, char **dest, unsigned indent)
 Serialize the JSON tree to a string, pretty-printed. More...
 
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. More...
 
JST_Error JST_get (const char *jsonpath, JST_Element *root, JST_Element **dest)
 Search an element by its path. More...
 
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. More...
 
JST_Error JST_remove_property (JST_Object *object, const char *name)
 
JST_Error JST_add_item (JST_Array *array, const JST_Element *value)
 
JST_Error JST_remove_item (JST_Array *array, unsigned index)
 
JST_Error JST_replace_item (JST_Array *array, unsigned index, const JST_Element *value)
 
JST_Error JST_walk (const JST_Element *root, JST_Visitor visitor, void *user_context)
 
JST_Error JST_delete_pair (JST_Pair *element)
 
JST_Error JST_delete_object (JST_Object *object)
 
JST_Error JST_delete_array_item (JST_ArrayItem *array)
 
JST_Error JST_delete_array (JST_Array *array)
 
JST_Error JST_delete_element (JST_Element *element)
 

Variables

const JST_Array JST_Array_Zero
 Constant defined to initialize safely a variable of type JST_Array. More...
 
const JST_Object JST_Object_Zero
 Constant defined to initialize safely a variable of type JST_Object. More...
 
const JST_Value JST_Value_Zero
 Constant defined to initialize safely a variable of type JST_Value. More...
 
const JST_Element JST_Element_Zero
 Constant defined to initialize safely a variable of type JST_Element. More...
 
const JST_ArrayItem JST_ArrayItem_Zero
 
const JST_Pair JST_Pair_Zero
 Constant defined to initialize safely a variable of type JST_Pair. More...
 
const JST_SyntaxError JST_SyntaxError_Zero
 Constant defined to initialize safely a variable of type JST_SyntaxError. More...
 

Typedef Documentation

◆ JST_ArrayItem

typedef struct JST_ArrayItem_ JST_ArrayItem

An array item has a parent and a typed value.

◆ JST_Element

typedef struct JST_Element_ JST_Element

An element is a typed value.

◆ JST_Pair

typedef struct JST_Pair_ JST_Pair

A object attribute item has a parent and is a named-typed-value pair.

◆ JST_Visitor

typedef 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 at line 134 of file jstools.h.

Enumeration Type Documentation

◆ JST_Error

enum JST_Error

When things goes wrong, an error information is given.

Enumerator
JST_ERR_NONE 

All correct

JST_ERR_NULL_ARGUMENT 

Function misused.

JST_ERR_NULL_TYPE 

A type field has been set to JST_NONE.

JST_ERR_ERRNO 

strerror() or perror() can be used to show the operating system layer error

JST_ERR_STRING_NOT_ENDED 

ending quote has not been found

JST_ERR_STRING_BAD_ESCAPE_SEQUENCE 

only ", \, /, b, f, n, r, t, u may be escaped

JST_ERR_STRING_CONTROL_CHAR_MUST_BE_ESCAPED 

a control character has been found, it must be escaped

JST_ERR_SEPARATOR_EXPECTED_AFTER_TRUE 

syntax error or 'true'

JST_ERR_SEPARATOR_EXPECTED_AFTER_FALSE 

syntax error or 'false'

JST_ERR_SEPARATOR_EXPECTED_AFTER_NULL 

syntax error or 'null'

JST_ERR_TRUE_EXPECTED 

'true' token was expected

JST_ERR_FALSE_EXPECTED 

'false' token was expected

JST_ERR_NULL_EXPECTED 

'null' token was expected

JST_ERR_NUMBER_EXPECTED 

token is not a number

JST_ERR_COLON_EXPECTED 

token is not a colon

JST_ERR_UNEXPECTED_TOKEN 

unexpected token

JST_ERR_PATH_SYNTAX 

Path syntax is (<name>|[<integer>])(.<name>|[<integer>])*.

JST_ERR_NOT_FOUND 

JST_get() has no result.

JST_ERR_ARRAY_INDEX_OUT_OF_RANGE 

index is out of range

JST_ERR_INTERRUPTED 

JST_Visitor has returned false to interrupt the tree's iteration.

Definition at line 108 of file jstools.h.

108  {
109  JST_ERR_NONE,
112  JST_ERR_ERRNO,
129 } JST_Error;
&#39;false&#39; token was expected
Definition: jstools.h:120
token is not a colon
Definition: jstools.h:123
only ", \, /, b, f, n, r, t, u may be escaped
Definition: jstools.h:114
syntax error or &#39;null&#39;
Definition: jstools.h:118
&#39;true&#39; token was expected
Definition: jstools.h:119
token is not a number
Definition: jstools.h:122
A type field has been set to JST_NONE.
Definition: jstools.h:111
ending quote has not been found
Definition: jstools.h:113
syntax error or &#39;false&#39;
Definition: jstools.h:117
&#39;null&#39; token was expected
Definition: jstools.h:121
index is out of range
Definition: jstools.h:127
syntax error or &#39;true&#39;
Definition: jstools.h:116
JST_Visitor has returned false to interrupt the tree&#39;s iteration.
Definition: jstools.h:128
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Path syntax is (<name>|[<integer>])(.<name>|[<integer>])*.
Definition: jstools.h:125
Function misused.
Definition: jstools.h:110
JST_get() has no result.
Definition: jstools.h:126
unexpected token
Definition: jstools.h:124

◆ JST_ValueType

The JSON Value types.

Enumerator
JST_OBJECT 
JST_ARRAY 
JST_BOOLEAN 
JST_INTEGER 
JST_DOUBLE 
JST_STRING 
JST_NULL 

Definition at line 57 of file jstools.h.

57  {
58  JST_OBJECT,
59  JST_ARRAY,
62  JST_DOUBLE,
63  JST_STRING,
64  JST_NULL,
JST_ValueType
The JSON Value types.
Definition: jstools.h:57

Function Documentation

◆ JST_add_item()

JST_Error JST_add_item ( JST_Array array,
const JST_Element value 
)

Definition at line 89 of file JST_update.c.

89  {
90  JST_ArrayItem * item = calloc( 1, sizeof( JST_ArrayItem ));
91  if( ! item ) {
92  return JST_ERR_ERRNO;
93  }
94  item->parent = array;
95  JST_Error error = element_copy( &(item->element), value );
96  if( error == JST_ERR_NONE ) {
97  ++(array->count);
98  array->items = realloc( array->items, array->count * sizeof( JST_ArrayItem * ));
99  if( array->items == NULL ) {
100  array->count = 0;
101  error = JST_ERR_ERRNO;
102  }
103  else {
104  array->items[array->count-1] = item;
105  }
106  }
107  if( error != JST_ERR_NONE ) {
108  JST_delete_element( &(item->element));
109  free( item );
110  }
111  return error;
112 }
JST_Element element
Definition: jstools.h:85
An array item has a parent and a typed value.
Definition: jstools.h:83
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
JST_Array * parent
Definition: jstools.h:84
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12

◆ JST_add_property()

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.

Parameters
objectthe target object to modify
indexthe property's rank, -1U to add at the end
namethe property's name
valuethe property's value
Returns
JST_ERR_NONE on success, JST_ERR_NULL_TYPE or JST_ERR_ERRNO on failure.

Definition at line 60 of file JST_update.c.

60  {
61  (void)index;
62  JST_Pair * item = calloc( 1, sizeof( JST_Pair ));
63  if( ! item ) {
64  return JST_ERR_ERRNO;
65  }
66  item->name = strdup( name );
67  item->parent = object;
68  JST_Error error = element_copy( &(item->element), value );
69  if( error == JST_ERR_NONE ) {
70  ++(object->count);
71  object->items = realloc( object->items, object->count * sizeof( JST_Pair * ));
72  if( object->items == NULL ) {
73  object->count = 0;
74  error = JST_ERR_ERRNO;
75  }
76  else {
77  object->items[object->count-1] = item;
78  qsort( object->items, object->count, sizeof( JST_Pair *), JST_pairs_compare );
79  }
80  }
81  if( error != JST_ERR_NONE ) {
82  JST_delete_element( &(item->element));
83  free( item->name );
84  free( item );
85  }
86  return error;
87 }
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&#39;s name.
Definition: jstools.h:96
int JST_pairs_compare(const void *left, const void *right)
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
JST_Object * parent
This property&#39;s owner.
Definition: jstools.h:94
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
JST_Element element
The value associated with the name.
Definition: jstools.h:95
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26

◆ JST_delete_array()

JST_Error JST_delete_array ( JST_Array array)

Definition at line 40 of file JST_delete.c.

40  {
41  if( array == NULL ) {
42  return JST_ERR_NULL_ARGUMENT;
43  }
44  JST_Error error = JST_ERR_NONE;
45  for( unsigned i = 0; ( error == JST_ERR_NONE )&&( i < array->count ); ++i ) {
46  error = JST_delete_array_item( array->items[i] );
47  }
48  free( array->items );
49  array->parent = NULL;
50  array->count = 0;
51  array->items = NULL;
52  return error;
53 }
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
JST_Error JST_delete_array_item(JST_ArrayItem *item)
Definition: JST_delete.c:31
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
struct JST_Element_ * parent
of type JST_ArrayItem or JST_Pair, case selector is parent->type
Definition: jstools.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ JST_delete_array_item()

JST_Error JST_delete_array_item ( JST_ArrayItem array)

Definition at line 31 of file JST_delete.c.

31  {
32  if( item == NULL ) {
33  return JST_ERR_NULL_ARGUMENT;
34  }
35  JST_Error error = JST_delete_element( &(item->element));
36  free( item );
37  return error;
38 }
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
Function misused.
Definition: jstools.h:110
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ JST_delete_element()

JST_Error JST_delete_element ( JST_Element element)

Definition at line 55 of file JST_delete.c.

55  {
56  if( element == NULL ) {
57  return JST_ERR_NULL_ARGUMENT;
58  }
59  switch( element->type ) {
60  case JST_OBJECT: JST_delete_object( &(element->value.object)); break;
61  case JST_ARRAY : JST_delete_array( &(element->value.array)); break;
62  case JST_STRING: free( element->value.string ); break;
63  default: break;
64  }
65  element->type = JST_NULL;
66  element->value.string = NULL;
67  return JST_ERR_NONE;
68 }
char * string
Definition: jstools.h:46
JST_Object object
Definition: jstools.h:41
JST_Value value
Definition: jstools.h:72
JST_Error JST_delete_array(JST_Array *array)
Definition: JST_delete.c:40
JST_Array array
Definition: jstools.h:42
Function misused.
Definition: jstools.h:110
JST_Error JST_delete_object(JST_Object *object)
Definition: JST_delete.c:15
JST_ValueType type
Definition: jstools.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ JST_delete_object()

JST_Error JST_delete_object ( JST_Object object)

Definition at line 15 of file JST_delete.c.

15  {
16  if( object == NULL ) {
17  return JST_ERR_NULL_ARGUMENT;
18  }
19  JST_Error error = JST_ERR_NONE;
20  for( unsigned i = 0; ( error == JST_ERR_NONE )&&( i < object->count ); ++i ) {
21  error = JST_delete_pair( object->items[i] );
22  }
23  free( object->items );
24  object->parent = NULL;
25  object->count = 0;
26  object->first = NULL;
27  object->items = NULL;
28  return error;
29 }
struct JST_Pair_ ** items
Array of JST_Pair, ordered by JST_Pair.name to ease search with bsearch()
Definition: jstools.h:25
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
JST_Error JST_delete_pair(JST_Pair *pair)
Definition: JST_delete.c:5
Function misused.
Definition: jstools.h:110
Here is the call graph for this function:
Here is the caller graph for this function:

◆ JST_delete_pair()

JST_Error JST_delete_pair ( JST_Pair element)

Definition at line 5 of file JST_delete.c.

5  {
6  if( pair == NULL ) {
8  }
9  JST_Error error = JST_delete_element( &(pair->element));
10  free( pair->name );
11  free( pair );
12  return error;
13 }
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
Function misused.
Definition: jstools.h:110
JST_Error JST_delete_element(JST_Element *element)
Definition: JST_delete.c:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ JST_get()

JST_Error JST_get ( const char *  jsonpath,
JST_Element root,
JST_Element **  dest 
)

Search an element by its path.

Parameters
jsonpathsyntax is (<name>|[<integer>])(.<name>|[<integer>])*
elementthe root of the search
destan address of a pointer to the element found
Returns
an error status, JST_ERR_NOT_FOUND when jsonpath doesn't match.

Definition at line 10 of file JST_get.c.

10  {
11  if(( root == NULL )||( jsonpath == NULL )||( dest == NULL )) {
12  return JST_ERR_NULL_ARGUMENT;
13  }
14  char * tokenized = strdup( jsonpath );
15  if( tokenized == NULL ) {
16  return JST_ERR_ERRNO;
17  }
18  char * token = strtok( tokenized, STRTOK_PATH );
19  if( token == NULL ) {
20  free( tokenized );
21  return JST_ERR_PATH_SYNTAX;
22  }
23  JST_Element * iter = root;
24  while( iter && token ) {
25  if( isdigit( *token )) {
26  char * err;
27  long unsigned index = strtoul( token, &err, 10 );
28  if( err && *err ) {
29  free( tokenized );
30  return JST_ERR_PATH_SYNTAX;
31  }
32  if( iter->type != JST_ARRAY ) {
33  free( tokenized );
34  return JST_ERR_PATH_SYNTAX;
35  }
36  if( index >= iter->value.array.count ) {
37  free( tokenized );
38  return JST_ERR_NOT_FOUND;
39  }
40  JST_ArrayItem * item = iter->value.array.items[index];
41  iter = &( item->element );
42  }
43  else {
44  if( iter->type != JST_OBJECT ) {
45  free( tokenized );
46  return JST_ERR_PATH_SYNTAX;
47  }
48  JST_Pair key, *key_addr = &key;
49  key.name = token;
50  JST_Pair ** pair =
51  bsearch( &key_addr, iter->value.object.items, iter->value.object.count, sizeof( JST_Pair * ), JST_pairs_compare );
52  if( pair == NULL ) {
53  free( tokenized );
54  return JST_ERR_NOT_FOUND;
55  }
56  iter = &((*pair)->element);
57  }
58  token = strtok( NULL, STRTOK_PATH );
59  }
60  free( tokenized );
61  *dest = iter;
62  return JST_ERR_NONE;
63 }
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&#39;s name.
Definition: jstools.h:96
int JST_pairs_compare(const void *left, const void *right)
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
JST_Element element
Definition: jstools.h:85
An array item has a parent and a typed value.
Definition: jstools.h:83
JST_Object object
Definition: jstools.h:41
JST_Value value
Definition: jstools.h:72
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
JST_Array array
Definition: jstools.h:42
Path syntax is (<name>|[<integer>])(.<name>|[<integer>])*.
Definition: jstools.h:125
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
JST_ValueType type
Definition: jstools.h:71
JST_get() has no result.
Definition: jstools.h:126
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26

◆ JST_load_from_file()

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.

Parameters
filepaththe file path
rootthe root of the JSON tree
linethe last line number read, available in case of error
contextfew characters before the error, available in case of error
ditsancethe offset of the error, in context
Returns
an error status.

Definition at line 573 of file JST_load.c.

573  {
574  if(( path == NULL )) {
575  return JST_ERR_NULL_ARGUMENT;
576  }
577  FILE * stream = fopen( path, "rt" );
578  if( stream == NULL ) {
579  return JST_ERR_ERRNO;
580  }
581  JST_Error error = JST_load_from_stream( stream, root, syntax_error );
582  int myerrno = errno;
583  fclose( stream );
584  errno = myerrno;
585  return error;
586 }
JST_Error JST_load_from_stream(FILE *stream, JST_Element *root, JST_SyntaxError *syntax_error)
Reads the input stream, allocates the node of the corresponding tree, than frees the input buffer...
Definition: JST_load.c:533
JST_Error
When things goes wrong, an error information is given.
Definition: jstools.h:108
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Function misused.
Definition: jstools.h:110
Here is the call graph for this function:

◆ JST_load_from_stream()

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.

Parameters
streamthe character stream to read
rootthe root of the JSON tree
linethe last line number read, available in case of error
contextfew characters before the error, available in case of error
ditsancethe offset of the error, in context
Returns
an error status.

Definition at line 533 of file JST_load.c.

533  {
534  if( syntax_error ) {
535  *syntax_error = JST_SyntaxError_Zero;
536  }
537  if(( stream == NULL )||( root == NULL )) {
538  return JST_ERR_NULL_ARGUMENT;
539  }
540  *root = JST_Element_Zero;
541  Context context = Context_Zero;
542  Token token = Token_Zero;
543  context.stream = stream;
544  bool ok = true
545  && get_next_token( &context, &token )
546  && set_value( &context, root, NULL, &token )
547  && remaining_chars_are_only_whitespaces( &context );
548  if( ! ok ) {
549  if( syntax_error ) {
550  syntax_error->line = (unsigned)( 1 + context.line );
551  size_t pos = (( context.pos < context.limit )||(context.limit == 0 )) ? context.pos : ( context.limit - 1 );
552  while(( pos > 0 )
553  && (( context.pos - pos ) < ( sizeof( syntax_error->context ) / 2 ))
554  && ( context.buffer[pos] != '\r' )
555  && ( context.buffer[pos] != '\n' ))
556  {
557  --pos;
558  }
559  if(( context.buffer[pos] == '\r' )||( context.buffer[pos] == '\n' )) {
560  ++pos;
561  }
562  strncpy( syntax_error->context, context.buffer + pos, sizeof( syntax_error->context ) - 1 );
563  syntax_error->context[sizeof( syntax_error->context )-1] = '\0';
564  syntax_error->pos = (unsigned)( context.pos - pos );
565  if( syntax_error->pos > 0 ) {
566  --(syntax_error->pos);
567  }
568  }
569  }
570  return context.error;
571 }
const JST_Element JST_Element_Zero
Constant defined to initialize safely a variable of type JST_Element.
Definition: JST_load.c:19
const JST_SyntaxError JST_SyntaxError_Zero
Constant defined to initialize safely a variable of type JST_SyntaxError.
Definition: JST_load.c:22
Function misused.
Definition: jstools.h:110
Here is the caller graph for this function:

◆ JST_remove_item()

JST_Error JST_remove_item ( JST_Array array,
unsigned  index 
)

Definition at line 140 of file JST_update.c.

140  {
141  if( array == NULL ) {
142  return JST_ERR_NULL_ARGUMENT;
143  }
144  if( index >= array->count ) {
146  }
147  JST_ArrayItem * item = array->items[index];
148  memmove( array->items+index, array->items+index+1, sizeof( JST_ArrayItem * )*( array->count - index - 1 ));
149  --(array->count);
150  JST_delete_array_item( item );
151  return JST_ERR_NONE;
152 }
An array item has a parent and a typed value.
Definition: jstools.h:83
index is out of range
Definition: jstools.h:127
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12
JST_Error JST_delete_array_item(JST_ArrayItem *array)
Definition: JST_delete.c:31
Here is the call graph for this function:

◆ JST_remove_property()

JST_Error JST_remove_property ( JST_Object object,
const char *  name 
)

Definition at line 124 of file JST_update.c.

124  {
125  if(( object == NULL )||( name == NULL )) {
126  return JST_ERR_NULL_ARGUMENT;
127  }
128  for( unsigned i = 0; i < object->count; ++i ) {
129  JST_Pair * pair = object->items[i];
130  if( 0 == strcmp( pair->name, name )) {
131  memmove( object->items+i, object->items+i+1, sizeof( JST_Pair * )*( object->count - i - 1 ));
132  --(object->count);
133  JST_delete_pair( pair );
134  return JST_ERR_NONE;
135  }
136  }
137  return JST_ERR_NOT_FOUND;
138 }
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&#39;s name.
Definition: jstools.h:96
A object attribute item has a parent and is a named-typed-value pair.
Definition: jstools.h:93
JST_Error JST_delete_pair(JST_Pair *element)
Definition: JST_delete.c:5
Function misused.
Definition: jstools.h:110
JST_get() has no result.
Definition: jstools.h:126
unsigned count
Cardinality of the previous array.
Definition: jstools.h:26
Here is the call graph for this function:

◆ JST_replace_item()

JST_Error JST_replace_item ( JST_Array array,
unsigned  index,
const JST_Element value 
)

Definition at line 114 of file JST_update.c.

114  {
115  if(( array == NULL )||( src == NULL )) {
116  return JST_ERR_NULL_ARGUMENT;
117  }
118  if( index > array->count ) {
120  }
121  return element_copy( &(array->items[index]->element), src );
122 }
JST_Element element
Definition: jstools.h:85
index is out of range
Definition: jstools.h:127
Function misused.
Definition: jstools.h:110
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
Definition: jstools.h:11
unsigned count
Cardinality of the previous array.
Definition: jstools.h:12

◆ JST_save_to_file()

JST_Error JST_save_to_file ( const char *  filepath,
const JST_Element root,
unsigned  indent 
)

Save the JSON tree to a file, pretty-printed.

Parameters
filepaththe path of the target file
rootthe root of the tree to save
indentthe amout of space to pretty print the tree.

Definition at line 56 of file JST_save.c.

56  {
57  if(( path == NULL )||( root == NULL )) {
58  return JST_ERR_NULL_ARGUMENT;
59  }
60  FILE * stream = fopen( path, "wt" );
61  if( stream == NULL ) {
62  return JST_ERR_ERRNO;
63  }
64  return JST_save_to_stream( stream, root, indent );
65 }
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
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
Function misused.
Definition: jstools.h:110
Here is the call graph for this function:

◆ JST_save_to_file_compact()

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.

Parameters
filepaththe path of the target file
rootthe root of the tree to save

Definition at line 120 of file JST_save.c.

120  {
121  if(( path == NULL )||( root == NULL )) {
122  return JST_ERR_NULL_ARGUMENT;
123  }
124  FILE * stream = fopen( path, "wt" );
125  if( stream == NULL ) {
126  return JST_ERR_ERRNO;
127  }
128  return JST_save_to_stream_compact( stream, root );
129 }
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
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Function misused.
Definition: jstools.h:110
Here is the call graph for this function:

◆ JST_save_to_stream()

JST_Error JST_save_to_stream ( FILE *  stream,
const JST_Element root,
unsigned  indent 
)

Save the JSON tree to a stream, pretty-printed.

Parameters
streamthe target stream.
rootthe root of the tree to save.
indentthe amout of space to pretty print the tree.

Definition at line 47 of file JST_save.c.

47  {
48  if(( stream == NULL )||( root == NULL )) {
49  return JST_ERR_NULL_ARGUMENT;
50  }
51  save_element( root, stream, 0, indent );
52  fprintf( stream, "\n" );
53  return fclose( stream ) ? JST_ERR_ERRNO : JST_ERR_NONE;
54 }
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Function misused.
Definition: jstools.h:110
Here is the caller graph for this function:

◆ JST_save_to_stream_compact()

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.

Parameters
filepaththe path of the target file
rootthe root of the tree to save

Definition at line 112 of file JST_save.c.

112  {
113  if(( stream == NULL )||( root == NULL )) {
114  return JST_ERR_NULL_ARGUMENT;
115  }
116  save_element_compact( root, stream );
117  return fclose( stream ) ? JST_ERR_ERRNO : JST_ERR_NONE;
118 }
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
Function misused.
Definition: jstools.h:110
Here is the caller graph for this function:

◆ JST_serialize()

JST_Error JST_serialize ( JST_Element root,
char **  dest,
unsigned  indent 
)

Serialize the JSON tree to a string, pretty-printed.

Parameters
filepaththe path of the target file
rootthe root of the tree to save
desta pointer to the char buffer allocated by this function, must be freed by the user
indentthe amout of space to pretty print the tree. When indent is negative, it means the JSON tree is printed with all the nodes on a single line.

Definition at line 112 of file JST_serialize.c.

112  {
113  if(( root == NULL )||( dest == NULL )) {
114  return JST_ERR_NULL_ARGUMENT;
115  }
116  JST_String string = JST_String_Zero;
117  if(( serialize_element( root, &string, 0, indent ) == JST_ERR_NONE )&& JST_String_append_char( &string, '\n' )) {
118  string.buffer[string.length] = '\0';
119  *dest = realloc( string.buffer, string.length + 1 ); // truncate, if necessary
120  if( *dest == NULL ) {
121  JST_String_delete( &string );
122  return JST_ERR_ERRNO;
123  }
124  string.buffer = NULL;
125  }
126  JST_String_delete( &string );
127  return JST_ERR_NONE;
128 }
char * buffer
Definition: JST_string.h:8
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
bool JST_String_append_char(JST_String *string, char c)
Definition: JST_string.c:8
JST_Error JST_String_delete(JST_String *string)
Definition: JST_string.c:45
Function misused.
Definition: jstools.h:110
const JST_String JST_String_Zero
Definition: JST_string.c:6

◆ JST_serialize_compact()

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.

Parameters
filepaththe path of the target file
rootthe root of the tree to save
desta pointer to the char buffer allocated by this function, must be freed by the user negative, it means the JSON tree is printed with all the nodes on a single line.

Definition at line 227 of file JST_serialize.c.

227  {
228  if(( root == NULL )||( dest == NULL )) {
229  return JST_ERR_NULL_ARGUMENT;
230  }
231  JST_String string = JST_String_Zero;
232  if(( serialize_element_compact( root, &string ) == JST_ERR_NONE )) {
233  string.buffer[string.length] = '\0';
234  *dest = realloc( string.buffer, string.length + 1 ); // truncate, if necessary
235  if( *dest == NULL ) {
236  JST_String_delete( &string );
237  return JST_ERR_ERRNO;
238  }
239  string.buffer = NULL;
240  }
241  JST_String_delete( &string );
242  return JST_ERR_NONE;
243 }
char * buffer
Definition: JST_string.h:8
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
JST_Error JST_String_delete(JST_String *string)
Definition: JST_string.c:45
Function misused.
Definition: jstools.h:110
const JST_String JST_String_Zero
Definition: JST_string.c:6

◆ JST_walk()

JST_Error JST_walk ( const JST_Element root,
JST_Visitor  visitor,
void *  user_context 
)

Definition at line 45 of file JST_walk.c.

45  {
46  if(( root == NULL )||( visitor == NULL )) {
47  return JST_ERR_NULL_ARGUMENT;
48  }
49  return visit_element( NULL, -1U, root, visitor, context ) ? JST_ERR_NONE : JST_ERR_INTERRUPTED;
50 }
JST_Visitor has returned false to interrupt the tree&#39;s iteration.
Definition: jstools.h:128
Function misused.
Definition: jstools.h:110

Variable Documentation

◆ JST_Array_Zero

const JST_Array JST_Array_Zero

Constant defined to initialize safely a variable of type JST_Array.

Definition at line 16 of file JST_load.c.

◆ JST_ArrayItem_Zero

const JST_ArrayItem JST_ArrayItem_Zero

Definition at line 20 of file JST_load.c.

◆ JST_Element_Zero

const JST_Element JST_Element_Zero

Constant defined to initialize safely a variable of type JST_Element.

Definition at line 19 of file JST_load.c.

◆ JST_Object_Zero

const JST_Object JST_Object_Zero

Constant defined to initialize safely a variable of type JST_Object.

Definition at line 17 of file JST_load.c.

◆ JST_Pair_Zero

const JST_Pair JST_Pair_Zero

Constant defined to initialize safely a variable of type JST_Pair.

Definition at line 21 of file JST_load.c.

◆ JST_SyntaxError_Zero

const JST_SyntaxError JST_SyntaxError_Zero

Constant defined to initialize safely a variable of type JST_SyntaxError.

Definition at line 22 of file JST_load.c.

◆ JST_Value_Zero

const JST_Value JST_Value_Zero

Constant defined to initialize safely a variable of type JST_Value.

Definition at line 18 of file JST_load.c.