Skip to content

Commit aec0305

Browse files
committed
Enable serializing/hiding private ids for table JSON serialization
1 parent 675b970 commit aec0305

9 files changed

Lines changed: 201 additions & 23 deletions

File tree

flecs.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33391,6 +33391,7 @@ void flecs_rest_parse_json_ser_iter_params(
3339133391
flecs_rest_bool_param(req, "variables", &desc->serialize_variables);
3339233392
flecs_rest_bool_param(req, "is_set", &desc->serialize_is_set);
3339333393
flecs_rest_bool_param(req, "values", &desc->serialize_values);
33394+
flecs_rest_bool_param(req, "private", &desc->serialize_private);
3339433395
flecs_rest_bool_param(req, "entities", &desc->serialize_entities);
3339533396
flecs_rest_bool_param(req, "entity_labels", &desc->serialize_entity_labels);
3339633397
flecs_rest_bool_param(req, "variable_labels", &desc->serialize_variable_labels);
@@ -51920,8 +51921,18 @@ void flecs_json_serialize_iter_result_table_type(
5192051921

5192151922
ecs_type_t *type = &it->table->type;
5192251923
for (int i = 0; i < type->count; i ++) {
51924+
ecs_id_t id = type->array[i];
51925+
if (!desc->serialize_private) {
51926+
ecs_entity_t e = id;
51927+
if (ECS_IS_PAIR(id)) {
51928+
e = ecs_pair_first(world, id);
51929+
}
51930+
if (ecs_owns_id(world, e, EcsPrivate)) {
51931+
continue;
51932+
}
51933+
}
5192351934
flecs_json_next(buf);
51924-
flecs_json_serialize_id(world, type->array[i], buf);
51935+
flecs_json_serialize_id(world, id, buf);
5192551936
}
5192651937

5192751938
flecs_json_array_pop(buf);
@@ -51932,8 +51943,18 @@ void flecs_json_serialize_iter_result_table_type(
5193251943

5193351944
ecs_type_t *type = &it->table->type;
5193451945
for (int i = 0; i < type->count; i ++) {
51946+
ecs_id_t id = type->array[i];
51947+
if (!desc->serialize_private) {
51948+
ecs_entity_t e = id;
51949+
if (ECS_IS_PAIR(id)) {
51950+
e = ecs_pair_first(world, id);
51951+
}
51952+
if (ecs_owns_id(world, e, EcsPrivate)) {
51953+
continue;
51954+
}
51955+
}
5193551956
flecs_json_next(buf);
51936-
flecs_json_serialize_id_label(world, type->array[i], buf);
51957+
flecs_json_serialize_id_label(world, id, buf);
5193751958
}
5193851959

5193951960
flecs_json_array_pop(buf);
@@ -52344,7 +52365,8 @@ static
5234452365
int flecs_json_serialize_iter_result_columns(
5234552366
const ecs_world_t *world,
5234652367
const ecs_iter_t *it,
52347-
ecs_strbuf_t *buf)
52368+
ecs_strbuf_t *buf,
52369+
const ecs_iter_to_json_desc_t *desc)
5234852370
{
5234952371
ecs_table_t *table = it->table;
5235052372
if (!table || !table->column_count) {
@@ -52364,6 +52386,17 @@ int flecs_json_serialize_iter_result_columns(
5236452386
storage_column = column_map[i];
5236552387
}
5236652388

52389+
if (!desc->serialize_private) {
52390+
ecs_id_t id = type->array[i];
52391+
ecs_entity_t e = id;
52392+
if (ECS_IS_PAIR(id)) {
52393+
e = ecs_pair_first(world, id);
52394+
}
52395+
if (ecs_owns_id(world, e, EcsPrivate)) {
52396+
continue;
52397+
}
52398+
}
52399+
5236752400
ecs_strbuf_list_next(buf);
5236852401

5236952402
if (storage_column == -1) {
@@ -52472,7 +52505,7 @@ int flecs_json_serialize_iter_result(
5247252505

5247352506
/* Serialize component values */
5247452507
if (desc && desc->serialize_table) {
52475-
if (flecs_json_serialize_iter_result_columns(world, it, buf)) {
52508+
if (flecs_json_serialize_iter_result_columns(world, it, buf, desc)) {
5247652509
return -1;
5247752510
}
5247852511
} else {
@@ -52625,7 +52658,8 @@ int ecs_world_to_json_buf(
5262552658
ecs_iter_to_json_desc_t json_desc = {
5262652659
.serialize_table = true,
5262752660
.serialize_ids = true,
52628-
.serialize_entities = true
52661+
.serialize_entities = true,
52662+
.serialize_private = true
5262952663
};
5263052664

5263152665
int ret = ecs_iter_to_json_buf(world, &it, buf_out, &json_desc);

flecs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12729,6 +12729,7 @@ typedef struct ecs_iter_to_json_desc_t {
1272912729
bool serialize_variables; /**< Serialize variables */
1273012730
bool serialize_is_set; /**< Serialize is_set (for optional terms) */
1273112731
bool serialize_values; /**< Serialize component values */
12732+
bool serialize_private; /**< Serialize component values */
1273212733
bool serialize_entities; /**< Serialize entities (for This terms) */
1273312734
bool serialize_entity_labels; /**< Serialize doc name for entities */
1273412735
bool serialize_entity_ids; /**< Serialize numerical ids for entities */

include/flecs/addons/json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ typedef struct ecs_iter_to_json_desc_t {
253253
bool serialize_variables; /**< Serialize variables */
254254
bool serialize_is_set; /**< Serialize is_set (for optional terms) */
255255
bool serialize_values; /**< Serialize component values */
256+
bool serialize_private; /**< Serialize component values */
256257
bool serialize_entities; /**< Serialize entities (for This terms) */
257258
bool serialize_entity_labels; /**< Serialize doc name for entities */
258259
bool serialize_entity_ids; /**< Serialize numerical ids for entities */

include/flecs/private/api_defines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
#pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
110110
/* Very difficult to workaround this warning in C, especially for an ECS. */
111111
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
112+
/* This warning gets thrown when trying to cast pointer returned from dlproc */
113+
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
112114
#elif defined(ECS_TARGET_GNU)
113115
#ifndef __cplusplus
114116
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"

src/addons/json/serialize.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,8 +1526,18 @@ void flecs_json_serialize_iter_result_table_type(
15261526

15271527
ecs_type_t *type = &it->table->type;
15281528
for (int i = 0; i < type->count; i ++) {
1529+
ecs_id_t id = type->array[i];
1530+
if (!desc->serialize_private) {
1531+
ecs_entity_t e = id;
1532+
if (ECS_IS_PAIR(id)) {
1533+
e = ecs_pair_first(world, id);
1534+
}
1535+
if (ecs_owns_id(world, e, EcsPrivate)) {
1536+
continue;
1537+
}
1538+
}
15291539
flecs_json_next(buf);
1530-
flecs_json_serialize_id(world, type->array[i], buf);
1540+
flecs_json_serialize_id(world, id, buf);
15311541
}
15321542

15331543
flecs_json_array_pop(buf);
@@ -1538,8 +1548,18 @@ void flecs_json_serialize_iter_result_table_type(
15381548

15391549
ecs_type_t *type = &it->table->type;
15401550
for (int i = 0; i < type->count; i ++) {
1551+
ecs_id_t id = type->array[i];
1552+
if (!desc->serialize_private) {
1553+
ecs_entity_t e = id;
1554+
if (ECS_IS_PAIR(id)) {
1555+
e = ecs_pair_first(world, id);
1556+
}
1557+
if (ecs_owns_id(world, e, EcsPrivate)) {
1558+
continue;
1559+
}
1560+
}
15411561
flecs_json_next(buf);
1542-
flecs_json_serialize_id_label(world, type->array[i], buf);
1562+
flecs_json_serialize_id_label(world, id, buf);
15431563
}
15441564

15451565
flecs_json_array_pop(buf);
@@ -1950,7 +1970,8 @@ static
19501970
int flecs_json_serialize_iter_result_columns(
19511971
const ecs_world_t *world,
19521972
const ecs_iter_t *it,
1953-
ecs_strbuf_t *buf)
1973+
ecs_strbuf_t *buf,
1974+
const ecs_iter_to_json_desc_t *desc)
19541975
{
19551976
ecs_table_t *table = it->table;
19561977
if (!table || !table->column_count) {
@@ -1970,6 +1991,17 @@ int flecs_json_serialize_iter_result_columns(
19701991
storage_column = column_map[i];
19711992
}
19721993

1994+
if (!desc->serialize_private) {
1995+
ecs_id_t id = type->array[i];
1996+
ecs_entity_t e = id;
1997+
if (ECS_IS_PAIR(id)) {
1998+
e = ecs_pair_first(world, id);
1999+
}
2000+
if (ecs_owns_id(world, e, EcsPrivate)) {
2001+
continue;
2002+
}
2003+
}
2004+
19732005
ecs_strbuf_list_next(buf);
19742006

19752007
if (storage_column == -1) {
@@ -2078,7 +2110,7 @@ int flecs_json_serialize_iter_result(
20782110

20792111
/* Serialize component values */
20802112
if (desc && desc->serialize_table) {
2081-
if (flecs_json_serialize_iter_result_columns(world, it, buf)) {
2113+
if (flecs_json_serialize_iter_result_columns(world, it, buf, desc)) {
20822114
return -1;
20832115
}
20842116
} else {
@@ -2231,7 +2263,8 @@ int ecs_world_to_json_buf(
22312263
ecs_iter_to_json_desc_t json_desc = {
22322264
.serialize_table = true,
22332265
.serialize_ids = true,
2234-
.serialize_entities = true
2266+
.serialize_entities = true,
2267+
.serialize_private = true
22352268
};
22362269

22372270
int ret = ecs_iter_to_json_buf(world, &it, buf_out, &json_desc);

src/addons/rest.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void flecs_rest_parse_json_ser_iter_params(
197197
flecs_rest_bool_param(req, "variables", &desc->serialize_variables);
198198
flecs_rest_bool_param(req, "is_set", &desc->serialize_is_set);
199199
flecs_rest_bool_param(req, "values", &desc->serialize_values);
200+
flecs_rest_bool_param(req, "private", &desc->serialize_private);
200201
flecs_rest_bool_param(req, "entities", &desc->serialize_entities);
201202
flecs_rest_bool_param(req, "entity_labels", &desc->serialize_entity_labels);
202203
flecs_rest_bool_param(req, "variable_labels", &desc->serialize_variable_labels);

test/meta/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@
783783
"serialize_table",
784784
"serialize_table_w_id_labels",
785785
"serialize_table_w_var_labels",
786+
"serialize_table_w_private",
786787
"serialize_world",
787788
"serialize_term_labels",
788789
"serialize_id_labels",

test/meta/src/SerializeIterToJson.c

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,17 +2187,17 @@ void SerializeIterToJson_serialize_table(void) {
21872187

21882188
test_str(json, "{\"results\":["
21892189
"{"
2190-
"\"ids\":[[\"Position\"], [\"Foo\"], [\"flecs.core.Identifier\",\"flecs.core.Name\"]], "
2190+
"\"ids\":[[\"Position\"], [\"Foo\"]], "
21912191
"\"entities\":[\"e1\"], "
2192-
"\"values\":[[{\"x\":10, \"y\":20}], 0, 0]"
2192+
"\"values\":[[{\"x\":10, \"y\":20}], 0]"
21932193
"}, {"
2194-
"\"ids\":[[\"Position\"], [\"Velocity\"], [\"Foo\"], [\"Bar\"], [\"flecs.core.Identifier\",\"flecs.core.Name\"]], "
2194+
"\"ids\":[[\"Position\"], [\"Velocity\"], [\"Foo\"], [\"Bar\"]], "
21952195
"\"entities\":[\"e2\"], "
2196-
"\"values\":[[{\"x\":20, \"y\":30}], [{\"x\":1, \"y\":1}], 0, 0, 0]"
2196+
"\"values\":[[{\"x\":20, \"y\":30}], [{\"x\":1, \"y\":1}], 0, 0]"
21972197
"}, {"
2198-
"\"ids\":[[\"Position\"], [\"Mass\"], [\"flecs.core.Identifier\",\"flecs.core.Name\"]], "
2198+
"\"ids\":[[\"Position\"], [\"Mass\"]], "
21992199
"\"entities\":[\"e3\"], "
2200-
"\"values\":[[{\"x\":30, \"y\":40}], [{\"value\":100}], 0]}]"
2200+
"\"values\":[[{\"x\":30, \"y\":40}], [{\"value\":100}]]}]"
22012201
"}");
22022202

22032203
ecs_os_free(json);
@@ -2275,17 +2275,17 @@ void SerializeIterToJson_serialize_table_w_id_labels(void) {
22752275

22762276
test_str(json, "{\"results\":["
22772277
"{"
2278-
"\"id_labels\":[[\"position\"], [\"Foo\"], [\"Identifier\", \"Name\"]], "
2278+
"\"id_labels\":[[\"position\"], [\"Foo\"]], "
22792279
"\"entities\":[\"e1\"], "
2280-
"\"values\":[[{\"x\":10, \"y\":20}], 0, 0]"
2280+
"\"values\":[[{\"x\":10, \"y\":20}], 0]"
22812281
"}, {"
2282-
"\"id_labels\":[[\"position\"], [\"velocity\"], [\"Foo\"], [\"Bar\"], [\"Identifier\", \"Name\"]], "
2282+
"\"id_labels\":[[\"position\"], [\"velocity\"], [\"Foo\"], [\"Bar\"]], "
22832283
"\"entities\":[\"e2\"], "
2284-
"\"values\":[[{\"x\":20, \"y\":30}], [{\"x\":1, \"y\":1}], 0, 0, 0]"
2284+
"\"values\":[[{\"x\":20, \"y\":30}], [{\"x\":1, \"y\":1}], 0, 0]"
22852285
"}, {"
2286-
"\"id_labels\":[[\"position\"], [\"mass\"], [\"Identifier\", \"Name\"]], "
2286+
"\"id_labels\":[[\"position\"], [\"mass\"]], "
22872287
"\"entities\":[\"e3\"], "
2288-
"\"values\":[[{\"x\":30, \"y\":40}], [{\"value\":100}], 0]}]"
2288+
"\"values\":[[{\"x\":30, \"y\":40}], [{\"value\":100}]]}]"
22892289
"}");
22902290

22912291
ecs_os_free(json);
@@ -2366,6 +2366,106 @@ void SerializeIterToJson_serialize_table_w_var_labels(void) {
23662366
char *json = ecs_iter_to_json(world, &it, &desc);
23672367
test_assert(json != NULL);
23682368

2369+
test_str(json, "{\"vars\":[\"p\"], \"results\":["
2370+
"{"
2371+
"\"ids\":[[\"Position\"], [\"Foo\"]], "
2372+
"\"var_labels\":[\"parent\"], "
2373+
"\"parent\":\"Parent\", "
2374+
"\"entities\":[\"e1\"], "
2375+
"\"values\":[[{\"x\":10, \"y\":20}], 0]"
2376+
"}, {"
2377+
"\"ids\":[[\"Position\"], [\"Velocity\"], [\"Foo\"], [\"Bar\"]], "
2378+
"\"var_labels\":[\"parent\"], "
2379+
"\"parent\":\"Parent\", "
2380+
"\"entities\":[\"e2\"], "
2381+
"\"values\":[[{\"x\":20, \"y\":30}], [{\"x\":1, \"y\":1}], 0, 0]"
2382+
"}, {"
2383+
"\"ids\":[[\"Position\"], [\"Mass\"]], "
2384+
"\"var_labels\":[\"parent\"], "
2385+
"\"parent\":\"Parent\", "
2386+
"\"entities\":[\"e3\"], "
2387+
"\"values\":[[{\"x\":30, \"y\":40}], [{\"value\":100}]]}]"
2388+
"}");
2389+
2390+
ecs_os_free(json);
2391+
2392+
ecs_rule_fini(f);
2393+
2394+
ecs_fini(world);
2395+
}
2396+
2397+
void SerializeIterToJson_serialize_table_w_private(void) {
2398+
ecs_world_t *world = ecs_init();
2399+
2400+
ECS_COMPONENT(world, Position);
2401+
ECS_COMPONENT(world, Velocity);
2402+
ECS_COMPONENT(world, Mass);
2403+
ECS_TAG(world, Foo);
2404+
ECS_TAG(world, Bar);
2405+
2406+
ecs_struct_init(world, &(ecs_struct_desc_t){
2407+
.entity = ecs_id(Position),
2408+
.members = {
2409+
{"x", ecs_id(ecs_i32_t)},
2410+
{"y", ecs_id(ecs_i32_t)}
2411+
}
2412+
});
2413+
2414+
ecs_struct_init(world, &(ecs_struct_desc_t){
2415+
.entity = ecs_id(Velocity),
2416+
.members = {
2417+
{"x", ecs_id(ecs_i32_t)},
2418+
{"y", ecs_id(ecs_i32_t)}
2419+
}
2420+
});
2421+
2422+
ecs_struct_init(world, &(ecs_struct_desc_t){
2423+
.entity = ecs_id(Mass),
2424+
.members = {
2425+
{"value", ecs_id(ecs_i32_t)}
2426+
}
2427+
});
2428+
2429+
ecs_entity_t parent = ecs_new_entity(world, "Parent");
2430+
ecs_doc_set_name(world, parent, "parent");
2431+
2432+
ecs_entity_t e1 = ecs_new_entity(world, "e1");
2433+
ecs_entity_t e2 = ecs_new_entity(world, "e2");
2434+
ecs_entity_t e3 = ecs_new_entity(world, "e3");
2435+
2436+
ecs_add_pair(world, e1, EcsChildOf, parent);
2437+
ecs_add_pair(world, e2, EcsChildOf, parent);
2438+
ecs_add_pair(world, e3, EcsChildOf, parent);
2439+
2440+
ecs_set(world, e1, Position, {10, 20});
2441+
ecs_set(world, e2, Position, {20, 30});
2442+
ecs_set(world, e3, Position, {30, 40});
2443+
2444+
ecs_add(world, e1, Foo);
2445+
ecs_add(world, e2, Foo);
2446+
ecs_add(world, e2, Bar);
2447+
2448+
ecs_set(world, e2, Velocity, {1, 1});
2449+
ecs_set(world, e3, Mass, {100});
2450+
2451+
ecs_rule_t *f = ecs_rule(world, {
2452+
.terms = {
2453+
{ .id = ecs_id(Position) },
2454+
{ .first.id = EcsChildOf, .second.name = "p", .second.flags = EcsIsVariable }
2455+
}
2456+
});
2457+
2458+
ecs_iter_t it = ecs_rule_iter(world, f);
2459+
2460+
ecs_iter_to_json_desc_t desc = {0};
2461+
desc.serialize_table = true;
2462+
desc.serialize_entities = true;
2463+
desc.serialize_variable_labels = true;
2464+
desc.serialize_ids = true;
2465+
desc.serialize_private = true;
2466+
char *json = ecs_iter_to_json(world, &it, &desc);
2467+
test_assert(json != NULL);
2468+
23692469
test_str(json, "{\"vars\":[\"p\"], \"results\":["
23702470
"{"
23712471
"\"ids\":[[\"Position\"], [\"Foo\"], [\"flecs.core.Identifier\",\"flecs.core.Name\"], [\"flecs.core.ChildOf\",\"Parent\"]], "

0 commit comments

Comments
 (0)