Add function to access items via dot notation

This commit is contained in:
Daniel Schubert 2016-10-06 22:53:05 +02:00
parent 94df772485
commit 77e0e45c71
2 changed files with 22 additions and 0 deletions

21
cJSON.c
View File

@ -677,6 +677,27 @@ cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array?array->child
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object?object->child:0;while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
int cJSON_HasObjectItem(cJSON *object,const char *string) {return cJSON_GetObjectItem(object,string)?1:0;}
cJSON *cJSON_GetDotObjectItem(cJSON *object,const char *string) {
int node_name_length;
char *node_name;
const char *position;
position = strchr(string, '.');
if (position != NULL) {
node_name_length = position - string;
node_name = (char*)cJSON_malloc(node_name_length + 1);
strncpy(node_name, string, node_name_length);
object = cJSON_GetObjectItem(object, node_name);
cJSON_free(node_name);
return cJSON_GetDotObjectItem(object, position + 1);
}
else {
return cJSON_GetObjectItem(object, string);
}
}
/* Utility for array list handling. */
static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
/* Utility for handling references. */

View File

@ -81,6 +81,7 @@ extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
extern int cJSON_HasObjectItem(cJSON *object,const char *string);
extern cJSON *cJSON_GetDotObjectItem(cJSON *object,const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr(void);