Skip to content

Commit 23d95b0

Browse files
committed
find_by functions skips the search on passed nod, therefore we can call it multiple times and get the next value
1 parent 142ba1e commit 23d95b0

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

main.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,63 @@ int main(int argc, char* argv[])
7272
{
7373
return 1; // failed to parse
7474
}
75+
//
7576
// SOME TESTING
7677
JSONNode *key = gson_find_by_key(node, "\"gender\"");
7778
if (key != NULL)
79+
{
7880
printf("1Found: %s\n", gson_get_key_value(key));
81+
} else {
82+
printf("1Key not found: gender\n");
83+
}
84+
85+
key = gson_find_by_key(key, "\"gender\"");
86+
if (key != NULL)
87+
{
88+
printf("Key found: %s\n", gson_get_key_value(key));
89+
} else {
90+
printf("Key not found: gender\n");
91+
}
92+
7993

8094
JSONNode *val = gson_find_by_str_val(node, "\"male\"");
8195
if (val != NULL)
82-
printf("2Found: %s\n", gson_get_str_value(val));
96+
{
97+
printf("Val found: %s\n", gson_get_str_value(val));
98+
} else {
99+
printf("Val not found: male\n");
100+
}
101+
val = gson_find_by_str_val(val, "\"male\"");
102+
if (val != NULL)
103+
{
104+
printf("Val found: %s\n", gson_get_str_value(val));
105+
} else {
106+
printf("Val not found: male\n");
107+
}
83108

84109
JSONNode *_float = gson_find_by_float_val(node, 30);
85110
if (_float != NULL)
86-
printf("3Found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
111+
{
112+
printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
113+
} else {
114+
printf("Val not found: 30.00\n");
115+
}
116+
117+
_float = gson_find_by_float_val(_float, 30);
118+
if (_float != NULL)
119+
{
120+
printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
121+
} else {
122+
printf("Val not found: 30.00\n");
123+
}
124+
125+
_float = gson_find_by_float_val(_float, 30);
126+
if (_float != NULL)
127+
{
128+
printf("Val found: %s:%f\n", gson_get_key_value(_float), gson_get_float_value(_float));
129+
} else {
130+
printf("Val not found: 30.00\n");
131+
}
87132

88133

89134
if (_float != NULL)
@@ -113,4 +158,4 @@ int main(int argc, char* argv[])
113158
free(source);
114159

115160
return 0;
116-
}
161+
}

src/gson.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,11 +1001,15 @@ gson_debug_general(parser, curr);
10011001

10021002
JSONNode* gson_find_by_key(JSONNode *node, char *key)
10031003
{
1004+
JSONNode *orig = node;
10041005
while (node != NULL)
10051006
{
10061007
if (node->key && strcmp(key, node->key) == 0)
10071008
{
1008-
return node;
1009+
if (node != orig)
1010+
{
1011+
return node;
1012+
}
10091013
}
10101014
if (node->child)
10111015
{
@@ -1028,13 +1032,17 @@ JSONNode* gson_find_by_key(JSONNode *node, char *key)
10281032

10291033
JSONNode* gson_find_by_str_val(JSONNode *node, char *value)
10301034
{
1035+
JSONNode *orig = node;
10311036
while (node != NULL)
10321037
{
10331038
if (node->str_val
10341039
&& strcmp(value, node->str_val) == 0
10351040
&& node->type == JSON_STRING)
10361041
{
1037-
return node;
1042+
if (node != orig)
1043+
{
1044+
return node;
1045+
}
10381046
}
10391047
if (node->child)
10401048
{
@@ -1057,11 +1065,15 @@ JSONNode* gson_find_by_str_val(JSONNode *node, char *value)
10571065

10581066
JSONNode* gson_find_by_float_val(JSONNode *node, float value)
10591067
{
1068+
JSONNode *orig = node;
10601069
while (node != NULL)
10611070
{
10621071
if (node->num_val && value == node->num_val && node->type == JSON_NUMBER)
10631072
{
1064-
return node;
1073+
if (node != orig)
1074+
{
1075+
return node;
1076+
}
10651077
}
10661078
if (node->child)
10671079
{
@@ -1122,4 +1134,4 @@ JSONType gson_get_bool_value(JSONNode *node)
11221134
{
11231135
return node->type;
11241136
}
1125-
}
1137+
}

tests/mock_data/1_simple_json.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@
1313
"name": "August",
1414
"age": 7
1515
}
16+
],
17+
"siblings":
18+
[
19+
{
20+
"name": "John",
21+
"age": 30
22+
}
1623
]
17-
}
24+
}

0 commit comments

Comments
 (0)