json: json.update and vectordoes not work with ordered_json

Hi,

I am trying to convert my code from json to ordered_json, but I get some problems.

    ordered_json jsonAnimals = {
            {"animal", "dog"}};

    ordered_json jsonCat = {
            {"animal", "cat"}};

    jsonAnimals.update(jsonCat);

I get the same problem with the following code (which works with json)

    std::vector<std::pair<std::string, int64_t>> intData = {std::make_pair("aaaa", 11),
                                                            std::make_pair("bbb", 222)};

    ordered_json jsonObj;

    for (const auto& data : intData)
    {
        jsonObj[data.first] = data.second;
    }

Both generate the same error:

In instantiation of ‘void nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::update(nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::const_reference) [with ObjectType = nlohmann::ordered_map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::const_reference = const nlohmann::basic_json<nlohmann::ordered_map>&; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value_type = nlohmann::basic_json<nlohmann::ordered_map>]’:
/home/tawfic/CLionProjects/untitled2/main.cpp:575:42:   required from here
/home/tawfic/Desktop/fpc/json.hpp:22178:50: error: cannot bind rvalue reference of type ‘std::__cxx11::basic_string<char>&&’ to lvalue of type ‘const key_type’ {aka ‘const std::__cxx11::basic_string<char>’}
             m_value.object->operator[](it.key()) = it.value();
In file included from /home/tawfic/CLionProjects/untitled2/main.cpp:562:
/home/tawfic/Desktop/fpc/json.hpp:16436:8: note:   initializing argument 1 of ‘T& nlohmann::ordered_map<Key, T, IgnoredLess, Allocator>::operator[](Key&&) [with Key = std::__cxx11::basic_string<char>; T = nlohmann::basic_json<nlohmann::ordered_map>; IgnoredLess = std::less<void>; Allocator = std::allocator<std::pair<const std::__cxx11::basic_string<char>, nlohmann::basic_json<nlohmann::ordered_map> > >]’
     T& operator[](Key&& key)

Should I do something special if I convert from json to ordered_json?

Thank you 👍

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

I don’t have an issue for executing all tests for ordered_json as well. Given the large amount of tests, this would be unfeasible. What I started to do instead is to make sure ordered_map implements the same interface as std::map does which is the default type for JSON objects. Then I can make sure the future won’t introduce compilation errors as it did in your case.

Right now, #2319 implements all calls to std::map functions I could find in the code. I will work to complete them. However, I am currently facing a nasty MinGW bug which I will need some time to circumvent:

Fatal error: can't write 310 bytes to section .text of test/CMakeFiles/test-regression.dir/src/unit-regression.cpp.obj: 'File too big'

FYI: If you want to support me and my work on this project, you can become a sponsor on GitHub, or used Paypal.

Sorry, typo. Now I can reproduce the error.

There seems to be a missing operator in nlohmann::ordered_map. I’ll have a look.

@nlohmann Maybe it’s a problem in your example? using ordered_json = nlohmann::json; should be using ordered_json = nlohmann::ordered_json; This is how I am reproducing it.

So I am using the released version: https://github.com/nlohmann/json/releases/download/v3.9.0/json.hpp