Batteries Included

January 10, 2023 ยท View on GitHub

Default Traits

The included Type Traits contain (partial) specialisations for the following types.

Specialised forRemarks
null_t
bool
signed integers
unsigned integers
double, float
empty_binary_t
empty_array_t
empty_object_t
std::string
tao::string_view
const char*
const std::string&
std::vector< tao::byte >
tao::binary_view
const std::vector< tao::byte >&
std::vector< basic_value< Traits, Base > >Corresponds to JSON Array.
basic_value< Traits, Base >*Creates Value Pointer; must not be nullptr.
const basic_value< Traits, Base >*Creates Value Pointer; must not be nullptr.
std::map< std::string, basic_value< Traits, Base > >Corresponds to JSON Object.
tao::optional< T >Empty optional corresponds to JSON Null.

Additional Traits

The following additional specialisations are part of various additional traits classes. These classes are defined in various headers in tao/json/contrib. It is also possible to include tao/json/contrib/traits.hpp which adds appropriate specialisations to the default traits tao::json::traits<>.

Implementation forHeader
std::array< T, N >tao/json/contrib/array_traits.hpp
std::deque< T >tao/json/contrib/deque_traits.hpp
std::list< T >tao/json/contrib/list_traits.hpp
std::map< std::string, T >tao/json/contrib/map_traits.hpp
std::pair< U, V >tao/json/contrib/pair_traits.hpp
std::set< T >tao/json/contrib/set_traits.hpp
std::shared_ptr< T >tao/json/contrib/shared_ptr_traits.hpp
std::tuple< Ts... >tao/json/contrib/tuple_traits.hpp
std::unique_ptr< T >tao/json/contrib/unique_ptr_traits.hpp
std::unordered_map< std::string, T >tao/json/contrib/unordered_map_traits.hpp
std::unordered_set< T >tao/json/contrib/unordered_set_traits.hpp
std::vector< T >tao/json/contrib/vector_traits.hpp

Events Producers

Events Producers are functions that take some input from which they generate Events function calls on a provided Consumer.

The common namespace prefix tao::json:: is omitted.

ProducerDescription
events::from_streamReads JSON from a std::istream.
events::from_stringReads JSON from a std::string.
events::from_valueGenerates Events for a basic_value<>.
events::from_fileReads JSON from the file.
jaxn::events::from_streamReads JAXN from a std::istream.
jaxn::events::from_stringReads JAXN from a std::string.
jaxn::events::from_fileReads JAXN from the file.
cbor::events::from_stringReads CBOR from a std::string.
cbor::events::from_fileReads CBOR from the file.
msgpack::events::from_stringReads MSGPACK from a std::string.
msgpack::events::from_fileReads MSGPACK from the file.
ubjson::events::from_stringReads UBJSON from a std::string.
ubjson::events::from_fileReads UBJSON from the file.

Events Consumers

Events Consumers are structs or classes that implement (a complete subset of) the Events Interface. Consumers are usually instantiated and then passed to an Events Producer function as argument.

The common namespace prefix tao::json:: is omitted.

ConsumerDescription
events::debugWrites all Events to a std::ostream in human-readable form.
events::discardDoes nothing with the Events it receives.
events::hashCalculates a SHA-256 hash for the received Events.
events::statisticsCounts Events, and string and binary lengths.
events::to_pretty_streamWrites nicely formatted JSON to a std::ostream.
events::to_streamWrites compact JSON to a std::ostream.
events::to_stringWrites compact JSON to a std::string.
events::to_valueBuilds a Value with the received Events.
events::validate_event_orderChecks whether the order of received Events is consistent.
jaxn::events::to_pretty_streamWrites nicely formatted JAXN to a std::ostream.
jaxn::events::to_streamWrites compact JAXN to a std::ostream.
jaxn::events::to_stringWrites compact JAXN to a std::string.
cbor::events::to_streamWrites CBOR to a std::ostream.
cbor::events::to_stringWrites CBOR to a std::string.
msgpack::events::to_streamWrites MSGPACK to a std::ostream.
msgpack::events::to_stringWrites MSGPACK to a std::string.
ubjson::events::to_streamWrites UBJSON to a std::ostream.
ubjson::events::to_stringWrites UBJSON to a std::string.

Events Transformers

Transformers are structs or classes that are both Event Consumers that implement the Events Interface functions, and Event Producers that call the Events Interface functions on one or more other Events Consumers.

The common namespace prefix tao::json:: is omitted.

TransformerDescription
events::binary_to_base64Passes through all Events except for binary data which is converted to base64-encoded strings.
events::binary_to_base64urlPasses through all Events except for binary data which is converted to URL-safe base64-encoded strings.
events::binary_to_exceptionPasses through all Events except for binary data which provokes an exception.
events::binary_to_hexPasses through all Events except for binary data which is is converted to hex-dumped strings.
events::key_camel_case_to_snake_casePasses through all Events except for keys in Objects which are converted from "CamelCaseStyle" to "snake_case_style".
events::key_snake_case_to_camel_casePasses through all Events except for keys in Objects which are converted from "snake_case_style" to "CamelCaseStyle".
events::limit_nesting_depthPasses through all Events but throws an exception when the nesting of Arrays and Objects exceeds a limit.
events::limit_value_countPasses through all Events but throws an exception when the total number of (sub)-value exceeds a limit.
events::events::non_finite_to_exceptionPasses through all Events except for numbers of type double which contain non-finite values which provoke an exception.
events::non_finite_to_nullPasses through all Events except for numbers of type double which contain non-finite values which are passed on as null().
events::non_finite_to_stringPasses through all Events except for numbers of type double which contain non-finite values which are passed on as appropriate string().
events::prefer_signedPasses through all Events except for numbers of type std::uint64_t which fit into a std::int64_t and are passed on as such.
events::prefer_unsignedPasses through all Events except for numbers of type std::int64_t which fit into a std::uint64_t and are passed on as such.
events::refPasses all Events to another Consumer or Transformer to which it holds a C++ reference.
events::teePasses all Events to an arbitrary number of other Consumers or Transformers which it holds in a std::tuple<>.
events::validate_keysPasses through all Events except for keys which are first validated against a provided PEGTL grammar rule.
events::virtual_refLike ref, but implements the virtual Event functions from virtual_base.

Convenience Functions

Functions that combine the similarly named Events Producer from above with an Events Consumer to return a corresponding Value.

The common namespace prefix tao::json:: is omitted.

FunctionDescription
from_streamReads JSON from a std::istream.
from_stringReads JSON from a std::string.
from_fileReads JSON from the file.
jaxn::from_streamReads JAXN from a std::istream.
jaxn::from_stringReads JAXN from a std::string.
jaxn::from_fileReads JAXN from the file.
cbor::from_stringReads CBOR from a std::string.
cbor::from_fileReads CBOR from the file.
msgpack::from_stringReads MSGPACK from a std::string.
msgpack::from_fileReads MSGPACK from the file.
ubjson::from_stringReads UBJSON from a std::string.
ubjson::from_fileReads UBJSON from the file.

Functions that combine the similarly named Events Consumers with a Producer that generates Events from a Value.

The common namespace prefix tao::json:: is omitted.

FunctionDescription
to_streamWrites compact or nicely formatted JSON to a std::ostream.
to_stringWrites compact or nicely formatted JSON to a std::string.
jaxn::to_streamWrites compact or nicely formatted JAXN to a std::ostream.
jaxn::to_stringWrites compact or nicely formatted JAXN to a std::string.
jaxn::to_streamWrites CBOR to a std::ostream.
jaxn::to_stringWrites CBOR to a std::string.
msgpack::to_streamWrites MSGPACK to a std::ostream.
msgpack::to_stringWrites MSGPACK to a std::string.
ubjson::to_streamWrites UBJSON to a std::ostream.
ubjson::to_stringWrites UBJSON to a std::string.

These functions optionally apply an arbitrary list of Transformers given as additional template parameters.

Conversion Utilities

The following conversion utilities are included in the src/examples/json directory. Note that they convert between formats on-the-fly, i.e. without creating any intermediate in-memory representation.

cbor_to_jaxn
cbor_to_json
cbor_to_msgpack
cbor_to_pretty_jaxn
cbor_to_pretty_json
cbor_to_ubjson
jaxn_to_cbor
jaxn_to_jaxn
jaxn_to_msgpack
jaxn_to_pretty_jaxn
jaxn_to_ubjson
json_to_cbor
json_to_json
json_to_msgpack
json_to_pretty_json
json_to_ubjson
msgpack_to_cbor
msgpack_to_jaxn
msgpack_to_json
msgpack_to_pretty_jaxn
msgpack_to_pretty_json
msgpack_to_ubjson
ubjson_to_cbor
ubjson_to_jaxn
ubjson_to_json
ubjson_to_msgpack
ubjson_to_prety_jaxn
ubjson_to_prety_json

Copyright (c) 2018-2023 Dr. Colin Hirsch and Daniel Frey