mirror of
https://github.com/vale981/libblobpack
synced 2025-03-05 09:51:42 -05:00
fix creation of json from a blob that does not represent a valid table (first check that it is a table, and if not then output it as an array)
This commit is contained in:
parent
0d1db44094
commit
f0a490fcb1
2 changed files with 18 additions and 4 deletions
|
@ -269,11 +269,21 @@ static void blob_format_json_list(struct strbuf *s, const struct blob_field *att
|
||||||
const struct blob_field *pos;
|
const struct blob_field *pos;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
|
// just validate first that we really have a table and if not then we just output it as an array.
|
||||||
|
for(pos = blob_field_first_child(attr); pos; pos = blob_field_next_child(attr, pos)){
|
||||||
|
if(!array && blob_field_type(pos) != BLOB_FIELD_STRING){
|
||||||
|
array = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// for kv we need to pop the value here as well
|
||||||
|
if(!array)
|
||||||
|
pos = blob_field_next_child(attr, pos);
|
||||||
|
}
|
||||||
|
|
||||||
blob_puts(s, (array ? "[" : "{" ), 1);
|
blob_puts(s, (array ? "[" : "{" ), 1);
|
||||||
s->indent_level++;
|
s->indent_level++;
|
||||||
add_separator(s);
|
add_separator(s);
|
||||||
|
|
||||||
//__blob_for_each_attr(pos, attr, rem) {
|
|
||||||
for(pos = blob_field_first_child(attr); pos; pos = blob_field_next_child(attr, pos)){
|
for(pos = blob_field_first_child(attr); pos; pos = blob_field_next_child(attr, pos)){
|
||||||
if (!first) {
|
if (!first) {
|
||||||
blob_puts(s, ",", 1);
|
blob_puts(s, ",", 1);
|
||||||
|
@ -282,12 +292,13 @@ static void blob_format_json_list(struct strbuf *s, const struct blob_field *att
|
||||||
|
|
||||||
if(!array){
|
if(!array){
|
||||||
blob_format_string(s, blob_field_data(pos));
|
blob_format_string(s, blob_field_data(pos));
|
||||||
blob_puts(s, ": ", s->indent ? 2 : 1);
|
blob_puts(s, ":", s->indent ? 2 : 1);
|
||||||
pos = blob_field_next_child(attr, pos);
|
pos = blob_field_next_child(attr, pos);
|
||||||
}
|
}
|
||||||
blob_format_element(s, pos, array, false);
|
blob_format_element(s, pos, array, false);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->indent_level--;
|
s->indent_level--;
|
||||||
add_separator(s);
|
add_separator(s);
|
||||||
blob_puts(s, (array ? "]" : "}"), 1);
|
blob_puts(s, (array ? "]" : "}"), 1);
|
||||||
|
|
|
@ -33,11 +33,12 @@ int main(void){
|
||||||
blob_put_int(&blob, 5000000000lu);
|
blob_put_int(&blob, 5000000000lu);
|
||||||
blob_close_array(&blob, o);
|
blob_close_array(&blob, o);
|
||||||
|
|
||||||
|
char *json = blob_to_json(&blob);
|
||||||
|
printf("json: %s\n", json);
|
||||||
|
|
||||||
const struct blob_field *out[8];
|
const struct blob_field *out[8];
|
||||||
TEST(blob_field_parse(blob_head(&blob), "isifssta", out, 8));
|
TEST(blob_field_parse(blob_head(&blob), "isifssta", out, 8));
|
||||||
|
|
||||||
char *json = blob_to_json(&blob);
|
|
||||||
|
|
||||||
TEST(strcmp("[1,\"foo\",243,3.141593e+00,\"copyme\",\"copyme\",{\"one\":1,\"two\":2,\"three\":3},[100,200,1000,70000,5000000000]]", json) == 0);
|
TEST(strcmp("[1,\"foo\",243,3.141593e+00,\"copyme\",\"copyme\",{\"one\":1,\"two\":2,\"three\":3},[100,200,1000,70000,5000000000]]", json) == 0);
|
||||||
|
|
||||||
struct blob b2;
|
struct blob b2;
|
||||||
|
@ -50,6 +51,8 @@ int main(void){
|
||||||
free(json);
|
free(json);
|
||||||
free(json2);
|
free(json2);
|
||||||
|
|
||||||
|
blob_free(&blob);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue