Open
Description
Description
json::update() triggers JSON_ASSERT when certain conditions are met:
- JSON_DIAGNOSTICS turned on (this JSON_ASSERT is inside #if JSON_DIAGNOSTICS block)
- Build type is "Debug"
- ordered_json used (basic_json works fine)
- "merge_objects" argument of update() set to "true"
- passed json objects have some specific structure:
Behavior depends on objects' fields and is very inconsistent. Assertion failure happens with my example, but it won't if you remove any field, change fields' order, or make keys inside "numbers" equal
Reproduction steps
Build my example and try to run it
commands:
cmake -S . -B ./build
cmake --build ./build
./build/Debug/update_test.exe
Output:
j1: {"numbers":{"one":1}}
j2: {"numbers":{"two":2},"string":"t"}
Assertion failed: !check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) { return j.m_parent == this; }), file 3rd-party/nlohmann/json.hpp, line 700
Expected vs. actual results
Expected: No errors
Actual: Assertion failure
Minimal code example
CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project (update_test
LANGUAGES CXX
)
add_executable( ${PROJECT_NAME}
"main.cpp"
)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/3rd-party>
)
target_compile_definitions(${PROJECT_NAME} PUBLIC
JSON_DIAGNOSTICS=1
)
main.cpp
Based on update() unit test
https://github.com/nlohmann/json/blob/develop/tests/src/unit-modifiers.cpp#L728
#include <iostream>
#include <nlohmann/json.hpp>
int main(int argc, char** argv)
{
using json = nlohmann::ordered_json;
json j1 = {
{"numbers", {{"one", 1}}} };
json const j2 = {
{"numbers", {{"two", 2}}},
{"string", "t"} };
std::cout << "j1: " << j1.dump() << std::endl;
std::cout << "j2: " << j2.dump() << std::endl;
j1.update(j2, true); // assert triggered here
std::cout << "j1: " << j1.dump() << std::endl;
}
Error messages
Assertion failed: !check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) { return j.m_parent == this; }), file 3rd-party/nlohmann/json.hpp, line 700
Compiler and operating system
Windows 10, MSVC 2022 or gcc 13.1
Library version
3.11.3, v3.12.0, develop
Validation
- The bug also occurs if the latest version from the
develop
branch is used. - I can successfully compile and run the unit tests.