Skip to content

Commit d9b88cd

Browse files
committed
add searching by key string
1 parent b7875e3 commit d9b88cd

File tree

5 files changed

+92
-37
lines changed

5 files changed

+92
-37
lines changed

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -xe
44
NAME=gson
5-
CCFLAGS="-DDEBUG=0 -Wall -g -O3"
5+
CCFLAGS="-DDEBUG=0 -Wall -g"
66

77
compile_lib() {
88
gcc $CCFLAGS -c src/$NAME.c -o lib/$NAME.o

include/gson.h

+64-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,72 @@
44
typedef void Parser;
55
typedef void JSONNode;
66

7+
typedef enum {
8+
JSON_OBJECT,
9+
JSON_ARRAY,
10+
JSON_STRING,
11+
JSON_NUMBER,
12+
JSON_TRUE_VAL,
13+
JSON_FALSE_VAL,
14+
JSON_NULL_VAL,
15+
} JSONType;
16+
17+
/*
18+
* initialise parser, which will be passed in gson_parse()
19+
*/
720
extern Parser* parser_init(char* source);
21+
22+
/* destroy parser */
823
extern void parser_destroy(Parser *parser);
24+
25+
/*
26+
* takes parser and NULL as arguments, returns JSONNode type object pointing to the top level json object in the source
27+
*/
928
extern JSONNode* gson_parse(Parser *parser, JSONNode *node);
29+
30+
/*
31+
* cleanup - destroys the tree of JSONObjects recursively starting from 'node'
32+
*/
1033
extern void gson_destroy(JSONNode *node);
1134

12-
#endif
35+
/*
36+
* returns 'node' pointing to the first JSON object with matching 'key'
37+
*/
38+
extern JSONNode* gson_find_by_key(JSONNode *node, char *key);
39+
40+
/*
41+
* returns 'node' pointing to the first JSON object with matching 'value'
42+
*/
43+
extern JSONNode* gson_find_by_str_val(JSONNode *node, char *value);
44+
45+
/*
46+
* returns 'node' pointing to the first JSON object with matching 'value'
47+
*/
48+
extern JSONNode* gson_find_by_float_val(JSONNode *node, float value);
49+
50+
/*
51+
* returns the type of 'node'
52+
*/
53+
extern JSONType gson_get_object_type(JSONNode *node);
54+
55+
/*
56+
* returns key value 'node'
57+
*/
58+
extern char* gson_get_key_value(JSONNode *node);
59+
60+
/*
61+
* returns string value 'node'
62+
*/
63+
extern char* gson_get_str_value(JSONNode *node);
64+
65+
/*
66+
* returns numeric value 'node'
67+
*/
68+
extern float gson_get_float_value(JSONNode *node);
69+
70+
/*
71+
* returns JSON_FALSE_VAL, JSON_TRUE_VAL or JSON_NULL if the 'node' is of appropriate type
72+
*/
73+
extern JSONType gson_get_bool_value(JSONNode *node);
74+
75+
#endif // GSON_H

main.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <gson.h>
1+
#include "./include/gson.h"
22
#include <time.h>
33
#include <stdio.h>
44
#include <stdlib.h>
@@ -59,6 +59,9 @@ int main(int argc, char* argv[])
5959
t = clock() - t;
6060
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
6161
printf("parsing took %f seconds to execute \n", time_taken);
62+
JSONNode *key = gson_find_by_key(node, "\"gender\"");
63+
if (key != NULL)
64+
printf("Found!\n");
6265
#if DEBUG==1
6366
gson_debug_print_tree(node, 0);
6467
#endif

src/gson.c

+22-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <stdlib.h>
33
#include <string.h>
44
#include <stdbool.h>
5-
#include <time.h>
65
#include "gson_int.h"
76
#if DEBUG==1
87
#include "debug.h"
@@ -774,17 +773,6 @@ JSONNode* gson_true_val(Parser *parser, JSONNode *curr)
774773
gson_error(parser, "Error");
775774
return NULL;
776775
}
777-
char *val = "true";
778-
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
779-
if (!curr->str_val)
780-
{
781-
gson_error(parser, "Memory allocation failed\n");
782-
return curr;
783-
}
784-
strcpy(curr->str_val, val);
785-
#if DEBUG==1
786-
gson_debug_print_str_val(curr);
787-
#endif
788776
return curr;
789777
}
790778
JSONNode* gson_false_val(Parser *parser, JSONNode *curr)
@@ -827,17 +815,6 @@ JSONNode* gson_false_val(Parser *parser, JSONNode *curr)
827815
gson_error(parser, "Error");
828816
return NULL;
829817
}
830-
char *val = "false";
831-
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
832-
if (!curr->str_val)
833-
{
834-
gson_error(parser, "Memory allocation failed\n");
835-
return curr;
836-
}
837-
strcpy(curr->str_val, val);
838-
#if DEBUG==1
839-
gson_debug_print_str_val(curr);
840-
#endif
841818
return curr;
842819
}
843820

@@ -881,17 +858,6 @@ JSONNode* gson_null_val(Parser *parser, JSONNode *curr)
881858
gson_error(parser, "Error");
882859
return NULL;
883860
}
884-
char *val = "null";
885-
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
886-
if (!curr->str_val)
887-
{
888-
gson_error(parser, "Memory allocation failed\n");
889-
return curr;
890-
}
891-
strcpy(curr->str_val, val);
892-
#if DEBUG==1
893-
gson_debug_print_str_val(curr);
894-
#endif
895861
return curr;
896862
}
897863

@@ -1031,3 +997,25 @@ gson_debug_general(parser, curr);
1031997
if (parser->had_error) return NULL;
1032998
return curr;
1033999
}
1000+
1001+
JSONNode* gson_find_by_key(JSONNode *node, char *key)
1002+
{
1003+
if (node == NULL)
1004+
return NULL;
1005+
1006+
if (node->key && strcmp(key, node->key) == 0)
1007+
{
1008+
return node;
1009+
}
1010+
1011+
JSONNode *found_in_child = gson_find_by_key(node->child, key);
1012+
if (found_in_child != NULL)
1013+
return found_in_child;
1014+
1015+
JSONNode *found_in_next = gson_find_by_key(node->next, key);
1016+
if (found_in_next != NULL)
1017+
return found_in_next;
1018+
1019+
return NULL;
1020+
}
1021+

tests/tests.c

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ int main()
114114
gson_destroy(node);
115115
}
116116

117+
// print summary
117118
if (total == passed)
118119
{
119120
printf("SUCCESS: %d/%d test PASSED\n", passed, total);

0 commit comments

Comments
 (0)