Skip to content

Commit

Permalink
Fix issue with serializing type info to JSON for sparse components
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Nov 6, 2024
1 parent 0b2d91f commit 59555e1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
25 changes: 12 additions & 13 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43859,29 +43859,28 @@ bool flecs_json_serialize_table_type_info(
ecs_table_t *table,
ecs_strbuf_t *buf,
const ecs_iter_to_json_desc_t *desc)
{
ecs_column_t *columns = table->data.columns;
int32_t i, column_count = table->column_count;

{
flecs_json_memberl(buf, "type_info");
flecs_json_object_push(buf);

if (!column_count) {
flecs_json_object_pop(buf);
return false;
}

for (i = 0; i < column_count; i ++) {
ecs_column_t *column = &columns[i];
ecs_id_t id = flecs_column_id(table, i);
int32_t i, type_count = table->type.count;
for (i = 0; i < type_count; i ++) {
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
ecs_id_t id = table->type.array[i];
if (!(idr->flags & EcsIdIsSparse) &&
(!table->column_map || (table->column_map[i] == -1)))
{
continue;
}

if (!desc || !desc->serialize_builtin) {
if (flecs_json_is_builtin(id)) {
continue;
}
}

ecs_type_info_t *ti = column->ti;
const ecs_type_info_t *ti = idr->type_info;
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

flecs_json_next(buf);
Expand Down
25 changes: 12 additions & 13 deletions src/addons/json/serialize_iter_result_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,28 @@ bool flecs_json_serialize_table_type_info(
ecs_table_t *table,
ecs_strbuf_t *buf,
const ecs_iter_to_json_desc_t *desc)
{
ecs_column_t *columns = table->data.columns;
int32_t i, column_count = table->column_count;

{
flecs_json_memberl(buf, "type_info");
flecs_json_object_push(buf);

if (!column_count) {
flecs_json_object_pop(buf);
return false;
}

for (i = 0; i < column_count; i ++) {
ecs_column_t *column = &columns[i];
ecs_id_t id = flecs_column_id(table, i);
int32_t i, type_count = table->type.count;
for (i = 0; i < type_count; i ++) {
const ecs_table_record_t *tr = &table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
ecs_id_t id = table->type.array[i];
if (!(idr->flags & EcsIdIsSparse) &&
(!table->column_map || (table->column_map[i] == -1)))
{
continue;
}

if (!desc || !desc->serialize_builtin) {
if (flecs_json_is_builtin(id)) {
continue;
}
}

ecs_type_info_t *ti = column->ti;
const ecs_type_info_t *ti = idr->type_info;
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

flecs_json_next(buf);
Expand Down
1 change: 1 addition & 0 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@
"serialize_sparse_inherited",
"serialize_sparse_inherited_pair",
"serialize_sparse_inherited_mixed",
"serialize_sparse_w_type_info",
"serialize_auto_override_w_inherited",
"serialize_auto_override",
"serialize_auto_override_pair",
Expand Down
26 changes: 26 additions & 0 deletions test/meta/src/SerializeEntityToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,32 @@ void SerializeEntityToJson_serialize_sparse_inherited_mixed(void) {
ecs_fini(world);
}

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

ecs_entity_t ecs_id(Position) = ecs_struct(world, {
.entity = ecs_entity(world, {.name = "Position"}),
.members = {
{"x", ecs_id(ecs_i32_t)},
{"y", ecs_id(ecs_i32_t)}
}
});

ecs_add_id(world, ecs_id(Position), EcsSparse);

ecs_entity_t e = ecs_insert(world, ecs_value(Position, {10, 20}));
ecs_set_name(world, e, "Foo");

ecs_entity_to_json_desc_t desc = ECS_ENTITY_TO_JSON_INIT;
desc.serialize_type_info = true;
char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"Foo\", \"type_info\":{\"Position\":{\"x\":[\"int\"], \"y\":[\"int\"]}}, \"components\":{\"Position\":{\"x\":10, \"y\":20}}}");
ecs_os_free(json);

ecs_fini(world);
}

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

Expand Down
7 changes: 6 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ void SerializeEntityToJson_serialize_sparse_mixed(void);
void SerializeEntityToJson_serialize_sparse_inherited(void);
void SerializeEntityToJson_serialize_sparse_inherited_pair(void);
void SerializeEntityToJson_serialize_sparse_inherited_mixed(void);
void SerializeEntityToJson_serialize_sparse_w_type_info(void);
void SerializeEntityToJson_serialize_auto_override_w_inherited(void);
void SerializeEntityToJson_serialize_auto_override(void);
void SerializeEntityToJson_serialize_auto_override_pair(void);
Expand Down Expand Up @@ -3675,6 +3676,10 @@ bake_test_case SerializeEntityToJson_testcases[] = {
"serialize_sparse_inherited_mixed",
SerializeEntityToJson_serialize_sparse_inherited_mixed
},
{
"serialize_sparse_w_type_info",
SerializeEntityToJson_serialize_sparse_w_type_info
},
{
"serialize_auto_override_w_inherited",
SerializeEntityToJson_serialize_auto_override_w_inherited
Expand Down Expand Up @@ -4955,7 +4960,7 @@ static bake_test_suite suites[] = {
"SerializeEntityToJson",
NULL,
NULL,
72,
73,
SerializeEntityToJson_testcases
},
{
Expand Down

0 comments on commit 59555e1

Please sign in to comment.