Skip to content

Engine → Core

json_conversion.h

JSON conversion driven by type_info.

Structs

File Description
json::json_converter<math::mat4>
json::json_converter<math::quat>
json::json_converter<math::transform>
json::json_converter<math::vec2>
json::json_converter<math::vec3>
json::json_converter<math::vec4>
json::json_converter<string_id>
reflect::field_ref A resolved field inside an object: the data address plus its description. When the path ended in an array index the reference designates one element and kind() is the element kind. path is the normalised path resolve_path() followed ("parts[1].weight"); error messages of write_json/set_from_json/get_as_json use it, falling back to the field name when it is empty.

Functions

Public

reflect::from_json

[[nodiscard]] CONJURE_API result<void> from_json( const type_info &type, void *object, const json::value &in, std::vector<std::string> *unknownFields = nullptr );

Updates object from a JSON object. Missing members keep their current value; unknown members are appended to unknownFields (dotted paths) when it is not null. Stops at the first failing field.

template <typename T> [[nodiscard]] result<void> from_json( T &object, const json::value &in, std::vector<std::string> *unknownFields = nullptr );

from_json() for a CONJURE_REFLECT_STRUCT type.

reflect::get_as_json

[[nodiscard]] CONJURE_API result<json::document> get_as_json(field_ref ref);

The referenced value as a new document.

reflect::resolve_path

[[nodiscard]] CONJURE_API result<field_ref> resolve_path( const type_info &type, void *object, std::string_view path );

Follows a path such as "a.b[2].c" through nested structs and arrays. Indices are plain decimal digits. NOT_FOUND for an unknown field name, OUT_OF_RANGE for an index past the array, INVALID_ARGUMENT for a malformed path (including a signed index such as "[+1]"), an index on a non-array or a '.' into a non-struct. The returned reference carries the normalised path.

reflect::set_from_json

[[nodiscard]] CONJURE_API result<void> set_from_json(field_ref ref, const json::value &in);

Replaces the referenced value from JSON with the same rules as from_json(). Errors name the reference's path.

reflect::to_json

[[nodiscard]] CONJURE_API result<void> to_json(const type_info &type, const void *object, json::writer &out);

Serialises object (an instance of type) as a JSON object into out, fields in declaration order. Fails with INVALID_STATE for a STRUCT/ENUM field without its description and UNSUPPORTED for a custom kind of unsupported size; the writer may then be left mid-object.

template <typename T> [[nodiscard]] result<void> to_json(const T &object, json::writer &out);

to_json() for a CONJURE_REFLECT_STRUCT type. INVALID_STATE when the type has no registered description.

reflect::to_json_document

[[nodiscard]] CONJURE_API result<json::document> to_json_document(const type_info &type, const void *object);

to_json() into a new document.

template <typename T> [[nodiscard]] result<json::document> to_json_document(const T &object);

to_json_document() for a CONJURE_REFLECT_STRUCT type.

reflect::write_json

CONJURE_API void write_json(const math::vec2 &value, json::writer &out);

Writes one math value in the representation documented above.

CONJURE_API void write_json(const math::vec3 &value, json::writer &out);
CONJURE_API void write_json(const math::vec4 &value, json::writer &out);
CONJURE_API void write_json(const math::quat &value, json::writer &out);
CONJURE_API void write_json(const math::mat4 &value, json::writer &out);
CONJURE_API void write_json(const math::transform &value, json::writer &out);
CONJURE_API void write_json(const string_id &value, json::writer &out);
[[nodiscard]] CONJURE_API result<void> write_json(field_ref ref, json::writer &out);

Writes the referenced value (one field, or one array element) into out. Errors name the reference's path ("field 'counts[1]'").