mirror of https://github.com/akheron/jansson
Merge 5e994c7223
into 0f50bb10b6
This commit is contained in:
commit
0cd282c580
|
@ -1,9 +1,10 @@
|
|||
EXTRA_PROGRAMS = simple_parse
|
||||
EXTRA_PROGRAMS = simple_parse simple_search
|
||||
|
||||
simple_parse_SOURCES = simple_parse.c
|
||||
simple_search_SOURCES = simple_search.c
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir)/src -I$(top_srcdir)/src
|
||||
LDFLAGS = -static # for easier running
|
||||
LDADD = $(top_builddir)/src/libjansson.la
|
||||
|
||||
all: simple_parse
|
||||
all: simple_parse simple_search
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <jansson.h>
|
||||
|
||||
char * str_haystack = "{\
|
||||
\"haystack\": \"full of hay\",\
|
||||
\"keystr\": \"valuestr\",\
|
||||
\"keyint\": 1234,\
|
||||
\"keyreal\": 12.34,\
|
||||
\"keyobject\": {\
|
||||
\"name\": \"innerObject\",\
|
||||
\"otherKey\": \"otherValue\",\
|
||||
\"otherInt\": 778,\
|
||||
\"needle2\": \"waldo2\",\
|
||||
\"needle5\": [\
|
||||
\"waldo5\", \"waldo6\", \"waldo7\"\
|
||||
]\
|
||||
},\
|
||||
\"keyarray\": [\
|
||||
{\
|
||||
\"name\": \"innerObjectInArray\",\
|
||||
\"otherKey\": \"otherValue\",\
|
||||
\"otherInt\": 778,\
|
||||
\"needle3\": 92536\
|
||||
},\
|
||||
543,\
|
||||
\"innerString\",\
|
||||
{\
|
||||
\"needle4\": \"waldo4\",\
|
||||
\"needle4key2\": 432\
|
||||
}\
|
||||
],\
|
||||
\"needle1\": \"waldo1\"\
|
||||
}";
|
||||
|
||||
int main() {
|
||||
json_t * j_haystack = json_loads(str_haystack, JSON_DECODE_ANY, NULL);
|
||||
json_t * j_needle1 = json_string("waldo1");
|
||||
json_t * j_needle2 = json_string("waldo2");
|
||||
json_t * j_needle3 = json_integer(92536);
|
||||
json_t * j_needle4 = json_pack("{sssi}", "needle4", "waldo4", "needle4key2", json_integer(432));
|
||||
json_t * j_needle5 = json_pack("[sss]", "waldo5", "waldo6", "waldo7");
|
||||
json_t * j_needle_none = json_pack("{ss}", "needle4", "waldo4");
|
||||
|
||||
if (json_search(j_haystack, j_needle1) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle1, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle1, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
if (json_search(j_haystack, j_needle2) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle2, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle2, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
if (json_search(j_haystack, j_needle3) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle3, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle3, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
if (json_search(j_haystack, j_needle4) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle4, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle4, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
if (json_search(j_haystack, j_needle5) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle5, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle5, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
if (json_search(j_haystack, j_needle_none) != NULL) {
|
||||
printf("needle %s found\n", json_dumps(j_needle_none, JSON_ENCODE_ANY));
|
||||
} else {
|
||||
printf("needle %s not found\n", json_dumps(j_needle_none, JSON_ENCODE_ANY));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -240,6 +240,9 @@ int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char
|
|||
|
||||
int json_equal(json_t *value1, json_t *value2);
|
||||
|
||||
/* search */
|
||||
|
||||
json_t * json_search(json_t * haystack, json_t * needle);
|
||||
|
||||
/* copying */
|
||||
|
||||
|
|
50
src/value.c
50
src/value.c
|
@ -982,6 +982,56 @@ int json_equal(json_t *json1, json_t *json2)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*** search ***/
|
||||
|
||||
/**
|
||||
* Look for an occurence of needle within haystack
|
||||
* If needle is present in haystack, return the reference to the json_t * that is equal to needle
|
||||
* If needle is not found, return NULL
|
||||
*/
|
||||
json_t * json_search(json_t * haystack, json_t * needle) {
|
||||
json_t * value1, * value2;
|
||||
size_t index;
|
||||
const char * key;
|
||||
|
||||
if (!haystack || !needle)
|
||||
return NULL;
|
||||
|
||||
if (haystack == needle)
|
||||
return haystack;
|
||||
|
||||
// If both haystack and needle are the same type, test them
|
||||
if (json_typeof(haystack) == json_typeof(needle))
|
||||
if (json_equal(haystack, needle))
|
||||
return haystack;
|
||||
|
||||
// If they are not equals, test json_search in haystack elements recursively if it's an array or an object
|
||||
if (json_is_array(haystack)) {
|
||||
json_array_foreach(haystack, index, value1) {
|
||||
if (json_equal(value1, needle)) {
|
||||
return value1;
|
||||
} else {
|
||||
value2 = json_search(value1, needle);
|
||||
if (value2 != NULL) {
|
||||
return value2;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (json_is_object(haystack)) {
|
||||
json_object_foreach(haystack, key, value1) {
|
||||
if (json_equal(value1, needle)) {
|
||||
return value1;
|
||||
} else {
|
||||
value2 = json_search(value1, needle);
|
||||
if (value2 != NULL) {
|
||||
return value2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*** copying ***/
|
||||
|
||||
|
|
Loading…
Reference in New Issue