Skip to content

Commit

Permalink
now parse_boolean in json its correct
Browse files Browse the repository at this point in the history
  • Loading branch information
KaisenAmin committed Jan 24, 2024
1 parent 08d17c5 commit 72f2575
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
58 changes: 45 additions & 13 deletions json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ void next_token(JsonParserState* state) {
default:
if (strncmp(state->input + state->position, "true", 4) == 0) {
state->current_token.type = JSON_TOKEN_BOOLEAN;
state->position += 3;
// state->position += 3;
}
else if (strncmp(state->input + state->position, "false", 5) == 0) {
state->current_token.type = JSON_TOKEN_BOOLEAN;
state->position += 4;
// state->position += 4;
}
else if (strncmp(state->input + state->position, "null", 4) == 0) {
if (strncmp(state->input + state->position, "null", 4) == 0) {
state->current_token.type = JSON_TOKEN_NULL;
state->position += 3;
}
else {
state->current_token.type = JSON_TOKEN_ERROR;
}
// else {
// state->current_token.type = JSON_TOKEN_ERROR;
// }
break;
}
state->position++;
Expand Down Expand Up @@ -126,7 +126,7 @@ static JsonElement* parse_string(JsonParserState* state) {

strncpy(str_content, state->input + start, length);
str_content[length] = '\0';
fmt_printf("%s\n", str_content);

state->position++;

// Create a new JsonElement for the string
Expand Down Expand Up @@ -161,9 +161,8 @@ static JsonElement* parse_number(JsonParserState* state) {

strncpy(number_str, state->input + start, length);
number_str[length] = '\0';
fmt_printf("Number %s\n", number_str);

double number_double = atof(number_str);
fmt_printf("%f\n", number_double);
free(number_str);

JsonElement* element = json_create(JSON_NUMBER);
Expand All @@ -180,7 +179,40 @@ static JsonElement* parse_null(JsonParserState* state) {
}

static JsonElement* parse_boolean(JsonParserState* state) {
// should be implement
if (state->current_token.type != JSON_TOKEN_BOOLEAN) {
fmt_fprintf(stderr, "Error: Expected Boolean token in parse_boolean");
return NULL;
}

size_t start = state->position - 1;
if (strncmp(state->input + start, "true", 4) == 0) {
state->current_token.type = JSON_TOKEN_BOOLEAN;
state->position += 3;
}
else if (strncmp(state->input + start, "false", 5) == 0) {
state->current_token.type = JSON_TOKEN_BOOLEAN;
state->position += 4;
}


size_t length = state->position - start;
char *boolean_str = (char*) malloc(length);
if (!boolean_str) {
fmt_fprintf(stderr, "Error: Memory allocation failed in parse_boolean.\n");
return NULL;
}
strncpy(boolean_str, state->input + start, length);
boolean_str[length] = '\0';
bool boolean_value = string_to_bool_from_cstr(boolean_str);

JsonElement* element = json_create(JSON_BOOL);
if (!element) {
fmt_fprintf(stderr, "Error: faile to create json bool value in parse_boolean.\n");
return NULL;
}

element->value.bool_val = boolean_value;
return element;
}

static JsonElement* parser_internal(JsonParserState* state) {
Expand Down Expand Up @@ -262,9 +294,9 @@ JsonElement* json_parse(const char* json_str) {
// case JSON_TOKEN_OBJECT_START:
// root = parse_object(&state);
// break;
// case JSON_TOKEN_BOOLEAN:
// root = parse_boolean(&state);
// break;
case JSON_TOKEN_BOOLEAN:
root = parse_boolean(&state);
break;
case JSON_TOKEN_NUMBER:
root = parse_number(&state);
break;
Expand Down
16 changes: 16 additions & 0 deletions string/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1996,4 +1996,20 @@ size_t string_length_utf8(const char* str) {
str++;
}
return length;
}

bool string_to_bool_from_cstr(const char* boolstr) {
if (!boolstr) {
fmt_fprintf(stderr, "Error: bool str is NULL and invalid in string_to_bool_cstr.\n");
return false;
}

if (strcmp(boolstr, "true") == 0) {
return true;
}
else if (strcmp(boolstr, "false") == 0) {
return false;
}

return false;
}
1 change: 1 addition & 0 deletions string/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bool string_contains(String* str, const char* substr);
bool string_set_pool_size(String* str, size_t newSize);
bool string_starts_with(const String* str, const char* substr);
bool string_ends_with(const String* str, const char* substr);
bool string_to_bool_from_cstr(const char* boolstr);

int string_compare(const String* str1, const String* str2);
int string_find(String* str, const char* buffer, size_t pos);
Expand Down

0 comments on commit 72f2575

Please sign in to comment.