jv_parse: refactor jv_parse_sized
Signed-off-by: David Korczynski <david@adalogics.com>
This commit is contained in:
1 parent
eb3b5654bb
commit
4570c23720
1 file changed
+8
-43
+8
-43
@@ -859,49 +859,6 @@ jv jv_parser_next(struct jv_parser* p) {
|
||||
}
|
||||
}
|
||||
|
||||
jv jv_parse_sized(const char* string, int length) {
|
||||
struct jv_parser parser;
|
||||
parser_init(&parser, 0);
|
||||
jv_parser_set_buf(&parser, string, length, 0);
|
||||
jv value = jv_parser_next(&parser);
|
||||
if (jv_is_valid(value)) {
|
||||
jv next = jv_parser_next(&parser);
|
||||
if (jv_is_valid(next)) {
|
||||
// multiple JSON values, we only wanted one
|
||||
jv_free(value);
|
||||
jv_free(next);
|
||||
value = jv_invalid_with_msg(jv_string("Unexpected extra JSON values"));
|
||||
} else if (jv_invalid_has_msg(jv_copy(next))) {
|
||||
// parser error after the first JSON value
|
||||
jv_free(value);
|
||||
value = next;
|
||||
} else {
|
||||
// a single valid JSON value
|
||||
jv_free(next);
|
||||
}
|
||||
} else if (jv_invalid_has_msg(jv_copy(value))) {
|
||||
// parse error, we'll return it
|
||||
} else {
|
||||
// no value at all
|
||||
jv_free(value);
|
||||
value = jv_invalid_with_msg(jv_string("Expected JSON value"));
|
||||
}
|
||||
parser_free(&parser);
|
||||
|
||||
if (!jv_is_valid(value) && jv_invalid_has_msg(jv_copy(value))) {
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
value = jv_invalid_with_msg(jv_string_fmt("%s (while parsing '%s')",
|
||||
jv_string_value(msg),
|
||||
string));
|
||||
jv_free(msg);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
jv jv_parse(const char* string) {
|
||||
return jv_parse_sized(string, strlen(string));
|
||||
}
|
||||
|
||||
jv jv_parse_sized_custom_flags(const char* string, int length, int flags) {
|
||||
struct jv_parser parser;
|
||||
parser_init(&parser, flags);
|
||||
@@ -941,6 +898,14 @@ jv jv_parse_sized_custom_flags(const char* string, int length, int flags) {
|
||||
return value;
|
||||
}
|
||||
|
||||
jv jv_parse_sized(const char* string, int length) {
|
||||
return jv_parse_sized_custom_flags(string, length, 0);
|
||||
}
|
||||
|
||||
jv jv_parse(const char* string) {
|
||||
return jv_parse_sized(string, strlen(string));
|
||||
}
|
||||
|
||||
jv jv_parse_custom_flags(const char* string, int flags) {
|
||||
return jv_parse_sized_custom_flags(string, strlen(string), flags);
|
||||
}
|
||||
Reference in new issue
Block a user