49 return va == vb ? 0 : va < vb ? -1 : 1;
82 constexpr double integer_min =
static_cast<double>(std::numeric_limits<std::int64_t>::min());
83 constexpr double integer_end = -integer_min;
84 if (std::isnan(b) || b >= integer_end)
89 const double whole = std::trunc(b);
90 const auto b_whole =
static_cast<std::int64_t
>(whole);
92 return a < b_whole ? -1 : 1;
108 return std::isnan(b) ? 0 : 1;
141 static int kindval(
kind k)
145 case jsonv::kind::null:
147 case jsonv::kind::boolean:
149 case jsonv::kind::integer:
150 case jsonv::kind::decimal:
152 case jsonv::kind::string:
154 case jsonv::kind::array:
156 case jsonv::kind::object:
168template <
typename TCompareTraits>
175 if (
int kindcmp = traits.compare_kinds(a.
kind(), b.
kind()))
180 case jsonv::kind::null:
182 case jsonv::kind::boolean:
184 case jsonv::kind::integer:
186 if (b.
kind() == jsonv::kind::integer)
190 case jsonv::kind::decimal:
191 if (b.
kind() == jsonv::kind::integer)
195 return cmp < 0 ? 1 : cmp > 0 ? -1 : 0;
199 case jsonv::kind::string:
201 case jsonv::kind::array:
206 if (
int cmp =
compare(*aiter, *biter, traits))
211 case jsonv::kind::object:
213 if (
int objmetacmp = traits.compare_objects_meta(a, b))
220 if (
int cmp = traits.compare_object_keys(aiter->first, biter->first))
222 if (
int cmp =
compare(aiter->second, biter->second, traits))
313 const std::function<
void (
const path&,
const value&)>& func,
314 const path& base_path,
315 bool leafs_only =
false
327 const std::function<
void (
const path&,
const value&)>& func,
328 bool leafs_only =
false
369 type_conflict_function type_conflict
374 same_key_function same_key;
376 type_conflict_function type_conflict;
446template <
typename... TValue>
452 std::move(current_path),
455 std::forward<TValue>(rest)...
463template <
typename... TValue>
469 std::forward<TValue>(values)...
476template <
typename... TValue>
482 std::forward<TValue>(values)...
491 public std::runtime_error
An implementation of merge_rules that allows you to bind whatever functions you want to resolve confl...
virtual value resolve_same_key(path &¤t_path, value &&a, value &&b) const override
virtual value resolve_type_conflict(path &¤t_path, value &&a, value &&b) const override
This class is used in merge_explicit for defining what the function should do in the cases of conflic...
virtual value resolve_same_key(path &¤t_path, value &&a, value &&b) const =0
Called when merging a kind::object and the two objects share a key.
virtual value resolve_type_conflict(path &¤t_path, value &&a, value &&b) const =0
Called when a and b have kind values which are incompatible for merging.
Represents an exact path in some JSON structure.
These rules will recursively merge everything they can and coerce all values.
virtual value resolve_type_conflict(path &¤t_path, value &&a, value &&b) const override
Calls coerce_merge to combine the values.
virtual value resolve_same_key(path &¤t_path, value &&a, value &&b) const override
Recursively calls merge_explicit with the two values.
These rules throw an exception on all conflicts.
virtual value resolve_same_key(path &¤t_path, value &&a, value &&b) const override
virtual value resolve_type_conflict(path &¤t_path, value &&a, value &&b) const override
Error thrown when an unrepresentable value is encountered in a JSON AST.
code
Special code for describing the error encountered.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
double as_decimal() const
Get this value as a decimal.
const std::string & as_string() const
Get this value as a string.
object_iterator end_object()
Get an iterator to the one past the end of this object.
jsonv::kind kind() const
Get this value's kind.
bool as_boolean() const
Get this value as a boolean.
object_iterator begin_object()
Get an iterator to the first key-value pair in this object.
int64_t as_integer() const
Get this value as an integer.
array_iterator begin_array()
Get an iterator to the beginning of this array.
array_iterator end_array()
Get an iterator to the end of this array.
Copyright (c) 2014-2020 by Travis Gockel.
value same
Elements that were the same between the two halves of the diff.
value right
Elements that were unique to the right hand side of the diff.
value left
Elements that were unique to the left hand side of the diff.
int compare(const value &a, const value &b, const TCompareTraits &traits)
Compare the values a and b using the comparison traits.
JSONV_PUBLIC diff_result diff(value left, value right)
Find the differences and similarities between the structures of left and right.
JSONV_PUBLIC value merge_explicit(const merge_rules &rules, path current_path, value a, value b)
Merges values into a single value by the given rules (see merge_rules).
JSONV_PUBLIC value map(const std::function< value(const value &)> &func, const value &input)
Run a function over the values in the input.
JSONV_PUBLIC int compare_icase(const value &a, const value &b)
Compare the values a and b, but use case-insensitive matching on kind::string values.
value merge(TValue &&... values)
Merges all the provided values into a single value.
value merge_recursive(TValue &&... values)
Merges all the provided values into a single value.
JSONV_PUBLIC void 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.
The results of the diff operation.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
kind
Describes the kind of data a value holds.
Traits describing how to perform various aspects of comparison.
static int compare_strings(const std::string &a, const std::string &b)
Compare two string values.
static int compare_integers(std::int64_t a, std::int64_t b)
Compare two integer values.
static int compare_integer_decimal(std::int64_t a, double b)
Compare the integer a with the decimal b by their exact numeric values.
static int compare_booleans(bool a, bool b)
Compare two boolean values.
static int compare_objects_meta(const value &, const value &)
Compare two objects before comparing the values.
static int compare_decimals(double a, double b)
Compare two decimal values exactly, without an epsilon tolerance.
static int compare_kinds(kind a, kind b)
Compare two kinds a and b.
static int compare_object_keys(const std::string &a, const std::string &b)
Compare two strings used for the keys of objects.
Copyright (c) 2012-2020 by Travis Gockel.