JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
JST_walk.c
Go to the documentation of this file.
1 #include "jstools.h"
2 
3 #include <stdio.h>
4 #include <stddef.h>
5 
6 static bool visit_element( const char * name, unsigned index, const JST_Element * element, JST_Visitor visitor, void * user_context );
7 
8 static bool visit_object( const JST_Object * object, JST_Visitor visitor, void * user_context ) {
9  for( unsigned i = 0; i < object->count; ++i ) {
10  if( ! visit_element( object->items[i]->name, -1U, &(object->items[i]->element), visitor, user_context )) {
11  return false;
12  }
13  }
14  return true;
15 }
16 
17 static bool visit_array( const JST_Array * array, JST_Visitor visitor, void * user_context ) {
18  for( unsigned i = 0; i < array->count; ++i ) {
19  if( ! visit_element( NULL, i, &(array->items[i]->element), visitor, user_context )) {
20  return false;
21  }
22  }
23  return true;
24 }
25 
26 static bool visit_element( const char * name, unsigned index, const JST_Element * element, JST_Visitor visitor, void * context ) {
27  (*visitor)( name, index, true, element, context );
28  switch( element->type ) {
29  case JST_OBJECT:
30  if( ! visit_object( &(element->value.object), visitor, context )) {
31  return false;
32  }
33  break;
34  case JST_ARRAY:
35  if( ! visit_array( &(element->value.array), visitor, context )) {
36  return false;
37  }
38  break;
39  default: break;
40  }
41  (*visitor)( name, index, false, element, context );
42  return true;
43 }
44 
45 JST_Error JST_walk( const JST_Element * root, JST_Visitor visitor, void * context ) {
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 }
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 JSON array.
Definition: jstools.h:10
JST_Element element
Definition: jstools.h:85
A JSON object, a sorted set of named-value pairs.
Definition: jstools.h:24
JST_Error JST_walk(const JST_Element *root, JST_Visitor visitor, void *context)
Definition: JST_walk.c:45
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
An element is a typed value.
Definition: jstools.h:70
bool(* JST_Visitor)(const char *name, unsigned index, bool pre, const JST_Element *element, void *user_context)
Visitor in depth first with &#39;pre&#39; set to true then backtrack with &#39;pre&#39; set to false.
Definition: jstools.h:134
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_Element element
The value associated with the name.
Definition: jstools.h:95