JSON Voorhees
Killer JSON for C++
Algorithm

A collection of useful free functions a la <algorithm>. More...

Classes

struct  jsonv::compare_traits
 Traits describing how to perform various aspects of comparison. More...
 
struct  jsonv::diff_result
 The results of the diff operation. More...
 
class  jsonv::merge_rules
 This class is used in merge_explicit for defining what the function should do in the cases of conflicts. More...
 
class  jsonv::dynamic_merge_rules
 An implementation of merge_rules that allows you to bind whatever functions you want to resolve conflicts. More...
 
class  jsonv::throwing_merge_rules
 These rules throw an exception on all conflicts. More...
 
class  jsonv::recursive_merge_rules
 These rules will recursively merge everything they can and coerce all values. More...
 
class  jsonv::validation_error
 Error thrown when an unrepresentable value is encountered in a JSON AST. More...
 
struct  jsonv::value_compare
 Compares a value to another using the standard-issue value::compare function. More...
 
struct  jsonv::value_compare_icase
 Compares two values, ignoring the case for any value with kind::string. More...
 
struct  jsonv::basic_value_binary_predicate< FCompare, FResult >
 

Typedefs

using jsonv::value_equal_to = basic_value_binary_predicate< value_compare, std::equal_to< int >>
 
using jsonv::value_equal_to_icase = basic_value_binary_predicate< value_compare_icase, std::equal_to< int >>
 
using jsonv::value_not_equal_to = basic_value_binary_predicate< value_compare, std::not_equal_to< int >>
 
using jsonv::value_not_equal_to_icase = basic_value_binary_predicate< value_compare_icase, std::not_equal_to< int >>
 
using jsonv::value_less = basic_value_binary_predicate< value_compare, std::less< int >>
 
using jsonv::value_less_icase = basic_value_binary_predicate< value_compare_icase, std::less< int >>
 
using jsonv::value_less_equal = basic_value_binary_predicate< value_compare, std::less_equal< int >>
 
using jsonv::value_less_equal_icase = basic_value_binary_predicate< value_compare_icase, std::less_equal< int >>
 
using jsonv::value_greater = basic_value_binary_predicate< value_compare, std::greater< int >>
 
using jsonv::value_greater_icase = basic_value_binary_predicate< value_compare_icase, std::greater< int >>
 
using jsonv::value_greater_equal = basic_value_binary_predicate< value_compare, std::greater_equal< int >>
 
using jsonv::value_greater_equal_icase = basic_value_binary_predicate< value_compare_icase, std::greater_equal< int >>
 

Functions

template<typename TCompareTraits >
int jsonv::compare (const value &a, const value &b, const TCompareTraits &traits)
 Compare the values a and b using the comparison traits. More...
 
JSONV_PUBLIC int jsonv::compare (const value &a, const value &b)
 Compare the values a and b with strict comparison traits. More...
 
JSONV_PUBLIC int jsonv::compare_icase (const value &a, const value &b)
 Compare the values a and b, but use case-insensitive matching on kind::string values. More...
 
JSONV_PUBLIC diff_result jsonv::diff (value left, value right)
 Find the differences and similarities between the structures of left and right. More...
 
JSONV_PUBLIC value jsonv::map (const std::function< value(const value &)> &func, const value &input)
 Run a function over the values in the input. More...
 
JSONV_PUBLIC value jsonv::map (const std::function< value(value)> &func, value &&input)
 Run a function over the values in the input. More...
 
JSONV_PUBLIC void jsonv::traverse (const value &tree, const std::function< void(const path &, const value &)> &func, const path &base_path, bool leafs_only=false)
 Recursively walk the provided tree and call func for each item in the tree. More...
 
JSONV_PUBLIC void jsonv::traverse (const value &tree, const std::function< void(const path &, const value &)> &func, bool leafs_only=false)
 Recursively walk the provided tree and call func for each item in the tree. More...
 
JSONV_PUBLIC value jsonv::merge_explicit (const merge_rules &rules, path current_path, value a, value b)
 Merges two values, a and b into a single value. More...
 
JSONV_PUBLIC value jsonv::merge_explicit (const merge_rules &, const path &, value a)
 
JSONV_PUBLIC value jsonv::merge_explicit (const merge_rules &, const path &)
 
template<typename... TValue>
value jsonv::merge_explicit (const merge_rules &rules, path current_path, value a, value b, value c, TValue &&...rest)
 
template<typename... TValue>
value jsonv::merge (TValue &&...values)
 Merges all the provided values into a single value. More...
 
template<typename... TValue>
value jsonv::merge_recursive (TValue &&...values)
 Merges all the provided values into a single value. More...
 
JSONV_PUBLIC std::ostream & jsonv::operator<< (std::ostream &os, const validation_error::code &code)
 
JSONV_PUBLIC void jsonv::validate (const value &val)
 Check that the provided val is perfectly representable as a JSON string. More...
 
value jsonv::value::map (const std::function< value(const value &)> &func) const &
 Run a function over the values of this instance. More...
 
value jsonv::value::map (const std::function< value(value)> &func)&&
 Run a function over the values of this instance. More...
 

Detailed Description

A collection of useful free functions a la <algorithm>.


Class Documentation

struct jsonv::diff_result

The results of the diff operation.

Definition at line 194 of file algorithm.hpp.

Class Members
value left Elements that were unique to the left hand side of the diff.
value right Elements that were unique to the right hand side of the diff.
value same Elements that were the same between the two halves of the diff.

Function Documentation

template<typename TCompareTraits >
int jsonv::compare ( const value a,
const value b,
const TCompareTraits &  traits 
)

Compare the values a and b using the comparison traits.

Template Parameters
TCompareTraitsA type which should be compatible with the public signatures on the compare_traits class.

Definition at line 124 of file algorithm.hpp.

JSONV_PUBLIC int jsonv::compare ( const value a,
const value b 
)

Compare the values a and b with strict comparison traits.

See also
value::compare
compare_icase
JSONV_PUBLIC int jsonv::compare_icase ( const value a,
const value b 
)

Compare the values a and b, but use case-insensitive matching on kind::string values.

This does not use case-insensitive matching on the keys of objects!

See also
compare
JSONV_PUBLIC diff_result jsonv::diff ( value  left,
value  right 
)

Find the differences and similarities between the structures of left and right.

If left and right have a different kind (and the kind difference is not kind::integer and kind::decimal), left and right will be placed directly in the result. If they have the same kind and it is scalar, the values get a direct comparison. If they are the same, the result is moved to diff_result::same. If they are different, left and right are moved to diff_result::left and diff_result::right, respectively. For kind::array and kind::object, the value elements are compared recursively.

JSONV_PUBLIC value jsonv::map ( const std::function< value(const value &)> &  func,
const value input 
)

Run a function over the values in the input.

The behavior of this function is different, depending on the kind of input. For scalar kinds (kind::integer, kind::null, etc), func is called once with the value. If input is kind::array, func is called for every value in the array and the output will be an array with each element transformed by func. If input is kind::object, the result will be an object with each key transformed by func.

Parameters
funcThe function to apply to the element or elements of input.
inputThe value to transform.
JSONV_PUBLIC value jsonv::map ( const std::function< value(value)> &  func,
value &&  input 
)

Run a function over the values in the input.

The behavior of this function is different, depending on the kind of input. For scalar kinds (kind::integer, kind::null, etc), func is called once with the value. If input is kind::array, func is called for every value in the array and the output will be an array with each element transformed by func. If input is kind::object, the result will be an object with each key transformed by func.

Parameters
funcThe function to apply to the element or elements of input.
inputThe value to transform.
Note
This version of map provides only a basic exception-safety guarantee. If an exception is thrown while transforming a non-scalar kind, there is no rollback action, so input is left in a usable, but unpredictable state. If you need a strong exception guarantee, use the version of map that takes a constant reference to a value.
value jsonv::value::map ( const std::function< value(const value &)> &  func) const

Run a function over the values of this instance.

The behavior of this function is different, depending on the kind. For scalar kinds (kind::integer, kind::null, etc), func is called once with the value. If this is kind::array, func is called for every value in the array and the output will be an array with each element transformed by func. If this is kind::object, the result will be an object with each key transformed by func.

Parameters
funcThe function to apply to the element or elements of this instance.
value jsonv::value::map ( const std::function< value(value)> &  func)

Run a function over the values of this instance.

The behavior of this function is different, depending on the kind. For scalar kinds (kind::integer, kind::null, etc), func is called once with the value. If this is kind::array, func is called for every value in the array and the output will be an array with each element transformed by func. If this is kind::object, the result will be an object with each key transformed by func.

Parameters
funcThe function to apply to the element or elements of this instance.
Note
This version of map provides only a basic exception-safety guarantee. If an exception is thrown while transforming a non-scalar kind, there is no rollback action, so this is left in a usable, but unpredictable state. If you need a strong exception guarantee, use the constant reference version of map.
template<typename... TValue>
value jsonv::merge ( TValue &&...  values)

Merges all the provided values into a single value.

If there are any key or type conflicts, an exception will be thrown.

Definition at line 399 of file algorithm.hpp.

JSONV_PUBLIC value jsonv::merge_explicit ( const merge_rules rules,
path  current_path,
value  a,
value  b 
)

Merges two values, a and b into a single value.

The merging follows a few simple rules:

  • If a.kind() != b.kind() and they are not kind::integer and kind::decimal, call on_type_conflict and return the result.
  • Otherwise, branch based on the (shared) type:
    • kind::object - Return a new object with all the values from a and b for the keys which are unique per object. For the keys which are shared, the value is the result of on_same_key.
    • kind::array - Return a new array with the values of b appended to a.
    • kind::string - Return a new string with b appended to a.
    • kind::boolean - Return a.as_boolean() || b.as_boolean()
    • kind::integer - If is kind::integer, return a + b as an integer; otherwise, return it as a decimal.
    • kind::decimal - Return a + b as a decimal.
Parameters
rulesare the rules to merge with (see merge_rules).
current_pathThe current path into the value that we are merging. This can be used to give more useful error information if we are merging recursively.
ais a value to merge.
bis a value to merge.
template<typename... TValue>
value jsonv::merge_recursive ( TValue &&...  values)

Merges all the provided values into a single value.

If there are any keys which are shared, their values are also merged.

Definition at line 411 of file algorithm.hpp.

JSONV_PUBLIC void jsonv::traverse ( const value tree,
const std::function< void(const path &, const value &)> &  func,
const path base_path,
bool  leafs_only = false 
)

Recursively walk the provided tree and call func for each item in the tree.

Parameters
treeThe JSON value to traverse.
funcThe function to call for each element in the tree.
base_pathThe path to prepend to each output path to func. This can be useful if beginning traversal from inside of some JSON structure.
leafs_onlyIf true, call func only when the current path is a "leaf" value (string, integer, decimal, boolean, or null or an empty array or object); if false, call func for all entries in the tree.
JSONV_PUBLIC void jsonv::traverse ( const value tree,
const std::function< void(const path &, const value &)> &  func,
bool  leafs_only = false 
)

Recursively walk the provided tree and call func for each item in the tree.

Parameters
treeThe JSON value to traverse.
funcThe function to call for each element in the tree.
leafs_onlyIf true, call func only when the current path is a "leaf" value (string, integer, decimal, boolean, or null or an empty array or object); if false, call func for all entries in the tree.
JSONV_PUBLIC void jsonv::validate ( const value val)

Check that the provided val is perfectly representable as a JSON string.

The JSON specification does not have support for things like non-finite floating-point numbers (NaN and infinity). This means value defined with these values will get serialized as null. This constitutes a loss of information, but not acting this way would lead to the encoder outputting invalid JSON text, which is completely unacceptable. Use this funciton to check that there will be no information loss when encoding.

Exceptions
validation_errorif val contains an unrepresentable value.