TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ diff()

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::diff ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType > &  source,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType > &  target,
const std::string &  path = "" 
)
inlinestatic

creates a diff as a JSON patch

Creates a JSON Patch so that value source can be changed into the value target by calling patch function.

Invariant
For two JSON values source and target, the following code yields always true:
source.patch(diff(source, target)) == target;
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json diff(const basic_json &source, const basic_json &target, const std::string &path="")
creates a diff as a JSON patch
Definition: json.hpp:26466
Note
Currently, only remove, add, and replace operations are generated.
Parameters
[in]sourceJSON value to compare from
[in]targetJSON value to compare against
[in]pathhelper value to create JSON pointers
Returns
a JSON patch to convert the source to target

@complexity Linear in the lengths of source and target.

@liveexample{The following code shows how a JSON patch is created as a diff for two JSON values.,diff}

See also
see patch – apply a JSON patch
see merge_patch – apply a JSON Merge Patch
RFC 6902 (JSON Patch)
Since
version 2.0.0

Definition at line 26466 of file json.hpp.

26468 {
26469 // the patch
26470 basic_json result(value_t::array);
26471
26472 // if the values are the same, return empty patch
26473 if (source == target)
26474 {
26475 return result;
26476 }
26477
26478 if (source.type() != target.type())
26479 {
26480 // different types: replace value
26481 result.push_back(
26482 {
26483 {"op", "replace"}, {"path", path}, {"value", target}
26484 });
26485 return result;
26486 }
26487
26488 switch (source.type())
26489 {
26490 case value_t::array:
26491 {
26492 // first pass: traverse common elements
26493 std::size_t i = 0;
26494
26495 while (i < source.size() && i < target.size())
26496 {
26497 // recursive call to compare array values at index i
26498 auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
26499 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
26500 ++i;
26501 }
26502
26503 // i now reached the end of at least one array
26504 // in a second pass, traverse the remaining elements
26505 // remove my remaining elements
26506 const auto end_index = static_cast<difference_type>(result.size());
26507
26508 while (i < source.size())
26509 {
26510 // add operations in reverse order to avoid invalid
26511 // indices
26512 result.insert(result.begin() + end_index, object(
26513 {
26514 {"op", "remove"},
26515 {"path", path + "/" + std::to_string(i)}
26516 }));
26517 ++i;
26518 }
26519
26520 // add other remaining elements
26521 while (i < target.size())
26522 {
26523 result.push_back(
26524 {
26525 {"op", "add"},
26526 {"path", path + "/-"},
26527 {"value", target[i]}
26528 });
26529 ++i;
26530 }
26531
26532 break;
26533 }
26534
26535 case value_t::object:
26536 {
26537 // first pass: traverse this object's elements
26538 for (auto it = source.cbegin(); it != source.cend(); ++it)
26539 {
26540 // escape the key name to be used in a JSON patch
26541 const auto path_key = path + "/" + detail::escape(it.key());
26542
26543 if (target.find(it.key()) != target.end())
26544 {
26545 // recursive call to compare object values at key it
26546 auto temp_diff = diff(it.value(), target[it.key()], path_key);
26547 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
26548 }
26549
26550 else
26551 {
26552 // found a key that is not in o -> remove it
26553 result.push_back(object(
26554 {
26555 {"op", "remove"}, {"path", path_key}
26556 }));
26557 }
26558 }
26559
26560 // second pass: traverse other object's elements
26561 for (auto it = target.cbegin(); it != target.cend(); ++it)
26562 {
26563 if (source.find(it.key()) == source.end())
26564 {
26565 // found a key that is not in this -> add it
26566 const auto path_key = path + "/" + detail::escape(it.key());
26567 result.push_back(
26568 {
26569 {"op", "add"}, {"path", path_key},
26570 {"value", it.value()}
26571 });
26572 }
26573 }
26574
26575 break;
26576 }
26577
26578 case value_t::null:
26579 case value_t::string:
26580 case value_t::boolean:
26584 case value_t::binary:
26585 case value_t::discarded:
26586 default:
26587 {
26588 // both primitive type: replace value
26589 result.push_back(
26590 {
26591 {"op", "replace"}, {"path", path}, {"value", target}
26592 });
26593 break;
26594 }
26595 }
26596
26597 return result;
26598 }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:19164
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:17978
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:19662
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)
std::string escape(std::string s)
string escaping as described in RFC 6901 (Sect. 4)
Definition: json.hpp:2558