Skip to content

Commit

Permalink
Allow for X_TO_JSON_INIT macros to be used in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Nov 20, 2024
1 parent 2b3f393 commit 2a23e6e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
40 changes: 40 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13592,7 +13592,9 @@ typedef struct ecs_entity_to_json_desc_t {
} ecs_entity_to_json_desc_t;

/** Utility used to initialize JSON entity serializer. */
#ifndef __cplusplus
#define ECS_ENTITY_TO_JSON_INIT (ecs_entity_to_json_desc_t){\
.serialize_entity_id = false, \
.serialize_doc = false, \
.serialize_full_paths = true, \
.serialize_inherited = false, \
Expand All @@ -13603,6 +13605,20 @@ typedef struct ecs_entity_to_json_desc_t {
.serialize_refs = 0, \
.serialize_matches = false, \
}
#else
#define ECS_ENTITY_TO_JSON_INIT {\
false, \
false, \
true, \
false, \
true, \
false, \
false, \
false, \
0, \
false, \
}
#endif

/** Serialize entity into JSON string.
* This creates a JSON object with the entity's (path) name, which components
Expand Down Expand Up @@ -13658,6 +13674,7 @@ typedef struct ecs_iter_to_json_desc_t {
} ecs_iter_to_json_desc_t;

/** Utility used to initialize JSON iterator serializer. */
#ifndef __cplusplus
#define ECS_ITER_TO_JSON_INIT (ecs_iter_to_json_desc_t){\
.serialize_entity_ids = false, \
.serialize_values = true, \
Expand All @@ -13676,7 +13693,30 @@ typedef struct ecs_iter_to_json_desc_t {
.serialize_alerts = false, \
.serialize_refs = false, \
.serialize_matches = false, \
.query = NULL \
}
#else
#define ECS_ITER_TO_JSON_INIT {\
false, \
true, \
false, \
false, \
true, \
true, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
nullptr \
}
#endif

/** Serialize iterator into JSON string.
* This operation will iterate the contents of the iterator and serialize them
Expand Down
40 changes: 40 additions & 0 deletions include/flecs/addons/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ typedef struct ecs_entity_to_json_desc_t {
} ecs_entity_to_json_desc_t;

/** Utility used to initialize JSON entity serializer. */
#ifndef __cplusplus
#define ECS_ENTITY_TO_JSON_INIT (ecs_entity_to_json_desc_t){\
.serialize_entity_id = false, \
.serialize_doc = false, \
.serialize_full_paths = true, \
.serialize_inherited = false, \
Expand All @@ -238,6 +240,20 @@ typedef struct ecs_entity_to_json_desc_t {
.serialize_refs = 0, \
.serialize_matches = false, \
}
#else
#define ECS_ENTITY_TO_JSON_INIT {\
false, \
false, \
true, \
false, \
true, \
false, \
false, \
false, \
0, \
false, \
}
#endif

/** Serialize entity into JSON string.
* This creates a JSON object with the entity's (path) name, which components
Expand Down Expand Up @@ -293,6 +309,7 @@ typedef struct ecs_iter_to_json_desc_t {
} ecs_iter_to_json_desc_t;

/** Utility used to initialize JSON iterator serializer. */
#ifndef __cplusplus
#define ECS_ITER_TO_JSON_INIT (ecs_iter_to_json_desc_t){\
.serialize_entity_ids = false, \
.serialize_values = true, \
Expand All @@ -311,7 +328,30 @@ typedef struct ecs_iter_to_json_desc_t {
.serialize_alerts = false, \
.serialize_refs = false, \
.serialize_matches = false, \
.query = NULL \
}
#else
#define ECS_ITER_TO_JSON_INIT {\
false, \
true, \
false, \
false, \
true, \
true, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
false, \
nullptr \
}
#endif

/** Serialize iterator into JSON string.
* This operation will iterate the contents of the iterator and serialize them
Expand Down
4 changes: 3 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,9 @@
"out_of_order_member_declaration",
"entity_to_json",
"iter_to_json",
"query_to_json"
"query_to_json",
"entity_to_json_w_default_desc",
"query_to_json_w_default_desc"
]
}, {
"id": "Table",
Expand Down
22 changes: 22 additions & 0 deletions test/cpp/src/Meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,3 +1435,25 @@ void Meta_query_to_json(void) {

test_str(q.to_json().c_str(), "{\"results\":[{\"name\":\"foo\", \"fields\":{\"values\":[0]}}]}");
}

void Meta_entity_to_json_w_default_desc(void) {
flecs::world ecs;

flecs::entity e = ecs.entity("foo").set<Position>({10, 20});

flecs::entity_to_json_desc_t desc = ECS_ENTITY_TO_JSON_INIT;

test_str(e.to_json(&desc).c_str(), "{\"name\":\"foo\", \"components\":{\"Position\":null}}");
}

void Meta_query_to_json_w_default_desc(void) {
flecs::world ecs;

ecs.entity("foo").set<Position>({10, 20});

auto q = ecs.query<Position>();

flecs::iter_to_json_desc_t desc = ECS_ITER_TO_JSON_INIT;

test_str(q.to_json(&desc).c_str(), "{\"results\":[{\"name\":\"foo\", \"fields\":{\"values\":[0]}}]}");
}
12 changes: 11 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,8 @@ void Meta_out_of_order_member_declaration(void);
void Meta_entity_to_json(void);
void Meta_iter_to_json(void);
void Meta_query_to_json(void);
void Meta_entity_to_json_w_default_desc(void);
void Meta_query_to_json_w_default_desc(void);

// Testsuite 'Table'
void Table_each(void);
Expand Down Expand Up @@ -6664,6 +6666,14 @@ bake_test_case Meta_testcases[] = {
{
"query_to_json",
Meta_query_to_json
},
{
"entity_to_json_w_default_desc",
Meta_entity_to_json_w_default_desc
},
{
"query_to_json_w_default_desc",
Meta_query_to_json_w_default_desc
}
};

Expand Down Expand Up @@ -6984,7 +6994,7 @@ static bake_test_suite suites[] = {
"Meta",
NULL,
NULL,
59,
61,
Meta_testcases
},
{
Expand Down

0 comments on commit 2a23e6e

Please sign in to comment.