Skip to content

Commit

Permalink
[pgmoneta#357] bool, null/empty string support for json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Jubilee101 committed Aug 15, 2024
1 parent 7ba2633 commit 8a946e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/libpgmoneta/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,11 @@ json_add(struct json* obj, char* key, uintptr_t val, enum value_type type)
static bool
value_start(char ch)
{
return (isdigit(ch) || ch == '-' || ch == '+') || (ch == '[') || (ch == '{') || (ch == '"');
return (isdigit(ch) || ch == '-' || ch == '+') || // number
(ch == '[') || // array
(ch == '{') || // item
(ch == '"' || ch == 'n') || // string or null string
(ch == 't' || ch == 'f'); // potential boolean value
}

static int
Expand All @@ -728,7 +732,14 @@ fill_value(char* str, char* key, uint64_t* index, struct json* o)
{
goto error;
}
json_add(o, key, (uintptr_t)val, ValueString);
if (val == NULL)
{
json_add(o, key, (uintptr_t)"", ValueString);
}
else
{
json_add(o, key, (uintptr_t)val, ValueString);
}
idx++;
free(val);
}
Expand Down Expand Up @@ -785,6 +796,32 @@ fill_value(char* str, char* key, uint64_t* index, struct json* o)
}
json_add(o, key, (uintptr_t)val, ValueJSON);
}
else if (str[idx] == 'n' || str[idx] == 't' || str[idx] == 'f')
{
char* val = NULL;
while (idx < len && str[idx] >= 'a' && str[idx] <= 'z')
{
val = pgmoneta_append_char(val, str[idx++]);
}
if (pgmoneta_compare_string(val, "null"))
{
json_add(o, key, 0, ValueString);
}
else if (pgmoneta_compare_string(val, "true"))
{
json_add(o, key, true, ValueBool);
}
else if (pgmoneta_compare_string(val, "false"))
{
json_add(o, key, false, ValueBool);
}
else
{
free(val);
goto error;
}
free(val);
}
else
{
goto error;
Expand Down
2 changes: 1 addition & 1 deletion src/libpgmoneta/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ string_to_string_cb(uintptr_t data, char* tag, int indent)
memset(buf, 0, MISC_LENGTH);
if (str == NULL)
{
snprintf(buf, MISC_LENGTH, "(null)");
snprintf(buf, MISC_LENGTH, "null");
}
else
{
Expand Down

0 comments on commit 8a946e7

Please sign in to comment.