JSON namespace. More...
Classes | |
| class | DepthGuard |
| Nesting depth guard. More... | |
| class | Factory |
| JSON mapper factory. More... | |
| class | GenericMapper |
| Generic mapper. More... | |
| struct | JsonFormat |
| JSON format: serialization and deserialization of values with options. More... | |
| class | JSONItem |
| class | JSONMapper |
| JSON mapper with mapping options. More... | |
| class | MappingError |
| Mapping error. More... | |
| struct | MappingOptions |
| JSON mapping options. More... | |
| class | ObjectMapper |
| JSON complex object mapper. More... | |
| class | PrimitiveMapper |
| JSON primitive types mapper. More... | |
| class | Reader |
| JSON reader based on events. More... | |
| class | ReaderContext |
| JSON reader context. More... | |
| class | WriterContext |
| JSON writer context. More... | |
Concepts | |
| concept | Mappable |
| Type which the JSON format maps. | |
Typedefs | |
| using | AddItem = std::function<bool(anch::json::ReaderContext&)> |
| using | PushItem = std::function<void(const std::string&, anch::json::ReaderContext&)> |
| using | jint = int64_t |
| using | juint = uint64_t |
| using | jfloat = long double |
| template<typename T> | |
| using | SerializeFn = std::function<void(const T&, std::ostream&, const anch::json::MappingOptions&)> |
| template<typename T> | |
| using | DeserializeFn = std::function<bool(T&, anch::json::ReaderContext&)> |
Enumerations | |
| enum class | ErrorCode { INVALID_FORMAT = 0 , UNEXPECTED_FIELD , POTENTIAL_OVERFLOW , MISSING_FIELD , CONSTRAINT_VIOLATION , PAYLOAD_TOO_LARGE , UNKNOWN = 9999 } |
| enum class | EventType { VNULL = 0 , TRUE , FALSE , FIELD , NUMBER , STRING , BEGIN_OBJECT , END_OBJECT , BEGIN_ARRAY , END_ARRAY } |
Functions | |
| template<typename T> | |
| constexpr bool | isPrimitive () |
| Check if type is a primitive JSON type. | |
| template<typename T> | |
| void | serialize (const T &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<template< typename > typename C, typename T> | |
| void | serialize (const C< T > &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| void | serialize (anch::Flux< T > &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| void | serialize (const std::map< std::string, T > &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| std::string | serialize (const T &value, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<template< typename > typename C, typename T> | |
| std::string | serialize (const C< T > &value, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| std::string | serialize (const std::map< std::string, T > &value, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| void | deserialize (T &value, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| T | deserialize (std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<template< typename > typename C, typename T> | |
| void | deserialize (C< T > &values, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| void | deserialize (anch::Flux< T > &values, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| template<typename T> | |
| void | deserialize (std::map< std::string, T > &values, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
| void | lexTrue (anch::json::ReaderContext &context) |
| void | lexFalse (anch::json::ReaderContext &context) |
| bool | lexBoolean (bool &value, anch::json::ReaderContext &context) |
| void | parseDecimal (anch::json::ReaderContext &context) |
| bool | lexInteger (anch::json::jint &value, anch::json::ReaderContext &context) |
| bool | lexUInteger (anch::json::juint &value, anch::json::ReaderContext &context) |
| void | parseNumber (anch::json::ReaderContext &context) |
| bool | lexNumber (double &value, anch::json::ReaderContext &context) |
| void | lexStringContent (std::string &value, anch::json::ReaderContext &context) |
| void | lexStringContent (anch::json::ReaderContext &context) |
| bool | lexString (std::string &value, anch::json::ReaderContext &context) |
| bool | lexField (std::string &field, anch::json::ReaderContext &context) |
| char | discardFail (anch::json::ReaderContext &context) |
| void | lexEnd (anch::json::ReaderContext &context) |
| char | lexDiscard (anch::json::ReaderContext &context) |
| void | lexNull (anch::json::ReaderContext &context) |
| bool | objectHasValueLex (anch::json::ReaderContext &context) |
| template<typename F> | |
| void | lexObject (F &&onField, anch::json::ReaderContext &context) |
| template<typename F> | |
| bool | lexArray (F &&addFunc, anch::json::ReaderContext &context) |
| template<typename F> | |
| bool | lexMap (F &&pushFunc, anch::json::ReaderContext &context) |
| void | lexUnknown (anch::json::ReaderContext &context) |
| void | lexUnknownFail (anch::json::ReaderContext &context) |
| bool | isNumberChar (char c) |
| bool | isJsonNumber (std::string_view chars) |
| template<typename P> | |
| std::string_view | scanNumber (anch::json::ReaderContext &context, P isNumberChar) |
| template<> | |
| constexpr bool | isPrimitive< anch::oas::AdditionalProperties > () |
| template<> | |
| constexpr bool | isPrimitive< anch::oas::Scalar > () |
Variables | |
| const char | STRING_DELIMITER = '"' |
| const char | FIELD_SEPARATOR = ',' |
| const char | FIELD_VALUE_SEPARATOR = ':' |
| const char | OBJECT_BEGIN = '{' |
| const char | OBJECT_END = '}' |
| const char | ARRAY_BEGIN = '[' |
| const char | ARRAY_END =']' |
| const std::optional< std::string > | EMPTY_FIELD |
| MappingOptions | DEFAULT_MAPPING_OPTIONS |
JSON namespace.
Provides serialization and deserialization functions for JSON format.
| using anch::json::AddItem = std::function<bool(anch::json::ReaderContext&)> |
Parse and add item to array function (return true when ']' has been found, false otherwise)
| using anch::json::DeserializeFn = std::function<bool(T&, anch::json::ReaderContext&)> |
Deserialization function definition
| using anch::json::jfloat = long double |
Maximum float precision declaration
| using anch::json::jint = int64_t |
Maximum signed integer precision declaration
| using anch::json::juint = uint64_t |
Maximum unsigned integer precision declaration
| using anch::json::PushItem = std::function<void(const std::string&, anch::json::ReaderContext&)> |
Parse and insert item to map
| using anch::json::SerializeFn = std::function<void(const T&, std::ostream&, const anch::json::MappingOptions&)> |
Serialization function definition
|
strong |
JSON mapping error code
|
strong |
Reader found element type
| void anch::json::deserialize | ( | anch::Flux< T > & | values, |
| std::istream & | input, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Deserialize JSON array into objects' flux: each object is pushed to the flux consumer, then flux is finalized.
Consumer (and error handler when needed) have to be set before this method call.
| T | the object type |
| values | the flux to push values in |
| input | the input stream to parse |
| options | the options to use |
| void anch::json::deserialize | ( | C< T > & | values, |
| std::istream & | input, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Deserialize objects (it can also work with std::optional, std::shared_ptr, ...)
| T | the object's type |
| C | the container's type |
| values | the values to fill |
| input | the input stream to parse |
| options | the options to use |
| T anch::json::deserialize | ( | std::istream & | input, |
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Deserialize object
| T | the object type |
| input | the input stream to parse |
| options | the options to use |
| void anch::json::deserialize | ( | std::map< std::string, T > & | values, |
| std::istream & | input, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Deserialize objects
| T | the object type |
| values | the values to fill |
| input | the input stream to parse |
| options | the options to use |
| void anch::json::deserialize | ( | T & | value, |
| std::istream & | input, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Deserialize object
| T | the object type |
| value | the value to fill |
| input | the input stream to parse |
| options | the options to use |
| char anch::json::discardFail | ( | anch::json::ReaderContext & | context | ) |
Fail on any discardable character
| context | the parser context |
| anch::json::MappingError | discardable charater has been found |
| bool anch::json::isJsonNumber | ( | std::string_view | chars | ) |
Check that number characters are a valid JSON number (RFC 8259): [-] (0|[1-9][0-9]*) [.[0-9]+] [(e|E)[+|-][0-9]+]
| chars | the number characters |
true when valid | bool anch::json::isNumberChar | ( | char | c | ) |
Check if character can be part of a JSON number (digit, sign, '.', exponent)
| c | the character to check |
true when character can be part of a number
|
constexpr |
Check if type is a primitive JSON type.
When not specialzed, the function always return false .
Every C basic types are defined as primitive. std::string and std::string_view are also defined as primitive.
You can declare any type as primitive with template function specialization.
| T | the type to check |
true if type is a JSON primitive type, false otherwise
|
constexpr |
anch::oas::AdditionalProperties is deserialized with a dedicated mapper (boolean or schema)
true
|
constexpr |
anch::oas::Scalar is deserialized with a dedicated mapper (any JSON value)
true | bool anch::json::lexArray | ( | F && | addFunc, |
| anch::json::ReaderContext & | context ) |
Parse array
| F | the item parser type |
| addFunc | parse and add elem to array function (callable as bool(anch::json::ReaderContext&)) |
| context | the context |
false when null has been found, true otherwise| anch::json::MappingError | parsing error (the item index is added to the error path) |
| bool anch::json::lexBoolean | ( | bool & | value, |
| anch::json::ReaderContext & | context ) |
Boolean value lexer.
Possible values are: null, true and false
| value | the boolean value to store in (not set when null has been found) |
| context | the parser context |
false when null , true otherwise| anch::json::MappingError | parsing error |
| char anch::json::lexDiscard | ( | anch::json::ReaderContext & | context | ) |
Consume stream until unescapable character
| context | the parser context |
| anch::json::MappingError | maximum number of discardable charater has been reached |
| void anch::json::lexEnd | ( | anch::json::ReaderContext & | context | ) |
Check that only discardable characters (according to deserialize_max_discard_char option) follow the root value until the end of input stream
| context | the parser context |
| anch::json::MappingError | data has been found after the root value |
| void anch::json::lexFalse | ( | anch::json::ReaderContext & | context | ) |
'false' value lexer.
| context | the parser context |
| anch::json::MappingError | parsing error |
| bool anch::json::lexField | ( | std::string & | field, |
| anch::json::ReaderContext & | context ) |
Field lexer function.
This lexer will look for " character to begin and end with ".
If begin character is not found, field will be empty.
If " end character is not found (according to options and stream), it will raised exception
| field | the field to fill when found |
| context | the parser context |
true when found, false otherwise| anch::json::MappingError | parsing error |
| bool anch::json::lexInteger | ( | anch::json::jint & | value, |
| anch::json::ReaderContext & | context ) |
Signed integer value lexer.
Possible values are: null and signed integer
| value | the signed value to store in (not set when null has been found) |
| context | the parser context |
false when null , true otherwise| anch::json::MappingError | parsing error |
| bool anch::json::lexMap | ( | F && | pushFunc, |
| anch::json::ReaderContext & | context ) |
Parse map
| F | the item parser type |
| pushFunc | parse and add elem to map function (callable as void(const std::string&, anch::json::ReaderContext&)) |
| context | the context |
false when null has been found, true otherwise| anch::json::MappingError | parsing error (the key is added to the error path) |
| void anch::json::lexNull | ( | anch::json::ReaderContext & | context | ) |
'null' value lexer.
| context | the parser context |
| anch::json::MappingError | parsing error |
| bool anch::json::lexNumber | ( | double & | value, |
| anch::json::ReaderContext & | context ) |
Floating integer (double) value lexer.
Possible values are: null and floating integer value (can parse exponential representation).
| value | the value to write in (not set when null has been found) |
| context | the parser context |
false when null , true otherwise| anch::json::MappingError | parsing error |
| void anch::json::lexObject | ( | F && | onField, |
| anch::json::ReaderContext & | context ) |
Object lexer function.
The opening '{' has to be already consumed (see objectHasValueLex).
onField is called for each found field with the field name and the context positioned just after ':'. It has to consume the field value.
| F | the field handler type (callable as void(const std::string&, anch::json::ReaderContext&)) |
| onField | the field handler |
| context | the parser context |
| anch::json::MappingError | parsing error |
| bool anch::json::lexString | ( | std::string & | value, |
| anch::json::ReaderContext & | context ) |
String lexer function.
This lexer will look for " character to begin and end with ".
If begin character is not found, field will be empty.
If " end character is not found (according to options and stream), it will raised exception
| value | the string value to fill when found |
| context | the parser context |
false when null has been found, true otherwise| anch::json::MappingError | parsing error |
| void anch::json::lexStringContent | ( | anch::json::ReaderContext & | context | ) |
Lex string content (between the '"').
The result will be stored in the context buffer (see lexStringContent(std::string&, anch::json::ReaderContext&) ).
| context | the JSON context |
| anch::json::MappingError | invalid escape sequence or end of data before end of string |
| void anch::json::lexStringContent | ( | std::string & | value, |
| anch::json::ReaderContext & | context ) |
Lex string content (between the '"').
Escape sequences are decoded (\uXXXX sequences, including UTF-16 surrogate pairs, are converted to UTF-8).
The opening '"' has to be already consumed.
| value | the string to write in (cleared before) |
| context | the JSON context |
| anch::json::MappingError | invalid escape sequence or end of data before end of string |
| void anch::json::lexTrue | ( | anch::json::ReaderContext & | context | ) |
'true' value lexer.
| context | the parser context |
| anch::json::MappingError | parsing error |
| bool anch::json::lexUInteger | ( | anch::json::juint & | value, |
| anch::json::ReaderContext & | context ) |
Unsigned integer value lexer.
Possible values are: null and unsigned integer (min value is always 0)
| value | the unsigned value to store in (not set when null has been found) |
| context | the parser context |
false when null , true otherwise| anch::json::MappingError | parsing error |
| void anch::json::lexUnknown | ( | anch::json::ReaderContext & | context | ) |
Parse unknown field value
| context | the context |
| anch::json::MappingError | parsing error |
| void anch::json::lexUnknownFail | ( | anch::json::ReaderContext & | context | ) |
Raise anch::json::MappingError
| context | the context |
| anch::json::MappingError | parsing error |
| bool anch::json::objectHasValueLex | ( | anch::json::ReaderContext & | context | ) |
Check if object has value (start with '{') or is null
| context | the parser context |
| anch::json::MappingError | parsing error |
| void anch::json::parseDecimal | ( | anch::json::ReaderContext & | context | ) |
Parse decimale digits and store them to context value's buffer
| context | the context |
| void anch::json::parseNumber | ( | anch::json::ReaderContext & | context | ) |
Parse float.
End parsing at first unrecognized character.
| context | the parser context |
| std::string_view anch::json::scanNumber | ( | anch::json::ReaderContext & | context, |
| P | isNumberChar ) |
Scan number characters.
The first number character has already been read (it is at buffer[offset-1] ). When the number is entirely in the reader buffer, the result points directly into it (no copy). Otherwise, characters are copied in context's primitive buffer while the buffer is refilled.
The context offset is set on the first character which is not a number character.
| P | the number character predicate type |
| context | the reader context |
| isNumberChar | the number character predicate |
| void anch::json::serialize | ( | anch::Flux< T > & | value, |
| std::ostream & | out, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize object's flux.
Error handler has to be set before this method call when needed. Consumer and finalize functions are set in this method.
This method will lock the processing until anch::Flux::finalize method has not be called (or error happened and throw itself).
This function is implemented in header imkplementation instead uses GenericMapper implementation.
| T | the object type |
| value | the objects to serialize |
| out | the output stream to write in |
| options | the options to use |
| std::string anch::json::serialize | ( | const C< T > & | value, |
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize objects (it can also work with std::optional, std::shared_ptr, ...)
| T | the object type |
| C | the container's type |
| value | the object to serialize |
| options | the options to use |
std::string | void anch::json::serialize | ( | const C< T > & | value, |
| std::ostream & | out, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize objects (it can also work with std::optional, std::shared_ptr, ...)
| T | the object type |
| C | the container's type |
| value | the objects to serialize |
| out | the output stream to write in |
| options | the options to use |
| std::string anch::json::serialize | ( | const std::map< std::string, T > & | value, |
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize objects
| T | the object type |
| value | the object to serialize |
| options | the options to use |
std::string | void anch::json::serialize | ( | const std::map< std::string, T > & | value, |
| std::ostream & | out, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize objects
| T | the object type |
| value | the objects to serialize |
| out | the output stream to write in |
| options | the options to use |
| std::string anch::json::serialize | ( | const T & | value, |
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize object
| T | the object type |
| value | the object to serialize |
| options | the options to use |
std::string | void anch::json::serialize | ( | const T & | value, |
| std::ostream & | out, | ||
| const anch::json::MappingOptions & | options = anch::json::DEFAULT_MAPPING_OPTIONS ) |
Serialize object
| T | the object type |
| value | the object to serialize |
| out | the output stream to write in |
| options | the options to use |
| const char anch::json::ARRAY_BEGIN = '[' |
JSON start array ('[')
| const char anch::json::ARRAY_END =']' |
JSON start array (']')
|
extern |
Default mapping options. It is not a constant so it can be changed in your application initialization.
|
extern |
No field name constant value
| const char anch::json::FIELD_SEPARATOR = ',' |
JSON fields delimiter (',')
| const char anch::json::FIELD_VALUE_SEPARATOR = ':' |
JSON fiel/value delimiter (':')
| const char anch::json::OBJECT_BEGIN = '{' |
JSON start object ('{')
| const char anch::json::OBJECT_END = '}' |
JSON end object ('}')
| const char anch::json::STRING_DELIMITER = '"' |
JSON caracters string delimiter ('"')