JSON namespace. More...
Classes | |
class | Factory |
JSON mapper factory. 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 mapper context. More... | |
Typedefs | |
using | LexFunc = std::function<bool(anch::json::ReaderContext&)> |
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 , 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 | registerObject (anch::json::ObjectMapper< T > &mapper) |
template<typename T> | |
void | serialize (const T &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
void | serialize (const std::vector< T > &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
void | serialize (const std::list< T > &value, std::ostream &out, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
void | serialize (const std::set< 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<typename T> | |
std::string | serialize (const std::vector< T > &value, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
std::string | serialize (const std::list< T > &value, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
std::string | serialize (const std::set< 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<typename T> | |
void | deserialize (std::vector< T > &values, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
void | deserialize (std::list< T > &values, std::istream &input, const anch::json::MappingOptions &options=anch::json::DEFAULT_MAPPING_OPTIONS) |
template<typename T> | |
void | deserialize (std::set< 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 (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) |
char | lexDiscard (anch::json::ReaderContext &context) |
void | lexNull (anch::json::ReaderContext &context) |
bool | objectHasValueLex (anch::json::ReaderContext &context) |
void | lexObject (const std::map< std::string, anch::json::LexFunc > &readers, std::set< std::string > &fields, anch::json::ReaderContext &context) |
void | lexArray (anch::json::AddItem addFunc, anch::json::ReaderContext &context) |
void | lexMap (anch::json::PushItem pushFunc, anch::json::ReaderContext &context) |
void | lexUnknown (anch::json::ReaderContext &context) |
void | lexUnknownFail (anch::json::ReaderContext &context) |
void | serializeFieldName (std::ostream &out, const std::optional< std::string > &field) |
void | serializeFieldName (std::ostream &out, const std::string &field) |
template<typename T> | |
bool | serialize (const T &value, std::ostream &out, anch::json::SerializeFn< T > serializeFunc, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
bool | serializeNull (std::ostream &out, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
template<typename T> | |
bool | serialize (const T *const value, std::ostream &out, anch::json::SerializeFn< T > serializeFunc, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
template<typename T> | |
bool | serialize (const std::optional< T > &value, std::ostream &out, anch::json::SerializeFn< T > serializeFunc, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
template<typename T, typename A> | |
void | serializeArray (const A &array, std::ostream &out, anch::json::DeserializeFn< T > serializeFunc, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
template<typename T> | |
void | serializeMap (const std::map< std::string, T > &map, std::ostream &out, anch::json::SerializeFn< T > serializeFunc, const anch::json::MappingOptions &options, const std::optional< std::string > &field) |
template<typename T> | |
void | deserialize (T &value, anch::json::ReaderContext &context, anch::json::DeserializeFn< T > deserializeFunc) |
template<typename T> | |
void | deserialize (T *value, anch::json::ReaderContext &context, anch::json::DeserializeFn< T > deserializeFunc) |
template<typename T> | |
void | deserialize (std::optional< T > &value, anch::json::ReaderContext &context, anch::json::DeserializeFn< T > deserializeFunc) |
template<typename T> | |
bool | addToVector (std::vector< T > &value, anch::json::DeserializeFn< T > deser, anch::json::ReaderContext &context) |
template<typename T> | |
bool | addToList (std::list< T > &value, anch::json::DeserializeFn< T > deser, anch::json::ReaderContext &context) |
template<typename T> | |
bool | addToSet (std::set< T > &value, anch::json::DeserializeFn< T > deser, anch::json::ReaderContext &context) |
template<typename T> | |
void | deserializeMap (std::function< void(const std::pair< std::string, T > &)> pushFunc, anch::json::ReaderContext &context, anch::json::DeserializeFn< T > deserializeFunc) |
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::LexFunc = std::function<bool(anch::json::ReaderContext&)> |
Object lexer function
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 |
|
strong |
Reader found element type
bool anch::json::addToList | ( | std::list< T > & | value, |
anch::json::DeserializeFn< T > | deser, | ||
anch::json::ReaderContext & | context ) |
Deserialize item and add it to list if not null
value | the list to add in |
deser | the deserialization function |
context | the reader context |
bool anch::json::addToSet | ( | std::set< T > & | value, |
anch::json::DeserializeFn< T > | deser, | ||
anch::json::ReaderContext & | context ) |
Deserialize item and add it to set if not null
value | the set to add in |
deser | the deserialization function |
context | the reader context |
bool anch::json::addToVector | ( | std::vector< T > & | value, |
anch::json::DeserializeFn< T > | deser, | ||
anch::json::ReaderContext & | context ) |
Deserialize item and add it to vector if not null
value | the vector to add in |
deser | the deserialization function |
context | the reader context |
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::list< 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 | ( | std::optional< T > & | value, |
anch::json::ReaderContext & | context, | ||
anch::json::DeserializeFn< T > | deserializeFunc ) |
Deserialize JSON value
value | the value to fill |
context | the mapping context |
deserializeFunc | the value deserialization function |
void anch::json::deserialize | ( | std::set< 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 | ( | std::vector< 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, |
anch::json::ReaderContext & | context, | ||
anch::json::DeserializeFn< T > | deserializeFunc ) |
Deserialize JSON value
value | the value to fill |
context | the mapping context |
deserializeFunc | the value deserialization function |
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 |
void anch::json::deserialize | ( | T * | value, |
anch::json::ReaderContext & | context, | ||
anch::json::DeserializeFn< T > | deserializeFunc ) |
Deserialize JSON value
value | the value to fill |
context | the mapping context |
deserializeFunc | the value deserialization function |
void anch::json::deserializeMap | ( | std::function< void(const std::pair< std::string, T > &)> | pushFunc, |
anch::json::ReaderContext & | context, | ||
anch::json::DeserializeFn< T > | deserializeFunc ) |
JSON array deserialization generic implementation
T | the object type |
pushFunc | the push function according to container type |
context | the mapping context |
deserializeFunc | the value deserialization function |
JSON map deserialization generic implementation
T | the object type |
context | the mapping context |
pushFunc | the push function according to container type |
deserializeFunc | the value deserialization function |
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 |
|
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 void anch::json::lexArray | ( | anch::json::AddItem | addFunc, |
anch::json::ReaderContext & | context ) |
Parse array
addFunc | parse and add elem to array function |
context | the context |
anch::json::MappingError | parsing error |
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
otherwiseanch::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::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
otherwiseanch::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
otherwiseanch::json::MappingError | parsing error |
void anch::json::lexMap | ( | anch::json::PushItem | pushFunc, |
anch::json::ReaderContext & | context ) |
Parse map
pushFunc | parse and add elem to map function |
context | the context |
anch::json::MappingError | parsing error |
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
otherwiseanch::json::MappingError | parsing error |
void anch::json::lexObject | ( | const std::map< std::string, anch::json::LexFunc > & | readers, |
std::set< std::string > & | fields, | ||
anch::json::ReaderContext & | context ) |
Object lexer function.
readers | the declared fields and their reader function |
fields | the found fields to register in |
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
otherwiseanch::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.
context | the JSON context |
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
otherwiseanch::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 |
void anch::json::registerObject | ( | anch::json::ObjectMapper< T > & | mapper | ) |
Fields registry function
This function has to be specialized for each class/structure which has to be serialized/deserialized.
T | the object type |
mapper | the ObjectMapper to use |
std::string anch::json::serialize | ( | const std::list< 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::list< 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 |
bool anch::json::serialize | ( | const std::optional< T > & | value, |
std::ostream & | out, | ||
anch::json::SerializeFn< T > | serializeFunc, | ||
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
Serialize JSON optional value
the | object type |
value | the value to serialize |
out | the output stream to write in |
serializeFunc | the serialization function |
options | the options to use |
field | the optional field name |
std::string anch::json::serialize | ( | const std::set< 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::set< 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 std::vector< 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::vector< 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
bool anch::json::serialize | ( | const T & | value, |
std::ostream & | out, | ||
anch::json::SerializeFn< T > | serializeFunc, | ||
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
Serialize JSON value
the | object type |
value | the value to serialize |
out | the output stream to write in |
serializeFunc | the serialization function |
options | the options to use |
field | the optional field name |
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 |
bool anch::json::serialize | ( | const T *const | value, |
std::ostream & | out, | ||
anch::json::SerializeFn< T > | serializeFunc, | ||
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
Serialize JSON pointer value
the | object type |
value | the value to serialize |
out | the output stream to write in |
serializeFunc | the serialization function |
options | the options to use |
field | the optional field name |
void anch::json::serializeArray | ( | const A & | array, |
std::ostream & | out, | ||
anch::json::DeserializeFn< T > | serializeFunc, | ||
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
JSON array deserialization generic implementation
A | the container type |
T | the object type |
array | the array to serialize |
out | the output stream to write in |
serializeFunc | the serialization function |
options | the options to use |
field | the optional field name |
void anch::json::serializeFieldName | ( | std::ostream & | out, |
const std::optional< std::string > & | field ) |
Serialize field name according to value
out | the ouput stream to write in |
field | the optional field name to serialize (or not) |
void anch::json::serializeFieldName | ( | std::ostream & | out, |
const std::string & | field ) |
Serialize field name
out | the ouput stream to write in |
field | the field name to serialize |
void anch::json::serializeMap | ( | const std::map< std::string, T > & | map, |
std::ostream & | out, | ||
anch::json::SerializeFn< T > | serializeFunc, | ||
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
JSON map deserialization generic implementation
T | the object type |
map | the map to serialize |
out | the output stream to write in |
serializeFunc | the serialization function |
options | the options to use |
field | the optional field name |
bool anch::json::serializeNull | ( | std::ostream & | out, |
const anch::json::MappingOptions & | options, | ||
const std::optional< std::string > & | field ) |
Serialize unset value according to options
out | the output stream to write in |
options | the options to use |
field | the optional field name |
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 ('"')