JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
Coercion

Looser conversion functions to behave more like ECMAScript. More...

Functions

JSONV_PUBLIC bool jsonv::can_coerce (const kind &from, const kind &to)
 Can the given kind be converted from a kind to another?
 
JSONV_PUBLIC bool jsonv::can_coerce (const value &from, const kind &to)
 Can the given value be converted from a kind to another?
 
JSONV_PUBLIC std::nullptr_t jsonv::coerce_null (const value &from)
 Coerce from into a null.
 
JSONV_PUBLIC std::map< std::string, value > jsonv::coerce_object (const value &from)
 Coerce from into a map.
 
JSONV_PUBLIC std::vector< value > jsonv::coerce_array (const value &from)
 Coerce from into a vector.
 
JSONV_PUBLIC std::string jsonv::coerce_string (const value &from)
 Coerce from into an std::string.
 
JSONV_PUBLIC std::int64_t jsonv::coerce_integer (const value &from)
 Coerce from into an integer.
 
JSONV_PUBLIC double jsonv::coerce_decimal (const value &from)
 Coerce from into a double.
 
JSONV_PUBLIC bool jsonv::coerce_boolean (const value &from)
 Coerce from into a bool.
 
JSONV_PUBLIC value jsonv::coerce_merge (value a, value b)
 Combines a and b in any way possible.
 

Detailed Description

Looser conversion functions to behave more like ECMAScript.

Function Documentation

◆ can_coerce() [1/2]

JSONV_PUBLIC bool jsonv::can_coerce ( const kind &  from,
const kind &  to 
)

Can the given kind be converted from a kind to another?

Any kind can be coerced to a string or a boolean. A boolean, integer or decimal can be coerced to an integer or a decimal. Coercing to a null, object or array requires the source to have that same kind. A string going to an integer or a decimal is the one conversion this overload answers false for which can still succeed: it depends on what the string says, and the value overload is the one which looks.

Returns
true if the corresponding coerce_X function for the specified to will successfully return if given a value of the kind from; false if there is no such conversion (the coerce_X function might throw).

◆ can_coerce() [2/2]

JSONV_PUBLIC bool jsonv::can_coerce ( const value &  from,
const kind &  to 
)

Can the given value be converted from a kind to another?

Note
This is not only a convenience function! There is a special case for converting from a string into either a decimal or integer where the contents of the string must be considered. This function will look into the given from and see if it can successfully perfrom the coercion.
Returns
true if the corresponding coerce_X function for the specified to will successfully return if given the value from; false if there is no such conversion (the coerce_X function will throw).

◆ coerce_array()

JSONV_PUBLIC std::vector< value > jsonv::coerce_array ( const value &  from)

Coerce from into a vector.

Returns
a vector of the contents of from.
Exceptions
kind_errorif from is not kind::array.

◆ coerce_boolean()

JSONV_PUBLIC bool jsonv::coerce_boolean ( const value &  from)

Coerce from into a bool.

This follows the rules of Python's boolean coercion.

Returns
kind is... Rules
null false
object !from.empty()
array !from.empty()
string !from.empty() (even if the value is "false")
integer from != 0
decimal from != 0.0
boolean from.as_boolean()

◆ coerce_decimal()

JSONV_PUBLIC double jsonv::coerce_decimal ( const value &  from)

Coerce from into a double.

A string is accepted under the same rules as coerce_integer and is read as its nearest double. A number too small in magnitude for a double becomes zero; one too large for any finite double, such as 1e400, is refused.

Returns
kind is... Rules
null throws kind_error
object throws kind_error
array throws kind_error
string the number it holds, as above; otherwise throws kind_error
integer from.as_decimal()
decimal from.as_decimal()
boolean from.as_boolean() ? 1.0 : 0.0

◆ coerce_integer()

JSONV_PUBLIC std::int64_t jsonv::coerce_integer ( const value &  from)

Coerce from into an integer.

A decimal is truncated toward zero. If from is a decimal lower than the minimum of std::int64_t or higher than the maximum of std::int64_t, it is clamped to the lowest or highest value, respectively. A NaN decimal coerces to 0.

A string coerces if it holds a single JSON number (the RFC 8259 grammar), optionally surrounded by JSON whitespace (space, tab, line feed and carriage return), and nothing else. This is independent of parse_options: a comment, a leading +, a hexadecimal number, Infinity, NaN and null are all refused. An integer which fits in an std::int64_t is returned exactly. Any other number is read as its nearest double and then truncated and clamped as a decimal is. A number too large for any finite double, such as 1e400, is refused.

Returns
kind is... Rules
null throws kind_error
object throws kind_error
array throws kind_error
string the number it holds, as above; otherwise throws kind_error
integer from.as_integer()
decimal from.as_decimal(), truncated and clamped as above
boolean from.as_boolean() ? 1 : 0

◆ coerce_merge()

JSONV_PUBLIC value jsonv::coerce_merge ( value  a,
value  b 
)

Combines a and b in any way possible.

The result kind is usually based on the kind of a and loosely follows what ECMAScript does when you call + on two values (sort of). If you are looking for "predictable", this is not the function for you. If you are looking for convenience, this is it.

◆ coerce_null()

JSONV_PUBLIC std::nullptr_t jsonv::coerce_null ( const value &  from)

Coerce from into a null.

If from is not null, this will throw. It is not clear that there is a use for this beyond completeness.

Returns
nullptr if from has kind::null.
Exceptions
kind_errorif from is not kind::null.

◆ coerce_object()

JSONV_PUBLIC std::map< std::string, value > jsonv::coerce_object ( const value &  from)

Coerce from into a map.

Returns
a map of the contents of from.
Exceptions
kind_errorif from is not kind::object.

◆ coerce_string()

JSONV_PUBLIC std::string jsonv::coerce_string ( const value &  from)

Coerce from into an std::string.

If from is already kind::string, the value is simply returned. If from is any other kind, the result will be the same as to_string.