Closed FeignClaims closed 1 year ago
direct initialization demo
#include <iostream> #include <ostream> #include <nlohmann/json.hpp> auto main() -> int { std::map<std::string, std::string> map{{"key", "val"}, {"key2", "val2"}}; nlohmann::json json{map}; std::cout << json << '\n'; // output: [{"key":"val","key2":"val2"}] map = json; // exception: [json.exception.type_error.302] type must be object, but is array }
copy initialization demo
#include <iostream> #include <ostream> #include <nlohmann/json.hpp> auto main() -> int { std::map<std::string, std::string> map{{"key", "val"}, {"key2", "val2"}}; nlohmann::json json(map); std::cout << json << '\n'; // output: {"key":"val","key2":"val2"} map = json; // ok }
I think this behaviour is easy to make mistakes, so probably not an intentional design.
refer to description
The direct initialization code should behave as the same as the copy initialization one, which enables converting json back to std::map.
json
std::map
No response
x86-64 clang (trunk), x86-64 gcc (trunk)
35c0b3e54ce99716bf2a6bb6bdbd346f3e6d398b
develop
This is the very first item in the FAQ. https://json.nlohmann.me/home/faq/
I see, only searched in issue, sorry for the bother
Description
direct initialization demo
copy initialization demo
I think this behaviour is easy to make mistakes, so probably not an intentional design.
Reproduction steps
refer to description
Expected vs. actual results
The direct initialization code should behave as the same as the copy initialization one, which enables converting
json
back tostd::map
.Minimal code example
No response
Error messages
No response
Compiler and operating system
x86-64 clang (trunk), x86-64 gcc (trunk)
Library version
35c0b3e54ce99716bf2a6bb6bdbd346f3e6d398b
Validation
develop
branch is used.