Skip to content

Commit

Permalink
Fix JSON serialization errors in labels with double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jan 29, 2024
1 parent 4fc223a commit ca9b34a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 11 deletions.
6 changes: 2 additions & 4 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51024,9 +51024,7 @@ void flecs_json_label(
{
const char *lbl = flecs_json_entity_label(world, e);
if (lbl) {
ecs_strbuf_appendch(buf, '"');
ecs_strbuf_appendstr(buf, lbl);
ecs_strbuf_appendch(buf, '"');
flecs_json_string_escape(buf, lbl);
} else {
ecs_strbuf_appendch(buf, '0');
}
Expand Down Expand Up @@ -52969,7 +52967,7 @@ void flecs_json_serialize_iter_result_entity_labels(
int i;
for (i = 0; i < it->count; i ++) {
flecs_json_next(buf);
flecs_json_string(buf, labels[i].value);
flecs_json_string_escape(buf, labels[i].value);
}

flecs_json_array_pop(buf);
Expand Down
4 changes: 1 addition & 3 deletions src/addons/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ void flecs_json_label(
{
const char *lbl = flecs_json_entity_label(world, e);
if (lbl) {
ecs_strbuf_appendch(buf, '"');
ecs_strbuf_appendstr(buf, lbl);
ecs_strbuf_appendch(buf, '"');
flecs_json_string_escape(buf, lbl);
} else {
ecs_strbuf_appendch(buf, '0');
}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/json/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ void flecs_json_serialize_iter_result_entity_labels(
int i;
for (i = 0; i < it->count; i ++) {
flecs_json_next(buf);
flecs_json_string(buf, labels[i].value);
flecs_json_string_escape(buf, labels[i].value);
}

flecs_json_array_pop(buf);
Expand Down
2 changes: 2 additions & 0 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@
"serialize_type_info_w_unit_quantity",
"serialize_type_info_w_unit_over",
"serialize_w_entity_label",
"serialize_w_entity_label_w_str",
"serialize_w_var_labels",
"serialize_w_var_component",
"serialize_w_optional_tag",
Expand Down Expand Up @@ -811,6 +812,7 @@
"serialize_world",
"serialize_term_labels",
"serialize_id_labels",
"serialize_id_labels_w_str",
"serialize_vars_for_query",
"serialize_var_labels_for_query",
"serialize_var_ids_for_query",
Expand Down
77 changes: 77 additions & 0 deletions test/meta/src/SerializeIterToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,51 @@ void SerializeIterToJson_serialize_w_entity_label(void) {
ecs_fini(world);
}

void SerializeIterToJson_serialize_w_entity_label_w_str(void) {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Tag);

ecs_entity_t e1 = ecs_new_entity(world, "foo_bar");
ecs_entity_t e2 = ecs_new_entity(world, "hello_world");
ecs_doc_set_name(world, e2, "Hello \"World\"");

ecs_add(world, e1, Tag);
ecs_add(world, e2, Tag);

ecs_query_t *q = ecs_query_new(world, "Tag");
ecs_iter_t it = ecs_query_iter(world, q);

ecs_iter_to_json_desc_t desc = ECS_ITER_TO_JSON_INIT;
desc.serialize_entity_labels = true;
char *json = ecs_iter_to_json(world, &it, &desc);

test_str(json,
"{"
"\"ids\":[[\"Tag\"]], "
"\"results\":[{"
"\"ids\":[[\"Tag\"]], "
"\"sources\":[0], "
"\"entities\":["
"\"foo_bar\""
"]"
"}, {"
"\"ids\":[[\"Tag\"]], "
"\"sources\":[0], "
"\"entities\":["
"\"hello_world\""
"], "
"\"entity_labels\":["
"\"Hello \\\"World\\\"\""
"]"
"}]"
"}");

ecs_os_free(json);

ecs_fini(world);
}

void SerializeIterToJson_serialize_w_var_labels(void) {
ecs_world_t *world = ecs_init();

Expand Down Expand Up @@ -2696,6 +2741,38 @@ void SerializeIterToJson_serialize_id_labels(void) {
ecs_fini(world);
}

void SerializeIterToJson_serialize_id_labels_w_str(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_doc_set_name(world, ecs_id(Position), "\"position\"");

ecs_struct_init(world, &(ecs_struct_desc_t){
.entity = ecs_id(Position),
.members = {
{"x", ecs_id(ecs_f32_t)},
{"y", ecs_id(ecs_f32_t)}
}
});

ecs_entity_t e = ecs_new_entity(world, "ent");
ecs_set(world, e, Position, {1, 2});

ecs_query_t *q = ecs_query_new(world, "Position");
ecs_iter_t it = ecs_query_iter(world, q);

ecs_iter_to_json_desc_t desc = {0};
desc.serialize_id_labels = true;
char *json = ecs_iter_to_json(world, &it, &desc);
test_assert(json != NULL);
test_str(json, "{\"results\":[{\"id_labels\":[[\"\\\"position\\\"\"]]}]}");

ecs_os_free(json);

ecs_fini(world);
}

void SerializeIterToJson_serialize_vars_for_query(void) {
ecs_world_t *world = ecs_init();

Expand Down
2 changes: 0 additions & 2 deletions test/meta/src/SerializeIterToRowJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,6 @@ void SerializeIterToRowJson_serialize_this_w_1_tag_component_pair_var(void) {

test_str(json, expect);

printf("%s\n", json);

ecs_os_free(json);

ecs_rule_fini(q);
Expand Down
12 changes: 11 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ void SerializeIterToJson_serialize_type_info_w_unit(void);
void SerializeIterToJson_serialize_type_info_w_unit_quantity(void);
void SerializeIterToJson_serialize_type_info_w_unit_over(void);
void SerializeIterToJson_serialize_w_entity_label(void);
void SerializeIterToJson_serialize_w_entity_label_w_str(void);
void SerializeIterToJson_serialize_w_var_labels(void);
void SerializeIterToJson_serialize_w_var_component(void);
void SerializeIterToJson_serialize_w_optional_tag(void);
Expand Down Expand Up @@ -776,6 +777,7 @@ void SerializeIterToJson_serialize_table_w_private(void);
void SerializeIterToJson_serialize_world(void);
void SerializeIterToJson_serialize_term_labels(void);
void SerializeIterToJson_serialize_id_labels(void);
void SerializeIterToJson_serialize_id_labels_w_str(void);
void SerializeIterToJson_serialize_vars_for_query(void);
void SerializeIterToJson_serialize_var_labels_for_query(void);
void SerializeIterToJson_serialize_var_ids_for_query(void);
Expand Down Expand Up @@ -3961,6 +3963,10 @@ bake_test_case SerializeIterToJson_testcases[] = {
"serialize_w_entity_label",
SerializeIterToJson_serialize_w_entity_label
},
{
"serialize_w_entity_label_w_str",
SerializeIterToJson_serialize_w_entity_label_w_str
},
{
"serialize_w_var_labels",
SerializeIterToJson_serialize_w_var_labels
Expand Down Expand Up @@ -4105,6 +4111,10 @@ bake_test_case SerializeIterToJson_testcases[] = {
"serialize_id_labels",
SerializeIterToJson_serialize_id_labels
},
{
"serialize_id_labels_w_str",
SerializeIterToJson_serialize_id_labels_w_str
},
{
"serialize_vars_for_query",
SerializeIterToJson_serialize_vars_for_query
Expand Down Expand Up @@ -5519,7 +5529,7 @@ static bake_test_suite suites[] = {
"SerializeIterToJson",
NULL,
NULL,
63,
65,
SerializeIterToJson_testcases
},
{
Expand Down

0 comments on commit ca9b34a

Please sign in to comment.