JSON namespace. More...
Classes | |
class | Factory |
JSON mapper factory. More... | |
class | GenericMapper |
Generic mapper. 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... |
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<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 (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 (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) |
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
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 |
|
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 |
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 ('"')