|
JSON Voorhees
Killer JSON for C++
|
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? More... | |
| JSONV_PUBLIC bool | jsonv::can_coerce (const value &from, const kind &to) |
Can the given value be converted from a kind to another? More... | |
| JSONV_PUBLIC std::nullptr_t | jsonv::coerce_null (const value &from) |
Coerce from into a null. More... | |
| JSONV_PUBLIC std::map< std::string, value > | jsonv::coerce_object (const value &from) |
Coerce from into a map. More... | |
| JSONV_PUBLIC std::vector< value > | jsonv::coerce_array (const value &from) |
Coerce from into a vector. More... | |
| JSONV_PUBLIC std::string | jsonv::coerce_string (const value &from) |
Coerce from into an std::string. More... | |
| JSONV_PUBLIC std::int64_t | jsonv::coerce_integer (const value &from) |
| Coerce from into an integer. More... | |
| JSONV_PUBLIC double | jsonv::coerce_decimal (const value &from) |
Coerce from into a double. More... | |
| JSONV_PUBLIC bool | jsonv::coerce_boolean (const value &from) |
Coerce from into a bool. More... | |
| JSONV_PUBLIC value | jsonv::coerce_merge (value a, value b) |
| Combines a and b in any way possible. More... | |
Looser conversion functions to behave more like ECMAScript.
| JSONV_PUBLIC bool jsonv::can_coerce | ( | const kind & | from, |
| const kind & | to | ||
| ) |
Can the given kind be converted from a kind to another?
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). | JSONV_PUBLIC bool jsonv::can_coerce | ( | const value & | from, |
| const kind & | to | ||
| ) |
Can the given value be converted from a kind to another?
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.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). | JSONV_PUBLIC std::vector<value> jsonv::coerce_array | ( | const value & | from | ) |
Coerce from into a vector.
| kind_error | if from is not kind::array. |
| JSONV_PUBLIC bool jsonv::coerce_boolean | ( | const value & | from | ) |
Coerce from into a bool.
This follows the rules of Python's boolean coercion.
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() |
| JSONV_PUBLIC double jsonv::coerce_decimal | ( | const value & | from | ) |
Coerce from into a double.
kind is... | Rules |
|---|---|
null | throws kind_error |
object | throws kind_error |
array | throws kind_error |
string | parse(from.as_string()).as_decimal() |
integer | from.as_decimal() |
decimal | from.as_decimal() |
boolean | from.as_boolean() ? 1.0 : 0.0 |
| JSONV_PUBLIC std::int64_t jsonv::coerce_integer | ( | const value & | from | ) |
Coerce from into an integer.
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.
kind is... | Rules |
|---|---|
null | throws kind_error |
object | throws kind_error |
array | throws kind_error |
string | parse(from.as_string()).as_integer() |
integer | from.as_integer() |
decimal | std::int64_t(from.as_decimal()) |
boolean | from.as_boolean() ? 1 : 0 |
| 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.
| 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.
nullptr if from has kind::null. | kind_error | if from is not kind::null. |
| JSONV_PUBLIC std::map<std::string, value> jsonv::coerce_object | ( | const value & | from | ) |
Coerce from into a map.
| kind_error | if from is not kind::object. |
| 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.