Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added searching by key string #6

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ on:

jobs:
build:
name: run build
name: run build and tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: build library
run: ./build.sh
test:
name: run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: runtests
- name: run tests
run: ./build.sh test
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

set -xe
NAME=gson
CCFLAGS="-DDEBUG=0 -Wall -g -O3"
CCFLAGS="-DDEBUG=0 -Wall -g"

compile_lib() {
mkdir -pv lib
gcc $CCFLAGS -c src/$NAME.c -o lib/$NAME.o
gcc $CCFLAGS -c src/debug.c -o lib/debug.o
ar rcs lib/lib$NAME.a lib/$NAME.o lib/debug.o
Expand Down
65 changes: 64 additions & 1 deletion include/gson.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,72 @@
typedef void Parser;
typedef void JSONNode;

typedef enum {
JSON_OBJECT,
JSON_ARRAY,
JSON_STRING,
JSON_NUMBER,
JSON_TRUE_VAL,
JSON_FALSE_VAL,
JSON_NULL_VAL,
} JSONType;

/*
* initialise parser, which will be passed in gson_parse()
*/
extern Parser* parser_init(char* source);

/* destroy parser */
extern void parser_destroy(Parser *parser);

/*
* takes parser and NULL as arguments, returns JSONNode type object pointing to the top level json object in the source
*/
extern JSONNode* gson_parse(Parser *parser, JSONNode *node);

/*
* cleanup - destroys the tree of JSONObjects recursively starting from 'node'
*/
extern void gson_destroy(JSONNode *node);

#endif
/*
* returns 'node' pointing to the first JSON object with matching 'key'
*/
extern JSONNode* gson_find_by_key(JSONNode *node, char *key);

/*
* returns 'node' pointing to the first JSON object with matching 'value'
*/
extern JSONNode* gson_find_by_str_val(JSONNode *node, char *value);

/*
* returns 'node' pointing to the first JSON object with matching 'value'
*/
extern JSONNode* gson_find_by_float_val(JSONNode *node, float value);

/*
* returns the type of 'node'
*/
extern JSONType gson_get_object_type(JSONNode *node);

/*
* returns key value 'node'
*/
extern char* gson_get_key_value(JSONNode *node);

/*
* returns string value 'node'
*/
extern char* gson_get_str_value(JSONNode *node);

/*
* returns numeric value 'node'
*/
extern float gson_get_float_value(JSONNode *node);

/*
* returns JSON_FALSE_VAL, JSON_TRUE_VAL or JSON_NULL if the 'node' is of appropriate type
*/
extern JSONType gson_get_bool_value(JSONNode *node);

#endif // GSON_H
5 changes: 4 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <gson.h>
#include "./include/gson.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -59,6 +59,9 @@ int main(int argc, char* argv[])
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("parsing took %f seconds to execute \n", time_taken);
JSONNode *key = gson_find_by_key(node, "\"gender\"");
if (key != NULL)
printf("Found!\n");
#if DEBUG==1
gson_debug_print_tree(node, 0);
#endif
Expand Down
56 changes: 22 additions & 34 deletions src/gson.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "gson_int.h"
#if DEBUG==1
#include "debug.h"
Expand Down Expand Up @@ -774,17 +773,6 @@ JSONNode* gson_true_val(Parser *parser, JSONNode *curr)
gson_error(parser, "Error");
return NULL;
}
char *val = "true";
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
if (!curr->str_val)
{
gson_error(parser, "Memory allocation failed\n");
return curr;
}
strcpy(curr->str_val, val);
#if DEBUG==1
gson_debug_print_str_val(curr);
#endif
return curr;
}
JSONNode* gson_false_val(Parser *parser, JSONNode *curr)
Expand Down Expand Up @@ -827,17 +815,6 @@ JSONNode* gson_false_val(Parser *parser, JSONNode *curr)
gson_error(parser, "Error");
return NULL;
}
char *val = "false";
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
if (!curr->str_val)
{
gson_error(parser, "Memory allocation failed\n");
return curr;
}
strcpy(curr->str_val, val);
#if DEBUG==1
gson_debug_print_str_val(curr);
#endif
return curr;
}

Expand Down Expand Up @@ -881,17 +858,6 @@ JSONNode* gson_null_val(Parser *parser, JSONNode *curr)
gson_error(parser, "Error");
return NULL;
}
char *val = "null";
curr->str_val = malloc(sizeof(char) * (strlen(val) + 1));
if (!curr->str_val)
{
gson_error(parser, "Memory allocation failed\n");
return curr;
}
strcpy(curr->str_val, val);
#if DEBUG==1
gson_debug_print_str_val(curr);
#endif
return curr;
}

Expand Down Expand Up @@ -1031,3 +997,25 @@ gson_debug_general(parser, curr);
if (parser->had_error) return NULL;
return curr;
}

JSONNode* gson_find_by_key(JSONNode *node, char *key)
{
if (node == NULL)
return NULL;

if (node->key && strcmp(key, node->key) == 0)
{
return node;
}

JSONNode *found_in_child = gson_find_by_key(node->child, key);
if (found_in_child != NULL)
return found_in_child;

JSONNode *found_in_next = gson_find_by_key(node->next, key);
if (found_in_next != NULL)
return found_in_next;

return NULL;
}

1 change: 1 addition & 0 deletions tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ int main()
gson_destroy(node);
}

// print summary
if (total == passed)
{
printf("SUCCESS: %d/%d test PASSED\n", passed, total);
Expand Down