JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
Functions
JST_get.c File Reference
#include "jstools.h"
#include "JST_private.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
Include dependency graph for JST_get.c:

Go to the source code of this file.

Functions

JST_Error JST_get (const char *jsonpath, JST_Element *root, JST_Element **dest)
 Search an element by its path. More...
 

Function Documentation

◆ 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