Skip to content
Flecs v4.1
reflection/query_to_custom_json

  • C

    View on GitHub

    1#include <query_to_custom_json.h>
    2#include <stdio.h>
    3
    4// Same example as query_to_json, but with customized serializer parameters
    5
    6typedef struct {
    7 float x, y;
    8} Position, Velocity;
    9
    10typedef struct {
    11 float value;
    12} Mass;
    13
    14int main(int argc, char *argv[]) {
    15 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    16
    17 // Register component as usual
    18 ECS_COMPONENT(ecs, Position);
    19 ECS_COMPONENT(ecs, Velocity);
    20 ECS_COMPONENT(ecs, Mass);
    21
    22 // Add reflection data to components
    23 ecs_struct(ecs, {
    24 .entity = ecs_id(Position), // Make sure to use existing id
    25 .members = {
    26 { .name = "x", .type = ecs_id(ecs_f32_t) },
    27 { .name = "y", .type = ecs_id(ecs_f32_t) }
    28 }
    29 });
    30
    31 ecs_struct(ecs, {
    32 .entity = ecs_id(Velocity), // Make sure to use existing id
    33 .members = {
    34 { .name = "x", .type = ecs_id(ecs_f32_t) },
    35 { .name = "y", .type = ecs_id(ecs_f32_t) }
    36 }
    37 });
    38
    39 ecs_struct(ecs, {
    40 .entity = ecs_id(Mass), // Make sure to use existing id
    41 .members = {
    42 { .name = "value", .type = ecs_id(ecs_f32_t) }
    43 }
    44 });
    45
    46 ecs_entity_t a = ecs_set_name(ecs, 0, "a");
    47 ecs_entity_t b = ecs_set_name(ecs, 0, "b");
    48 ecs_entity_t c = ecs_set_name(ecs, 0, "c");
    49 ecs_entity_t d = ecs_set_name(ecs, 0, "d");
    50
    51 ecs_set(ecs, a, Position, {10, 20});
    52 ecs_set(ecs, b, Position, {20, 30});
    53 ecs_set(ecs, c, Position, {30, 40});
    54 ecs_set(ecs, d, Position, {40, 50});
    55
    56 ecs_set(ecs, a, Velocity, {1, 2});
    57 ecs_set(ecs, b, Velocity, {2, 3});
    58 ecs_set(ecs, c, Velocity, {3, 4});
    59 ecs_set(ecs, d, Velocity, {4, 5});
    60
    61 ecs_set(ecs, c, Mass, {10});
    62 ecs_set(ecs, d, Mass, {20});
    63
    64 // Query for components
    65 ecs_query_t *q = ecs_query(ecs, {
    66 .terms = {
    67 { .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }
    68 }
    69 });
    70
    71 ecs_iter_t it = ecs_query_iter(ecs, q);
    72
    73 // Serialize query to JSON. Customize serializer to only serialize entity
    74 // names and component values.
    76 .serialize_values = true
    77 };
    78 char *json = ecs_iter_to_json(&it, &desc);
    79 printf("%s\n", json);
    80 ecs_os_free(json);
    81
    82 // Iterator returns 2 sets of results, one for each table.
    83
    84 // {
    85 // "results": [
    86 // {
    87 // "name": "a",
    88 // "fields": [
    89 // {
    90 // "data": {
    91 // "x": 10,
    92 // "y": 20
    93 // }
    94 // },
    95 // {
    96 // "data": {
    97 // "x": 1,
    98 // "y": 2
    99 // }
    100 // }
    101 // ]
    102 // },
    103 // {
    104 // "name": "b",
    105 // "fields": [
    106 // {
    107 // "data": {
    108 // "x": 20,
    109 // "y": 30
    110 // }
    111 // },
    112 // {
    113 // "data": {
    114 // "x": 2,
    115 // "y": 3
    116 // }
    117 // }
    118 // ]
    119 // },
    120 // {
    121 // "name": "c",
    122 // "fields": [
    123 // {
    124 // "data": {
    125 // "x": 30,
    126 // "y": 40
    127 // }
    128 // },
    129 // {
    130 // "data": {
    131 // "x": 3,
    132 // "y": 4
    133 // }
    134 // }
    135 // ]
    136 // },
    137 // {
    138 // "name": "d",
    139 // "fields": [
    140 // {
    141 // "data": {
    142 // "x": 30,
    143 // "y": 40
    144 // }
    145 // },
    146 // {
    147 // "data": {
    148 // "x": 4,
    149 // "y": 5
    150 // }
    151 // }
    152 // ]
    153 // }
    154 // ]
    155 // }
    156
    157 ecs_fini(ecs);
    158}
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    FLECS_API char * ecs_iter_to_json(ecs_iter_t *iter, const ecs_iter_to_json_desc_t *desc)
    Serialize iterator into JSON string.
    float ecs_f32_t
    Built-in f32 type.
    Definition meta.h:108
    #define ecs_struct(world,...)
    Create a struct type.
    Definition meta.h:1777
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
    Set the name of an entity.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Iterator.
    Definition flecs.h:1183
    Used with ecs_iter_to_json().
    Definition json.h:301
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • C++

    View on GitHub

    1#include <query_to_custom_json.h>
    2#include <iostream>
    3
    4// Same example as query_to_json, but with customized serializer parameters
    5
    6struct Position {
    7 float x;
    8 float y;
    9};
    10
    11struct Velocity {
    12 float x;
    13 float y;
    14};
    15
    16struct Mass {
    17 float value;
    18};
    19
    20int main(int, char *[]) {
    21 flecs::world ecs;
    22
    23 // Register components with reflection data
    24 ecs.component<Position>()
    25 .member<float>("x")
    26 .member<float>("y");
    27
    28 ecs.component<Velocity>()
    29 .member<float>("x")
    30 .member<float>("y");
    31
    32 ecs.component<Mass>()
    33 .member<float>("value");
    34
    35 ecs.entity("a").set<Position>({10, 20}).set<Velocity>({1, 2});
    36 ecs.entity("b").set<Position>({20, 30}).set<Velocity>({2, 3});
    37 ecs.entity("c").set<Position>({30, 40}).set<Velocity>({3, 4}).set<Mass>({10});
    38 ecs.entity("d").set<Position>({30, 40}).set<Velocity>({4, 5}).set<Mass>({20});
    39
    40 // Query for components
    42 ecs.query<Position, const Velocity>();
    43
    44 // Serialize query to JSON. Only serialize entity names.
    45 flecs::iter_to_json_desc_t desc = ECS_ITER_TO_JSON_INIT;
    46 desc.serialize_fields = false;
    47 std::cout << q.iter().to_json(&desc).c_str() << "\n";
    48
    49 // Iterator returns 2 sets of results, one for each table.
    50
    51 // {
    52 // "results": [
    53 // {
    54 // "name": "a"
    55 // },
    56 // {
    57 // "name": "b"
    58 // },
    59 // {
    60 // "name": "c"
    61 // },
    62 // {
    63 // "name": "d"
    64 // }
    65 // ]
    66 // }
    67}
    #define ECS_ITER_TO_JSON_INIT
    Utility used to initialize JSON iterator serializer.
    Definition json.h:329
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.
    Definition impl.hpp:326
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129