Skip to content

Commit

Permalink
Add patch from nlohmann/json#212
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Karlsson authored and hernando committed Jun 27, 2018
1 parent d1b1606 commit 949464c
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions brion/detail/json.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* clang-format off */

/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++
Expand Down Expand Up @@ -125,7 +127,7 @@ using json = basic_json<>;
"unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
#endif
#elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
#error \
"unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
#endif
Expand Down Expand Up @@ -10983,6 +10985,23 @@ class basic_json
return object.release();
}

/// helper for insertion of an iterator (supports GCC 4.8+)
template<typename... Args>
iterator insert_iterator(const_iterator pos, Args&& ... args)
{
iterator result(this);
assert(m_value.array != nullptr);

auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
result.m_it.array_iterator = m_value.array->begin() + insert_pos;

// For GCC 4.9+ only, this could become:
// result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);

return result;
}

////////////////////////
// JSON value storage //
////////////////////////
Expand Down Expand Up @@ -15203,10 +15222,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator =
m_value.array->insert(pos.m_it.array_iterator, val);
return result;
return insert_iterator(pos, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " +
Expand Down Expand Up @@ -15259,10 +15275,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator =
m_value.array->insert(pos.m_it.array_iterator, cnt, val);
return result;
return insert_iterator(pos, cnt, val);
}

JSON_THROW(type_error::create(309, "cannot use insert() with " +
Expand Down Expand Up @@ -15329,12 +15342,10 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator =
m_value.array->insert(pos.m_it.array_iterator,
first.m_it.array_iterator,
last.m_it.array_iterator);
return result;
return insert_iterator(
pos,
first.m_it.array_iterator,
last.m_it.array_iterator);
}

/*!
Expand Down Expand Up @@ -15378,11 +15389,7 @@ class basic_json
}

// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator =
m_value.array->insert(pos.m_it.array_iterator, ilist.begin(),
ilist.end());
return result;
return insert_iterator(pos, ilist.begin(), ilist.end());
}

/*!
Expand Down Expand Up @@ -17933,15 +17940,15 @@ class basic_json

@since version 3.0.0
*/
void merge_patch(const basic_json& patch)
void merge_patch(const basic_json& jspatch)
{
if (patch.is_object())
if (jspatch.is_object())
{
if (not is_object())
{
*this = object();
}
for (auto it = patch.begin(); it != patch.end(); ++it)
for (auto it = jspatch.begin(); it != jspatch.end(); ++it)
{
if (it.value().is_null())
{
Expand All @@ -17955,7 +17962,7 @@ class basic_json
}
else
{
*this = patch;
*this = jspatch;
}
}

Expand Down

0 comments on commit 949464c

Please sign in to comment.