Skip to content
Flecs v4.1
json.h
Go to the documentation of this file.
1/**
2 * @file addons/json.h
3 * @brief JSON parser addon.
4 *
5 * Parse expression strings into component values. Entity identifiers,
6 * enumerations, and bitmasks are encoded as strings.
7 *
8 * See docs/FlecsRemoteApi.md for a description of the JSON format.
9 */
10
11#ifdef FLECS_JSON
12
13#ifndef FLECS_META
14#define FLECS_META
15#endif
16
17#ifndef FLECS_DOC
18#define FLECS_DOC
19#endif
20
21#ifndef FLECS_QUERY_DSL
22#define FLECS_QUERY_DSL /* For parsing component ID expressions */
23#endif
24
25#ifndef FLECS_JSON_H
26#define FLECS_JSON_H
27
28/**
29 * @defgroup c_addons_json Json
30 * @ingroup c_addons
31 * Functions for serializing to and from JSON.
32 *
33 * @{
34 */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/** Used with ecs_ptr_from_json(), ecs_entity_from_json(). */
41typedef struct ecs_from_json_desc_t {
42 const char *name; /**< Name of the expression (used for logging). */
43 const char *expr; /**< Full expression (used for logging). */
44
45 /** Callback that allows for specifying a custom lookup function. The
46 * default behavior uses ecs_lookup(). */
49 const char *value,
50 void *ctx);
51 void *lookup_ctx; /**< Context for lookup_action. */
52
53 /** Require components to be registered with reflection data. When not
54 * in strict mode, values for components without reflection are ignored. */
55 bool strict;
57
58/** Parse JSON string into value.
59 * This operation parses a JSON expression into the provided pointer. The
60 * memory pointed to must be large enough to contain a value of the used type.
61 *
62 * @param world The world.
63 * @param type The type of the expression to parse.
64 * @param ptr Pointer to the memory to write to.
65 * @param json The JSON expression to parse.
66 * @param desc Configuration parameters for the deserializer.
67 * @return Pointer to the character after the last one read, or NULL if failed.
68 */
69FLECS_API
71 const ecs_world_t *world,
72 ecs_entity_t type,
73 void *ptr,
74 const char *json,
75 const ecs_from_json_desc_t *desc);
76
77/** Parse JSON object with multiple component values into an entity. The format
78 * is the same as the one output by ecs_entity_to_json(), but at the moment
79 * only supports the "ids" and "values" members.
80 *
81 * @param world The world.
82 * @param entity The entity to deserialize into.
83 * @param json The JSON expression to parse (see entity in JSON format manual).
84 * @param desc Configuration parameters for the deserializer.
85 * @return Pointer to the character after the last one read, or NULL if failed.
86 */
87FLECS_API
89 ecs_world_t *world,
90 ecs_entity_t entity,
91 const char *json,
92 const ecs_from_json_desc_t *desc);
93
94/** Parse JSON object with multiple entities into the world. The format is the
95 * same as the one output by ecs_world_to_json().
96 *
97 * @param world The world.
98 * @param json The JSON expression to parse (see iterator in JSON format manual).
99 * @param desc Deserialization parameters.
100 * @return Last deserialized character, NULL if failed.
101 */
102FLECS_API
104 ecs_world_t *world,
105 const char *json,
106 const ecs_from_json_desc_t *desc);
107
108/** Same as ecs_world_from_json(), but loads JSON from a file.
109 *
110 * @param world The world.
111 * @param filename The file from which to load the JSON.
112 * @param desc Deserialization parameters.
113 * @return Last deserialized character, NULL if failed.
114 */
115FLECS_API
117 ecs_world_t *world,
118 const char *filename,
119 const ecs_from_json_desc_t *desc);
120
121/** Serialize array into JSON string.
122 * This operation serializes a value of the provided type to a JSON string. The
123 * memory pointed to must be large enough to contain a value of the used type.
124 *
125 * If count is 0, the function will serialize a single value, not wrapped in
126 * array brackets. If count is >= 1, the operation will serialize values to
127 * a comma-separated list inside of array brackets.
128 *
129 * @param world The world.
130 * @param type The type of the value to serialize.
131 * @param data The value to serialize.
132 * @param count The number of elements to serialize.
133 * @return String with JSON expression, or NULL if failed.
134 */
135FLECS_API
137 const ecs_world_t *world,
138 ecs_entity_t type,
139 const void *data,
140 int32_t count);
141
142/** Serialize array into JSON string buffer.
143 * Same as ecs_array_to_json(), but serializes to an ecs_strbuf_t instance.
144 *
145 * @param world The world.
146 * @param type The type of the value to serialize.
147 * @param data The value to serialize.
148 * @param count The number of elements to serialize.
149 * @param buf_out The strbuf to append the string to.
150 * @return Zero if success, non-zero if failed.
151 */
152FLECS_API
154 const ecs_world_t *world,
155 ecs_entity_t type,
156 const void *data,
157 int32_t count,
158 ecs_strbuf_t *buf_out);
159
160/** Serialize value into JSON string.
161 * Same as ecs_array_to_json(), with count = 0.
162 *
163 * @param world The world.
164 * @param type The type of the value to serialize.
165 * @param data The value to serialize.
166 * @return String with JSON expression, or NULL if failed.
167 */
168FLECS_API
170 const ecs_world_t *world,
171 ecs_entity_t type,
172 const void *data);
173
174/** Serialize value into JSON string buffer.
175 * Same as ecs_ptr_to_json(), but serializes to an ecs_strbuf_t instance.
176 *
177 * @param world The world.
178 * @param type The type of the value to serialize.
179 * @param data The value to serialize.
180 * @param buf_out The strbuf to append the string to.
181 * @return Zero if success, non-zero if failed.
182 */
183FLECS_API
185 const ecs_world_t *world,
186 ecs_entity_t type,
187 const void *data,
188 ecs_strbuf_t *buf_out);
189
190/** Serialize type info to JSON.
191 * This serializes type information to JSON, and can be used to store or transmit
192 * the structure of a (component) value.
193 *
194 * If the provided type does not have reflection data, "0" will be returned.
195 *
196 * @param world The world.
197 * @param type The type to serialize to JSON.
198 * @return A JSON string with the serialized type info, or NULL if failed.
199 */
200FLECS_API
202 const ecs_world_t *world,
203 ecs_entity_t type);
204
205/** Serialize type info into JSON string buffer.
206 * Same as ecs_type_info_to_json(), but serializes to an ecs_strbuf_t instance.
207 *
208 * @param world The world.
209 * @param type The type to serialize.
210 * @param buf_out The strbuf to append the string to.
211 * @return Zero if success, non-zero if failed.
212 */
213FLECS_API
215 const ecs_world_t *world,
216 ecs_entity_t type,
217 ecs_strbuf_t *buf_out);
218
219/** Used with ecs_entity_to_json(). */
221 bool serialize_entity_id; /**< Serialize entity ID. */
222 bool serialize_doc; /**< Serialize doc attributes. */
223 bool serialize_full_paths; /**< Serialize full paths for tags, components, and pairs. */
224 bool serialize_inherited; /**< Serialize base components. */
225 bool serialize_values; /**< Serialize component values. */
226 bool serialize_builtin; /**< Serialize built-in data as components (e.g., "name", "parent"). */
227 bool serialize_type_info; /**< Serialize type info (requires serialize_values). */
228 bool serialize_alerts; /**< Serialize active alerts for the entity. */
229 ecs_entity_t serialize_refs; /**< Serialize references (incoming edges) for a relationship. */
230 bool serialize_matches; /**< Serialize which queries the entity matches with. */
231 /** Callback to determine if a component should be serialized. */
233 (const ecs_world_t *, ecs_entity_t);
235
236/** Utility used to initialize JSON entity serializer. */
237#ifndef __cplusplus
238#define ECS_ENTITY_TO_JSON_INIT (ecs_entity_to_json_desc_t){\
239 .serialize_entity_id = false, \
240 .serialize_doc = false, \
241 .serialize_full_paths = true, \
242 .serialize_inherited = false, \
243 .serialize_values = true, \
244 .serialize_builtin = false, \
245 .serialize_type_info = false, \
246 .serialize_alerts = false, \
247 .serialize_refs = 0, \
248 .serialize_matches = false, \
249 .component_filter = NULL, \
250}
251#else
252#define ECS_ENTITY_TO_JSON_INIT {\
253 false, \
254 false, \
255 true, \
256 false, \
257 true, \
258 false, \
259 false, \
260 false, \
261 0, \
262 false, \
263 nullptr, \
264}
265#endif
266
267/** Serialize entity into JSON string.
268 * This creates a JSON object with the entity's (path) name, which components
269 * and tags the entity has, and the component values.
270 *
271 * The operation may fail if the entity contains components with invalid values.
272 *
273 * @param world The world.
274 * @param entity The entity to serialize to JSON.
275 * @param desc Serialization parameters.
276 * @return A JSON string with the serialized entity data, or NULL if failed.
277 */
278FLECS_API
280 ecs_world_t *world,
281 ecs_entity_t entity,
282 const ecs_entity_to_json_desc_t *desc);
283
284/** Serialize entity into JSON string buffer.
285 * Same as ecs_entity_to_json(), but serializes to an ecs_strbuf_t instance.
286 *
287 * @param world The world.
288 * @param entity The entity to serialize.
289 * @param buf_out The strbuf to append the string to.
290 * @param desc Serialization parameters.
291 * @return Zero if success, non-zero if failed.
292 */
293FLECS_API
295 ecs_world_t *world,
296 ecs_entity_t entity,
297 ecs_strbuf_t *buf_out,
298 const ecs_entity_to_json_desc_t *desc);
299
300/** Used with ecs_iter_to_json(). */
302 bool serialize_entity_ids; /**< Serialize entity IDs. */
303 bool serialize_values; /**< Serialize component values. */
304 bool serialize_builtin; /**< Serialize built-in data as components (e.g., "name", "parent"). */
305 bool serialize_doc; /**< Serialize doc attributes. */
306 bool serialize_full_paths; /**< Serialize full paths for tags, components, and pairs. */
307 bool serialize_fields; /**< Serialize field data. */
308 bool serialize_inherited; /**< Serialize inherited components. */
309 bool serialize_table; /**< Serialize entire table vs. matched components. */
310 bool serialize_type_info; /**< Serialize type information. */
311 bool serialize_field_info; /**< Serialize metadata for fields returned by the query. */
312 bool serialize_query_info; /**< Serialize query terms. */
313 bool serialize_query_plan; /**< Serialize query plan. */
314 bool serialize_query_profile; /**< Profile query performance. */
315 bool dont_serialize_results; /**< If true, the query will not be evaluated. */
316 bool serialize_alerts; /**< Serialize active alerts for the entity. */
317 ecs_entity_t serialize_refs; /**< Serialize references (incoming edges) for a relationship. */
318 bool serialize_matches; /**< Serialize which queries the entity matches with. */
319 bool serialize_parents_before_children; /**< If the query matches both children and parent, serialize the parent before children. */
320
321 /** Callback to determine if a component should be serialized. */
323 (const ecs_world_t *, ecs_entity_t);
324 ecs_poly_t *query; /**< Query object (required for serialize_query_[plan|profile]). */
326
327/** Utility used to initialize JSON iterator serializer. */
328#ifndef __cplusplus
329#define ECS_ITER_TO_JSON_INIT (ecs_iter_to_json_desc_t){\
330 .serialize_entity_ids = false, \
331 .serialize_values = true, \
332 .serialize_builtin = false, \
333 .serialize_doc = false, \
334 .serialize_full_paths = true, \
335 .serialize_fields = true, \
336 .serialize_inherited = false, \
337 .serialize_table = false, \
338 .serialize_type_info = false, \
339 .serialize_field_info = false, \
340 .serialize_query_info = false, \
341 .serialize_query_plan = false, \
342 .serialize_query_profile = false, \
343 .dont_serialize_results = false, \
344 .serialize_alerts = false, \
345 .serialize_refs = false, \
346 .serialize_matches = false, \
347 .serialize_parents_before_children = false,\
348 .component_filter = NULL, \
349 .query = NULL \
350}
351#else
352#define ECS_ITER_TO_JSON_INIT {\
353 false, \
354 true, \
355 false, \
356 false, \
357 true, \
358 true, \
359 false, \
360 false, \
361 false, \
362 false, \
363 false, \
364 false, \
365 false, \
366 false, \
367 false, \
368 false, \
369 false, \
370 false, \
371 nullptr, \
372 nullptr \
373}
374#endif
375
376/** Serialize iterator into JSON string.
377 * This operation will iterate the contents of the iterator and serialize them
378 * to JSON. The function accepts iterators from any source.
379 *
380 * @param iter The iterator to serialize to JSON.
381 * @param desc Serialization parameters.
382 * @return A JSON string with the serialized iterator data, or NULL if failed.
383 */
384FLECS_API
386 ecs_iter_t *iter,
387 const ecs_iter_to_json_desc_t *desc);
388
389/** Serialize iterator into JSON string buffer.
390 * Same as ecs_iter_to_json(), but serializes to an ecs_strbuf_t instance.
391 *
392 * @param iter The iterator to serialize.
393 * @param buf_out The strbuf to append the string to.
394 * @param desc Serialization parameters.
395 * @return Zero if success, non-zero if failed.
396 */
397FLECS_API
399 ecs_iter_t *iter,
400 ecs_strbuf_t *buf_out,
401 const ecs_iter_to_json_desc_t *desc);
402
403/** Used with ecs_world_to_json(). */
405 bool serialize_builtin; /**< Serialize Flecs built-in modules and contents. */
406 bool serialize_modules; /**< Serialize modules and contents. */
408
409/** Serialize world into JSON string.
410 * This operation serializes the contents of the world to JSON. The operation is
411 * equivalent to the following code:
412 *
413 * @code
414 * ecs_query_t *q = ecs_query(world, {
415 * .terms = {{ .id = EcsAny }}
416 * });
417 *
418 * ecs_iter_t it = ecs_query_iter(world, q);
419 * ecs_iter_to_json_desc_t desc = { .serialize_table = true };
420 * ecs_iter_to_json(&it, &desc);
421 * @endcode
422 *
423 * @param world The world to serialize.
424 * @param desc Serialization parameters.
425 * @return A JSON string with the serialized iterator data, or NULL if failed.
426 */
427FLECS_API
429 ecs_world_t *world,
430 const ecs_world_to_json_desc_t *desc);
431
432/** Serialize world into JSON string buffer.
433 * Same as ecs_world_to_json(), but serializes to an ecs_strbuf_t instance.
434 *
435 * @param world The world to serialize.
436 * @param buf_out The strbuf to append the string to.
437 * @param desc Serialization parameters.
438 * @return Zero if success, non-zero if failed.
439 */
440FLECS_API
442 ecs_world_t *world,
443 ecs_strbuf_t *buf_out,
444 const ecs_world_to_json_desc_t *desc);
445
446#ifdef __cplusplus
447}
448#endif
449
450#endif
451
452/** @} */
453
454#endif
FLECS_API const char * ecs_entity_from_json(ecs_world_t *world, ecs_entity_t entity, const char *json, const ecs_from_json_desc_t *desc)
Parse JSON object with multiple component values into an entity.
FLECS_API const char * ecs_world_from_json(ecs_world_t *world, const char *json, const ecs_from_json_desc_t *desc)
Parse JSON object with multiple entities into the world.
FLECS_API int ecs_type_info_to_json_buf(const ecs_world_t *world, ecs_entity_t type, ecs_strbuf_t *buf_out)
Serialize type info into JSON string buffer.
FLECS_API int ecs_iter_to_json_buf(ecs_iter_t *iter, ecs_strbuf_t *buf_out, const ecs_iter_to_json_desc_t *desc)
Serialize iterator into JSON string buffer.
FLECS_API int ecs_ptr_to_json_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf_out)
Serialize value into JSON string buffer.
FLECS_API const char * ecs_world_from_json_file(ecs_world_t *world, const char *filename, const ecs_from_json_desc_t *desc)
Same as ecs_world_from_json(), but loads JSON from a file.
FLECS_API char * ecs_ptr_to_json(const ecs_world_t *world, ecs_entity_t type, const void *data)
Serialize value into JSON string.
FLECS_API char * ecs_type_info_to_json(const ecs_world_t *world, ecs_entity_t type)
Serialize type info to JSON.
FLECS_API char * ecs_entity_to_json(ecs_world_t *world, ecs_entity_t entity, const ecs_entity_to_json_desc_t *desc)
Serialize entity into JSON string.
FLECS_API int ecs_entity_to_json_buf(ecs_world_t *world, ecs_entity_t entity, ecs_strbuf_t *buf_out, const ecs_entity_to_json_desc_t *desc)
Serialize entity into JSON string buffer.
FLECS_API int ecs_world_to_json_buf(ecs_world_t *world, ecs_strbuf_t *buf_out, const ecs_world_to_json_desc_t *desc)
Serialize world into JSON string buffer.
FLECS_API char * ecs_world_to_json(ecs_world_t *world, const ecs_world_to_json_desc_t *desc)
Serialize world into JSON string.
FLECS_API char * ecs_iter_to_json(ecs_iter_t *iter, const ecs_iter_to_json_desc_t *desc)
Serialize iterator into JSON string.
FLECS_API const char * ecs_ptr_from_json(const ecs_world_t *world, ecs_entity_t type, void *ptr, const char *json, const ecs_from_json_desc_t *desc)
Parse JSON string into value.
FLECS_API int ecs_array_to_json_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, int32_t count, ecs_strbuf_t *buf_out)
Serialize array into JSON string buffer.
FLECS_API char * ecs_array_to_json(const ecs_world_t *world, ecs_entity_t type, const void *data, int32_t count)
Serialize array into JSON string.
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
void ecs_poly_t
A poly object.
Definition flecs.h:528
Used with ecs_entity_to_json().
Definition json.h:220
bool serialize_values
Serialize component values.
Definition json.h:225
bool serialize_builtin
Serialize built-in data as components (e.g., "name", "parent").
Definition json.h:226
bool serialize_alerts
Serialize active alerts for the entity.
Definition json.h:228
ecs_entity_t serialize_refs
Serialize references (incoming edges) for a relationship.
Definition json.h:229
bool serialize_matches
Serialize which queries the entity matches with.
Definition json.h:230
bool serialize_type_info
Serialize type info (requires serialize_values).
Definition json.h:227
bool(*) component_filter(const ecs_world_t *, ecs_entity_t)
Callback to determine if a component should be serialized.
Definition json.h:233
bool serialize_inherited
Serialize base components.
Definition json.h:224
bool serialize_full_paths
Serialize full paths for tags, components, and pairs.
Definition json.h:223
bool serialize_entity_id
Serialize entity ID.
Definition json.h:221
bool serialize_doc
Serialize doc attributes.
Definition json.h:222
Used with ecs_ptr_from_json(), ecs_entity_from_json().
Definition json.h:41
const char * name
Name of the expression (used for logging).
Definition json.h:42
const char * expr
Full expression (used for logging).
Definition json.h:43
bool strict
Require components to be registered with reflection data.
Definition json.h:55
void * lookup_ctx
Context for lookup_action.
Definition json.h:51
ecs_entity_t(*) lookup_action(ecs_world_t *, const char *value, void *ctx)
Callback that allows for specifying a custom lookup function.
Definition json.h:47
Iterator.
Definition flecs.h:1183
Used with ecs_iter_to_json().
Definition json.h:301
bool serialize_full_paths
Serialize full paths for tags, components, and pairs.
Definition json.h:306
bool serialize_query_plan
Serialize query plan.
Definition json.h:313
bool serialize_fields
Serialize field data.
Definition json.h:307
bool serialize_doc
Serialize doc attributes.
Definition json.h:305
bool serialize_query_profile
Profile query performance.
Definition json.h:314
bool serialize_query_info
Serialize query terms.
Definition json.h:312
bool serialize_parents_before_children
If the query matches both children and parent, serialize the parent before children.
Definition json.h:319
bool dont_serialize_results
If true, the query will not be evaluated.
Definition json.h:315
bool serialize_field_info
Serialize metadata for fields returned by the query.
Definition json.h:311
bool serialize_builtin
Serialize built-in data as components (e.g., "name", "parent").
Definition json.h:304
bool serialize_table
Serialize entire table vs.
Definition json.h:309
ecs_entity_t serialize_refs
Serialize references (incoming edges) for a relationship.
Definition json.h:317
bool serialize_inherited
Serialize inherited components.
Definition json.h:308
ecs_poly_t * query
Query object (required for serialize_query_[plan|profile]).
Definition json.h:324
bool serialize_alerts
Serialize active alerts for the entity.
Definition json.h:316
bool serialize_values
Serialize component values.
Definition json.h:303
bool serialize_type_info
Serialize type information.
Definition json.h:310
bool serialize_entity_ids
Serialize entity IDs.
Definition json.h:302
bool serialize_matches
Serialize which queries the entity matches with.
Definition json.h:318
bool(*) component_filter(const ecs_world_t *, ecs_entity_t)
Callback to determine if a component should be serialized.
Definition json.h:323
Used with ecs_world_to_json().
Definition json.h:404
bool serialize_builtin
Serialize Flecs built-in modules and contents.
Definition json.h:405
bool serialize_modules
Serialize modules and contents.
Definition json.h:406