Skip to content

Commit

Permalink
#762 add functions for serializing world to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Feb 2, 2023
1 parent 76cfd3d commit 9e1327e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 2 deletions.
28 changes: 28 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35273,6 +35273,34 @@ char* ecs_iter_to_json(
return ecs_strbuf_get(&buf);
}

int ecs_world_to_json_buf(
ecs_world_t *world,
ecs_strbuf_t *buf_out)
{
ecs_filter_t f = ECS_FILTER_INIT;
ecs_filter(world, {
.terms = {{ .id = EcsAny }},
.storage = &f
});

ecs_iter_t it = ecs_filter_iter(world, &f);
ecs_iter_to_json_desc_t json_desc = { .serialize_table = true };
return ecs_iter_to_json_buf(world, &it, buf_out, &json_desc);
}

char* ecs_world_to_json(
ecs_world_t *world)
{
ecs_strbuf_t buf = ECS_STRBUF_INIT;

if (ecs_world_to_json_buf(world, &buf)) {
ecs_strbuf_reset(&buf);
return NULL;
}

return ecs_strbuf_get(&buf);
}

#endif

/**
Expand Down
40 changes: 40 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11767,6 +11767,37 @@ int ecs_iter_to_json_buf(
ecs_strbuf_t *buf_out,
const ecs_iter_to_json_desc_t *desc);

/** Serialize world into JSON string.
* This operation iterates the contents of the world to JSON. The operation is
* equivalent to the following code:
*
* ecs_filter_t *f = ecs_filter(world, {
* .terms = {{ .id = EcsAny }}
* });
*
* ecs_iter_t it = ecs_filter_init(world, &f);
* ecs_iter_to_json_desc_t desc = { .serialize_table = true };
* ecs_iter_to_json(world, iter, &desc);
*
* @param world The world to serialize.
* @return A JSON string with the serialized iterator data, or NULL if failed.
*/
FLECS_API
char* ecs_world_to_json(
ecs_world_t *world);

/** Serialize world into JSON string buffer.
* Same as ecs_world_to_json, but serializes to an ecs_strbuf_t instance.
*
* @param world The world to serialize.
* @param buf_out The strbuf to append the string to.
* @return Zero if success, non-zero if failed.
*/
FLECS_API
int ecs_world_to_json_buf(
ecs_world_t *world,
ecs_strbuf_t *buf_out);

#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -19135,6 +19166,15 @@ flecs::string to_json(const T* value) {
return to_json(tid, value);
}

/** Serialize world to JSON.
*
* \memberof flecs::world
* \ingroup cpp_addons_json
*/
flecs::string to_json() {
return flecs::string( ecs_world_to_json(m_world) );
}

# endif
# ifdef FLECS_APP
/**
Expand Down
9 changes: 9 additions & 0 deletions include/flecs/addons/cpp/mixins/json/world.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ flecs::string to_json(const T* value) {
flecs::entity_t tid = _::cpp_type<T>::id(m_world);
return to_json(tid, value);
}

/** Serialize world to JSON.
*
* \memberof flecs::world
* \ingroup cpp_addons_json
*/
flecs::string to_json() {
return flecs::string( ecs_world_to_json(m_world) );
}
31 changes: 31 additions & 0 deletions include/flecs/addons/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,37 @@ int ecs_iter_to_json_buf(
ecs_strbuf_t *buf_out,
const ecs_iter_to_json_desc_t *desc);

/** Serialize world into JSON string.
* This operation iterates the contents of the world to JSON. The operation is
* equivalent to the following code:
*
* ecs_filter_t *f = ecs_filter(world, {
* .terms = {{ .id = EcsAny }}
* });
*
* ecs_iter_t it = ecs_filter_init(world, &f);
* ecs_iter_to_json_desc_t desc = { .serialize_table = true };
* ecs_iter_to_json(world, iter, &desc);
*
* @param world The world to serialize.
* @return A JSON string with the serialized iterator data, or NULL if failed.
*/
FLECS_API
char* ecs_world_to_json(
ecs_world_t *world);

/** Serialize world into JSON string buffer.
* Same as ecs_world_to_json, but serializes to an ecs_strbuf_t instance.
*
* @param world The world to serialize.
* @param buf_out The strbuf to append the string to.
* @return Zero if success, non-zero if failed.
*/
FLECS_API
int ecs_world_to_json_buf(
ecs_world_t *world,
ecs_strbuf_t *buf_out);

#ifdef __cplusplus
}
#endif
Expand Down
28 changes: 28 additions & 0 deletions src/addons/json/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1667,4 +1667,32 @@ char* ecs_iter_to_json(
return ecs_strbuf_get(&buf);
}

int ecs_world_to_json_buf(
ecs_world_t *world,
ecs_strbuf_t *buf_out)
{
ecs_filter_t f = ECS_FILTER_INIT;
ecs_filter(world, {
.terms = {{ .id = EcsAny }},
.storage = &f
});

ecs_iter_t it = ecs_filter_iter(world, &f);
ecs_iter_to_json_desc_t json_desc = { .serialize_table = true };
return ecs_iter_to_json_buf(world, &it, buf_out, &json_desc);
}

char* ecs_world_to_json(
ecs_world_t *world)
{
ecs_strbuf_t buf = ECS_STRBUF_INIT;

if (ecs_world_to_json_buf(world, &buf)) {
ecs_strbuf_reset(&buf);
return NULL;
}

return ecs_strbuf_get(&buf);
}

#endif
3 changes: 2 additions & 1 deletion test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@
"serialize_paged_iterator_w_optional_component",
"serialize_paged_iterator_w_optional_tag",
"serialize_paged_iterator_w_vars",
"serialize_table"
"serialize_table",
"serialize_world"
]
}, {
"id": "SerializeTypeInfoToJson",
Expand Down
21 changes: 21 additions & 0 deletions test/meta/src/SerializeToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -4267,3 +4267,24 @@ void SerializeToJson_serialize_table() {

ecs_fini(world);
}

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

/* We can't test if the output for an exact value as this would change any
* time something is added/removed to the world. */

char *json_1 = ecs_world_to_json(world);
test_assert(json_1 != NULL);

char *json_2 = ecs_world_to_json(world);
test_assert(json_2 != NULL);

test_assert(json_1 != json_2);
test_str(json_1, json_2);

ecs_os_free(json_1);
ecs_os_free(json_2);

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ void SerializeToJson_serialize_paged_iterator_w_optional_component(void);
void SerializeToJson_serialize_paged_iterator_w_optional_tag(void);
void SerializeToJson_serialize_paged_iterator_w_vars(void);
void SerializeToJson_serialize_table(void);
void SerializeToJson_serialize_world(void);

// Testsuite 'SerializeTypeInfoToJson'
void SerializeTypeInfoToJson_bool(void);
Expand Down Expand Up @@ -3045,6 +3046,10 @@ bake_test_case SerializeToJson_testcases[] = {
{
"serialize_table",
SerializeToJson_serialize_table
},
{
"serialize_world",
SerializeToJson_serialize_world
}
};

Expand Down Expand Up @@ -3799,7 +3804,7 @@ static bake_test_suite suites[] = {
"SerializeToJson",
NULL,
NULL,
120,
121,
SerializeToJson_testcases
},
{
Expand Down

0 comments on commit 9e1327e

Please sign in to comment.