libdonut  2.3.2
Application framework for cross-platform game development in C++20
Classes | Typedefs | Enumerations | Functions
donut::json Namespace Reference

Classes

struct  SourceLocation
 Line and column numbers of a location in a JSON source string. More...
 
struct  Error
 Exception type for errors originating from the JSON API. More...
 
struct  SerializationOptions
 Options for JSON serialization. More...
 
struct  DeserializationOptions
 Options for JSON deserialization. More...
 
class  Object
 JSON object type whose API mimics that of std::multimap<String, Value>. More...
 
class  Array
 JSON array type whose API mimics that of std::vector<Value>. More...
 
class  Value
 JSON value type. More...
 
struct  Token
 Token data scanned from JSON. More...
 
class  Lexer
 Lexical analyzer for scanning and tokenizing input in the JSON5 format. More...
 
class  Parser
 Syntactic analyzer for parsing input in the JSON5 format obtained from a json::Lexer. More...
 
struct  Serializer
 Base template to specialize in order to implement JSON serialization for a specific type. More...
 
struct  Deserializer
 Base template to specialize in order to implement JSON deserialization for a specific type. More...
 
struct  Writer
 Stateful wrapper object of an output stream for JSON serialization. More...
 
struct  Reader
 Stateful wrapper object of an input stream for JSON deserialization. More...
 

Typedefs

using Null = Monostate
 JSON null type. More...
 
using Boolean = bool
 JSON boolean type. More...
 
using String = std::string
 JSON string type. More...
 
using Number = double
 JSON number type. More...
 
using StringParser = Parser< const char8_t * >
 Parser for reading contiguous UTF-8-encoded JSON strings. More...
 
using StreamParser = Parser< std::istreambuf_iterator< char > >
 Parser for reading UTF-8-encoded JSON input stream buffers. More...
 

Enumerations

enum class  TokenType : std::uint8_t {
  END_OF_FILE , IDENTIFIER_NULL , IDENTIFIER_FALSE , IDENTIFIER_TRUE ,
  IDENTIFIER_NAME , PUNCTUATOR_COMMA , PUNCTUATOR_COLON , PUNCTUATOR_OPEN_SQUARE_BRACKET ,
  PUNCTUATOR_CLOSE_SQUARE_BRACKET , PUNCTUATOR_OPEN_CURLY_BRACE , PUNCTUATOR_CLOSE_CURLY_BRACE , STRING ,
  NUMBER_BINARY , NUMBER_OCTAL , NUMBER_DECIMAL , NUMBER_HEXADECIMAL ,
  NUMBER_POSITIVE_INFINITY , NUMBER_NEGATIVE_INFINITY , NUMBER_POSITIVE_NAN , NUMBER_NEGATIVE_NAN
}
 Type of a scanned JSON5 token. More...
 

Functions

constexpr bool isWhitespaceCharacter (char32_t codePoint) noexcept
 Check if a Unicode code point is considered to be whitespace in JSON5. More...
 
constexpr bool isPunctuationCharacter (char32_t codePoint) noexcept
 Check if a Unicode code point is considered to be punctuation in JSON5. More...
 
constexpr bool isLineTerminatorCharacter (char32_t codePoint) noexcept
 Check if a Unicode code point marks the beginning of a line terminator sequence in JSON5. More...
 
template<typename T >
void serialize (std::ostream &stream, const T &value, const SerializationOptions &options={})
 Serialize a value of any JSON-serializable type to an output stream. More...
 
template<typename T >
void deserialize (std::istream &stream, T &value, const DeserializationOptions &options={})
 Deserialize a value of any JSON-serializable type from an input stream. More...
 
std::ostream & operator<< (std::ostream &stream, const Value &value)
 Write a JSON value to an output stream using the default serialization options. More...
 
std::istream & operator>> (std::istream &stream, Value &value)
 Read a JSON value from an input stream using the default deserialization options. More...
 
template<typename Callback >
auto onNull (Callback callback)
 Build a Parser::ValueVisitor that handles Null values with a given callback function. More...
 
template<typename Callback >
auto onBoolean (Callback callback)
 Build a Parser::ValueVisitor that handles Boolean values with a given callback function. More...
 
template<typename Callback >
auto onString (Callback callback)
 Build a Parser::ValueVisitor that handles String values with a given callback function. More...
 
template<typename Callback >
auto onNumber (Callback callback)
 Build a Parser::ValueVisitor that handles Number values with a given callback function. More...
 
template<typename Callback >
auto onObject (Callback callback)
 Build a Parser::ValueVisitor that handles objects with a given callback function. More...
 
template<typename Callback >
auto onArray (Callback callback)
 Build a Parser::ValueVisitor that handles arrays with a given callback function. More...
 
template<typename Callback >
auto onProperty (Callback callback)
 Build a Parser::PropertyVisitor that handles object properties with a given callback function. More...
 
void swap (Object &a, Object &b) noexcept
 
template<typename Predicate >
Object::size_type erase_if (Object &container, Predicate predicate)
 
void swap (Array &a, Array &b) noexcept
 
template<typename U >
Array::size_type erase (Array &container, const U &value)
 
template<typename Predicate >
Array::size_type erase_if (Array &container, Predicate predicate)
 

Typedef Documentation

◆ Null

JSON null type.

◆ Boolean

typedef bool donut::json::Boolean

JSON boolean type.

◆ String

using donut::json::String = typedef std::string

JSON string type.

◆ Number

typedef double donut::json::Number

JSON number type.

◆ StringParser

using donut::json::StringParser = typedef Parser<const char8_t*>

Parser for reading contiguous UTF-8-encoded JSON strings.

◆ StreamParser

using donut::json::StreamParser = typedef Parser<std::istreambuf_iterator<char> >

Parser for reading UTF-8-encoded JSON input stream buffers.

Enumeration Type Documentation

◆ TokenType

enum donut::json::TokenType : std::uint8_t
strong

Type of a scanned JSON5 token.

Enumerator
END_OF_FILE 

End-of-file marker.

IDENTIFIER_NULL 

Keyword null.

IDENTIFIER_FALSE 

Keyword false.

IDENTIFIER_TRUE 

Keyword true.

IDENTIFIER_NAME 

Unquoted identifier, e.g. abc.

PUNCTUATOR_COMMA 

Comma ',' symbol.

PUNCTUATOR_COLON 

Colon ':' symbol.

PUNCTUATOR_OPEN_SQUARE_BRACKET 

Open square bracket '[' symbol.

PUNCTUATOR_CLOSE_SQUARE_BRACKET 

Closing square bracket ']' symbol.

PUNCTUATOR_OPEN_CURLY_BRACE 

Open curly brace '{' symbol.

PUNCTUATOR_CLOSE_CURLY_BRACE 

Closing curly brace '}' symbol.

STRING 

Quoted string literal, e.g. "abc".

NUMBER_BINARY 

Binary number literal, e.g. 0b0000000111111111.

NUMBER_OCTAL 

Octal number literal, e.g. 0777.

NUMBER_DECIMAL 

Decimal number literal, e.g. 511.

NUMBER_HEXADECIMAL 

Hexadecimal number literal, e.g. 0x01FF.

NUMBER_POSITIVE_INFINITY 

Keyword Infinity.

NUMBER_NEGATIVE_INFINITY 

Keyword -Infinity.

NUMBER_POSITIVE_NAN 

Keyword NaN.

NUMBER_NEGATIVE_NAN 

Keyword -NaN.

Function Documentation

◆ isWhitespaceCharacter()

constexpr bool donut::json::isWhitespaceCharacter ( char32_t  codePoint)
constexprnoexcept

Check if a Unicode code point is considered to be whitespace in JSON5.

Parameters
codePointcode point value to check.
Returns
true if the code point is considered whitespace, false otherwise.

◆ isPunctuationCharacter()

constexpr bool donut::json::isPunctuationCharacter ( char32_t  codePoint)
constexprnoexcept

Check if a Unicode code point is considered to be punctuation in JSON5.

Parameters
codePointcode point value to check.
Returns
true if the code point is considered punctuation, false otherwise.

◆ isLineTerminatorCharacter()

constexpr bool donut::json::isLineTerminatorCharacter ( char32_t  codePoint)
constexprnoexcept

Check if a Unicode code point marks the beginning of a line terminator sequence in JSON5.

Parameters
codePointcode point value to check.
Returns
true if the code point is considered a line terminator, false otherwise.

◆ serialize()

template<typename T >
void donut::json::serialize ( std::ostream &  stream,
const T &  value,
const SerializationOptions options = {} 
)
inline

Serialize a value of any JSON-serializable type to an output stream.

Write any JSON-serializable value to an output stream using its corresponding implementation of Serializer.

Parameters
streamstream to write the output to.
valuevalue to serialize.
optionsserialization options, see SerializationOptions.
Exceptions
Erroron failure to serialize the value.
std::ios_base::failureif thrown by the output stream.
std::bad_allocon allocation failure.
anyexception thrown by a user-defined implementation of Serializer, if one is used in the serialization of the given value type.
Note
Serialization of user-defined types can be defined by implementing a specialization of the Serializer template.
Parameters
streamstream to write into.
valuevalue to write.
optionsoutput options, see SerializationOptions.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementation of T.
See also
Writer

◆ deserialize()

template<typename T >
void donut::json::deserialize ( std::istream &  stream,
T &  value,
const DeserializationOptions options = {} 
)
inline

Deserialize a value of any JSON-serializable type from an input stream.

Read a JSON value from an input stream into any value that is deserializable from JSON using its corresponding implementation of Deserializer.

Parameters
streamstream to read the input from.
valuevalue to deserialize to.
optionsdeserialization options, see DeserializationOptions.
Exceptions
Erroron failure to parse the value from the stream.
std::ios_base::failureif thrown by the input stream.
std::bad_allocon allocation failure.
anyexception thrown by a user-defined implementation of Deserializer, if one is used in the deserialization of the given value type.
Note
Deserialization of user-defined types can be defined by implementing a specialization of the Deserializer template.
Parameters
streamstream to read from.
valuereference to the output value to write the parsed result to.
optionsinput options, see DeserializationOptions.
Exceptions
anyexception thrown by the underlying input stream.
anyexception thrown by the Deserializer implementation of T.
See also
Reader

◆ operator<<()

std::ostream& donut::json::operator<< ( std::ostream &  stream,
const Value value 
)
inline

Write a JSON value to an output stream using the default serialization options.

Parameters
streamstream to write the output to.
valuethe JSON value to serialize.
Returns
a reference to the stream parameter, for chaining.
Exceptions
Erroron failure to serialize the value.
std::ios_base::failureif thrown by the output stream.
std::bad_allocon allocation failure.

◆ operator>>()

std::istream& donut::json::operator>> ( std::istream &  stream,
Value value 
)
inline

Read a JSON value from an input stream using the default deserialization options.

If the function fails to parse a JSON value from the stream, std::ios_base::failbit is set on the stream, which may or may not cause an exception to be thrown.

Parameters
streamstream to read the input from.
valuethe JSON value to deserialize to.
Returns
a reference to the stream parameter, for chaining.
Exceptions
std::ios_base::failureif thrown by the input stream.
std::bad_allocon allocation failure.

◆ onNull()

template<typename Callback >
auto donut::json::onNull ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles Null values with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitNull() and has the same semantics.
Returns
a visitor that uses the given callback for visiting values of type Null. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onBoolean()
json::onString()
json::onNumber()
json::onObject()
json::onArray()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)

◆ onBoolean()

template<typename Callback >
auto donut::json::onBoolean ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles Boolean values with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitBoolean() and has the same semantics.
Returns
a visitor that uses the given callback for visiting values of type Boolean. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onNull()
json::onString()
json::onNumber()
json::onObject()
json::onArray()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)

◆ onString()

template<typename Callback >
auto donut::json::onString ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles String values with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitString() and has the same semantics.
Returns
a visitor that uses the given callback for visiting values of type String. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onNull()
json::onBoolean()
json::onNumber()
json::onObject()
json::onArray()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)
Examples
example_game.cpp.

◆ onNumber()

template<typename Callback >
auto donut::json::onNumber ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles Number values with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitNumber() and has the same semantics.
Returns
a visitor that uses the given callback for visiting values of type Number. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onObject()
json::onArray()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)

◆ onObject()

template<typename Callback >
auto donut::json::onObject ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles objects with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitObject() and has the same semantics.
Returns
a visitor that uses the given callback for visiting objects. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onNumber()
json::onArray()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)

◆ onArray()

template<typename Callback >
auto donut::json::onArray ( Callback  callback)
inline

Build a Parser::ValueVisitor that handles arrays with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as ValueVisitor::visitArray() and has the same semantics.
Returns
a visitor that uses the given callback for visiting arrays. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
json::onNull()
json::onBoolean()
json::onString()
json::onNumber()
json::onObject()
Parser::parseFile(Parser::ValueVisitor&)
Parser::parseValue(Parser::ValueVisitor&)
Parser::parseArray(Parser::ValueVisitor&)
Examples
example_game.cpp.

◆ onProperty()

template<typename Callback >
auto donut::json::onProperty ( Callback  callback)
inline

Build a Parser::PropertyVisitor that handles object properties with a given callback function.

Parameters
callbackfunction object that is callable with the same signature as PropertyVisitor::visitProperty() and has the same semantics.
Returns
a visitor that uses the given callback for visiting arrays. This visitor can be combined with other related visitors using the pipe operator '|'.
Exceptions
anyexception thrown by the move constructor of the callback type.
See also
Parser::parseObject(Parser::PropertyVisitor&)
Examples
example_game.cpp.

◆ swap() [1/2]

void donut::json::swap ( Object a,
Object b 
)
inlinenoexcept

◆ erase_if() [1/2]

template<typename Predicate >
Object::size_type donut::json::erase_if ( Object container,
Predicate  predicate 
)
inline

◆ swap() [2/2]

void donut::json::swap ( Array a,
Array b 
)
inlinenoexcept

◆ erase()

template<typename U >
Array::size_type donut::json::erase ( Array container,
const U &  value 
)
inline

◆ erase_if() [2/2]

template<typename Predicate >
Array::size_type donut::json::erase_if ( Array container,
Predicate  predicate 
)
inline