AnCH JSON library is JSON serialization and deserialization library for JSON format.
AnCH JSON is a standalone library.
CMake target anch::json:
See installation.
Serialized fields are declared by the description of each type shared by formats: an anch::Mapping specialization (anch-utils, mapping.hpp , see AnCH utils library documentation ) whose registerObject function registers the fields with their JSON key and their member pointer (or a getter and / or a setter). std::optional , pointers, collections and maps fields are mapped with the mapper of their value type.
anch::json::serialize writes JSON in a stream or returns a string ; anch::json::deserialize reads a stream and raises anch::json::MappingError on invalid JSON (with its position). Mapping options (anch::json::MappingOptions ) control unknown fields, null values and limits (depth, payload size).
With ANCH_UUID and ANCH_DATE options, anch::UUID , anch::date::DateTime and anch::date::Date and anch::date::Time are mapped as strings (RFC 3339 for dates and times).
Output:
Constraints can be given when a field is registered. anch::val::required checks on deserialization that the field is present (error code MISSING_FIELD , every missing field is listed in the message) and that its value is not null , unless the field can hold null ( std::optional , std::shared_ptr , std::unique_ptr or pointer ; error code CONSTRAINT_VIOLATION ). Fields which are not required are not checked: a null value leaves them unchanged.
anch::json::MappingError::getPath gives the JSON Pointer (RFC 6901) of the value in error, from the deserialized root value ; what() starts with this path.
Output:
Value constraints of anch-val (see AnCH validation library documentation , header val/constraints.hpp ) are given at registration too. They are checked after the field value has been read and raise anch::json::MappingError with CONSTRAINT_VIOLATION error code. null and absent values are not checked ( anch::val::required checks presence). Constraints are checked at compile time against the field type: minLength on an integer field does not compile.
Output:
Constraints ( anch::val::required and value constraints) are checked according to mapping options:
deserialize_check_constraints (default true ): after values have been read ;serialize_check_constraints (default false ): before values are written. The error has no input excerpt (message is "<path>: <constraint message>" ) and the output contains the JSON written before the error.Required fields are always written, even when empty: null for empty std::optional and pointers, [] and {} for empty containers, whatever serialize_null and serialize_empty_col options are (the document would be refused by the other side otherwise).
Server side checks what it receives and trusts what it builds (default options). Client side checks what it sends and trusts server responses:
anch::val::validate (anch-val, val/validation.hpp , see AnCH validation library documentation ) checks an object without serializing it: constraints of its fields and of its nested values, every violation is returned with its path. Mapping options are not used.
Output: