AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::json Namespace Reference

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

Detailed Description

JSON namespace.

Provides serialization and deserialization functions for JSON format.

Typedef Documentation

◆ AddItem

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)

◆ DeserializeFn

template<typename T>
using anch::json::DeserializeFn = std::function<bool(T&, anch::json::ReaderContext&)>

Deserialization function definition

◆ jfloat

using anch::json::jfloat = long double

Maximum float precision declaration

◆ jint

using anch::json::jint = int64_t

Maximum signed integer precision declaration

◆ juint

using anch::json::juint = uint64_t

Maximum unsigned integer precision declaration

◆ PushItem

using anch::json::PushItem = std::function<void(const std::string&, anch::json::ReaderContext&)>

Parse and insert item to map

◆ SerializeFn

template<typename T>
using anch::json::SerializeFn = std::function<void(const T&, std::ostream&, const anch::json::MappingOptions&)>

Serialization function definition

Enumeration Type Documentation

◆ ErrorCode

enum class anch::json::ErrorCode
strong

JSON mapping error code

Author
Vincent Lachenal
Since
0.1
Enumerator
INVALID_FORMAT 

Invalid format

UNEXPECTED_FIELD 

Found unexpected field (and parsing options disallow it)

POTENTIAL_OVERFLOW 

Found too many space characters or charcters in field name (according to parsing option)

MISSING_FIELD 

Required field is missing

Since
0.2
CONSTRAINT_VIOLATION 

Value does not respect a field constraint (null value in a required field which can not be null , ...)

Since
0.2
PAYLOAD_TOO_LARGE 

Input stream is larger than max_payload option

Since
0.2
UNKNOWN 

Unexpected error code

◆ EventType

enum class anch::json::EventType
strong

Reader found element type

Author
Vincent Lachenal
Since
0.1
Enumerator
VNULL 

'null' value (no value)

TRUE 

Boolean 'true' value (no value)

FALSE 

Boolean 'false' value (no value)

FIELD 

Field value (value stored as std::string)

NUMBER 

Number value (value stored as double)

STRING 

String value (value as stored std::string)

BEGIN_OBJECT 

Begin object (no value)

END_OBJECT 

End object (no value)

BEGIN_ARRAY 

Begin array (no value)

END_ARRAY 

End array (no value)

Function Documentation

◆ deserialize() [1/5]

template<typename T>
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.

Template Parameters
Tthe object type
Parameters
valuesthe flux to push values in
inputthe input stream to parse
optionsthe options to use
Since
0.2
Here is the call graph for this function:

◆ deserialize() [2/5]

template<template< typename > typename C, typename T>
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, ...)

Template Parameters
Tthe object's type
Cthe container's type
Parameters
valuesthe values to fill
inputthe input stream to parse
optionsthe options to use
Here is the call graph for this function:

◆ deserialize() [3/5]

template<typename T>
T anch::json::deserialize ( std::istream & input,
const anch::json::MappingOptions & options = anch::json::DEFAULT_MAPPING_OPTIONS )

Deserialize object

Template Parameters
Tthe object type
Parameters
inputthe input stream to parse
optionsthe options to use
Returns
the deserialized object by copy
Here is the call graph for this function:

◆ deserialize() [4/5]

template<typename T>
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

Template Parameters
Tthe object type
Parameters
valuesthe values to fill
inputthe input stream to parse
optionsthe options to use
Here is the call graph for this function:

◆ deserialize() [5/5]

template<typename T>
void anch::json::deserialize ( T & value,
std::istream & input,
const anch::json::MappingOptions & options = anch::json::DEFAULT_MAPPING_OPTIONS )

Deserialize object

Template Parameters
Tthe object type
Parameters
valuethe value to fill
inputthe input stream to parse
optionsthe options to use
Here is the call graph for this function:

◆ discardFail()

char anch::json::discardFail ( anch::json::ReaderContext & context)

Fail on any discardable character

Parameters
contextthe parser context
Returns
the not discardable character
Exceptions
anch::json::MappingErrordiscardable charater has been found
Here is the call graph for this function:

◆ isJsonNumber()

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]+]

Parameters
charsthe number characters
Returns
true when valid
Here is the call graph for this function:

◆ isNumberChar()

bool anch::json::isNumberChar ( char c)

Check if character can be part of a JSON number (digit, sign, '.', exponent)

Parameters
cthe character to check
Returns
true when character can be part of a number
Here is the call graph for this function:

◆ isPrimitive()

template<typename T>
bool anch::json::isPrimitive ( )
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.

Template Parameters
Tthe type to check
Returns
true if type is a JSON primitive type, false otherwise
Here is the call graph for this function:

◆ isPrimitive< anch::oas::AdditionalProperties >()

template<>
bool anch::json::isPrimitive< anch::oas::AdditionalProperties > ( )
constexpr

anch::oas::AdditionalProperties is deserialized with a dedicated mapper (boolean or schema)

Returns
true

◆ isPrimitive< anch::oas::Scalar >()

template<>
bool anch::json::isPrimitive< anch::oas::Scalar > ( )
constexpr

anch::oas::Scalar is deserialized with a dedicated mapper (any JSON value)

Returns
true

◆ lexArray()

template<typename F>
bool anch::json::lexArray ( F && addFunc,
anch::json::ReaderContext & context )

Parse array

Template Parameters
Fthe item parser type
Parameters
addFuncparse and add elem to array function (callable as bool(anch::json::ReaderContext&))
contextthe context
Returns
false when null has been found, true otherwise
Exceptions
anch::json::MappingErrorparsing error (the item index is added to the error path)
Here is the call graph for this function:

◆ lexBoolean()

bool anch::json::lexBoolean ( bool & value,
anch::json::ReaderContext & context )

Boolean value lexer.
Possible values are: null, true and false

Parameters
valuethe boolean value to store in (not set when null has been found)
contextthe parser context
Returns
false when null , true otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexDiscard()

char anch::json::lexDiscard ( anch::json::ReaderContext & context)

Consume stream until unescapable character

Parameters
contextthe parser context
Returns
the not discardable character
Exceptions
anch::json::MappingErrormaximum number of discardable charater has been reached
Here is the call graph for this function:

◆ lexEnd()

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

Parameters
contextthe parser context
Exceptions
anch::json::MappingErrordata has been found after the root value
Since
0.2
Here is the call graph for this function:

◆ lexFalse()

void anch::json::lexFalse ( anch::json::ReaderContext & context)

'false' value lexer.

Parameters
contextthe parser context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexField()

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

Parameters
fieldthe field to fill when found
contextthe parser context
Returns
true when found, false otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexInteger()

bool anch::json::lexInteger ( anch::json::jint & value,
anch::json::ReaderContext & context )

Signed integer value lexer.
Possible values are: null and signed integer

Parameters
valuethe signed value to store in (not set when null has been found)
contextthe parser context
Returns
false when null , true otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexMap()

template<typename F>
bool anch::json::lexMap ( F && pushFunc,
anch::json::ReaderContext & context )

Parse map

Template Parameters
Fthe item parser type
Parameters
pushFuncparse and add elem to map function (callable as void(const std::string&, anch::json::ReaderContext&))
contextthe context
Returns
false when null has been found, true otherwise
Exceptions
anch::json::MappingErrorparsing error (the key is added to the error path)
Here is the call graph for this function:

◆ lexNull()

void anch::json::lexNull ( anch::json::ReaderContext & context)

'null' value lexer.

Parameters
contextthe parser context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexNumber()

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).

Parameters
valuethe value to write in (not set when null has been found)
contextthe parser context
Returns
false when null , true otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexObject()

template<typename F>
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.

Template Parameters
Fthe field handler type (callable as void(const std::string&, anch::json::ReaderContext&))
Parameters
onFieldthe field handler
contextthe parser context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexString()

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

Parameters
valuethe string value to fill when found
contextthe parser context
Returns
false when null has been found, true otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexStringContent() [1/2]

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&) ).

Parameters
contextthe JSON context
Exceptions
anch::json::MappingErrorinvalid escape sequence or end of data before end of string
Here is the call graph for this function:

◆ lexStringContent() [2/2]

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.

Parameters
valuethe string to write in (cleared before)
contextthe JSON context
Exceptions
anch::json::MappingErrorinvalid escape sequence or end of data before end of string
Here is the call graph for this function:

◆ lexTrue()

void anch::json::lexTrue ( anch::json::ReaderContext & context)

'true' value lexer.

Parameters
contextthe parser context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexUInteger()

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)

Parameters
valuethe unsigned value to store in (not set when null has been found)
contextthe parser context
Returns
false when null , true otherwise
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexUnknown()

void anch::json::lexUnknown ( anch::json::ReaderContext & context)

Parse unknown field value

Parameters
contextthe context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ lexUnknownFail()

void anch::json::lexUnknownFail ( anch::json::ReaderContext & context)

Raise anch::json::MappingError

Parameters
contextthe context
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ objectHasValueLex()

bool anch::json::objectHasValueLex ( anch::json::ReaderContext & context)

Check if object has value (start with '{') or is null

Parameters
contextthe parser context
Returns
object has value
Exceptions
anch::json::MappingErrorparsing error
Here is the call graph for this function:

◆ parseDecimal()

void anch::json::parseDecimal ( anch::json::ReaderContext & context)

Parse decimale digits and store them to context value's buffer

Parameters
contextthe context
Here is the call graph for this function:

◆ parseNumber()

void anch::json::parseNumber ( anch::json::ReaderContext & context)

Parse float.
End parsing at first unrecognized character.

Parameters
contextthe parser context
Here is the call graph for this function:

◆ scanNumber()

template<typename P>
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.

Template Parameters
Pthe number character predicate type
Parameters
contextthe reader context
isNumberCharthe number character predicate
Returns
the number characters (valid until next buffer refill)
Here is the call graph for this function:

◆ serialize() [1/7]

template<typename T>
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.

Template Parameters
Tthe object type
Parameters
valuethe objects to serialize
outthe output stream to write in
optionsthe options to use
Here is the call graph for this function:

◆ serialize() [2/7]

template<template< typename > typename C, typename T>
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, ...)

Template Parameters
Tthe object type
Cthe container's type
Parameters
valuethe object to serialize
optionsthe options to use
Returns
the serialized objects as std::string
Here is the call graph for this function:

◆ serialize() [3/7]

template<template< typename > typename C, typename T>
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, ...)

Template Parameters
Tthe object type
Cthe container's type
Parameters
valuethe objects to serialize
outthe output stream to write in
optionsthe options to use
Here is the call graph for this function:

◆ serialize() [4/7]

template<typename T>
std::string anch::json::serialize ( const std::map< std::string, T > & value,
const anch::json::MappingOptions & options = anch::json::DEFAULT_MAPPING_OPTIONS )

Serialize objects

Template Parameters
Tthe object type
Parameters
valuethe object to serialize
optionsthe options to use
Returns
the serialized objects as std::string
Here is the call graph for this function:

◆ serialize() [5/7]

template<typename T>
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

Template Parameters
Tthe object type
Parameters
valuethe objects to serialize
outthe output stream to write in
optionsthe options to use
Here is the call graph for this function:

◆ serialize() [6/7]

template<typename T>
std::string anch::json::serialize ( const T & value,
const anch::json::MappingOptions & options = anch::json::DEFAULT_MAPPING_OPTIONS )

Serialize object

Template Parameters
Tthe object type
Parameters
valuethe object to serialize
optionsthe options to use
Returns
the serialized object as std::string
Here is the call graph for this function:

◆ serialize() [7/7]

template<typename T>
void anch::json::serialize ( const T & value,
std::ostream & out,
const anch::json::MappingOptions & options = anch::json::DEFAULT_MAPPING_OPTIONS )

Serialize object

Template Parameters
Tthe object type
Parameters
valuethe object to serialize
outthe output stream to write in
optionsthe options to use
Here is the call graph for this function:

Variable Documentation

◆ ARRAY_BEGIN

const char anch::json::ARRAY_BEGIN = '['

JSON start array ('[')

◆ ARRAY_END

const char anch::json::ARRAY_END =']'

JSON start array (']')

◆ DEFAULT_MAPPING_OPTIONS

MappingOptions anch::json::DEFAULT_MAPPING_OPTIONS
extern

Default mapping options. It is not a constant so it can be changed in your application initialization.

◆ EMPTY_FIELD

const std::optional<std::string> anch::json::EMPTY_FIELD
extern

No field name constant value

◆ FIELD_SEPARATOR

const char anch::json::FIELD_SEPARATOR = ','

JSON fields delimiter (',')

◆ FIELD_VALUE_SEPARATOR

const char anch::json::FIELD_VALUE_SEPARATOR = ':'

JSON fiel/value delimiter (':')

◆ OBJECT_BEGIN

const char anch::json::OBJECT_BEGIN = '{'

JSON start object ('{')

◆ OBJECT_END

const char anch::json::OBJECT_END = '}'

JSON end object ('}')

◆ STRING_DELIMITER

const char anch::json::STRING_DELIMITER = '"'

JSON caracters string delimiter ('"')