-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot assign from ordered_json vector<CustomStruct> to value in not ordered json #2528
Comments
It's a bug in your own code. Actually, json and ordered_json are convertible to each other so you can construct ordered_json from your type and then use it as a field in json, But it's not scalable. int main() {
std::vector<releaseInfo> test;
ordered_json children = test;
json parent;
parent["test"] = children;
return 0;
} See a possible solution: #include <nlohmann/json.hpp>
using nlohmann::json;
using ordered_json = nlohmann::ordered_json;
#define _NLOHMANN_DEFINE_TYPE_INTRUSIVE(JsonType, Type, ...) \
friend void to_json(JsonType& nlohmann_json_j, const Type& nlohmann_json_t){ \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
}
class releaseInfo {
public:
uint64_t id = 0;
std::string key;
_NLOHMANN_DEFINE_TYPE_INTRUSIVE(nlohmann::json, releaseInfo, id, key);
_NLOHMANN_DEFINE_TYPE_INTRUSIVE(nlohmann::ordered_json, releaseInfo, id, key);
};
int main() {
std::vector<releaseInfo> test;
json parent;
parent["test"] = test;
return 0;
} As I can see NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE both don't work with ordered_json. And moreover, they don't work with any other basic_json overload. From the nlohmann/json point of view, it's kind of lack of feature. #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_2(Type, ...) \
template<class JsonType> friend void to_json(JsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
template<class JsonType> friend void from_json(const JsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_2(Type, ...) \
template<class JsonType> void to_json(JsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
template<class JsonType> void from_json(const JsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } Need to test new macroses more though. I wrote them in a few minutes. They may break ODR or something. |
@kotori2 you can be able to fix the bug in order to debug it |
@YarikTH Thanks for your kind and thorough explanation.
I think my memory just corrupted...
Actually it's kinda weird to me. Simply value assigning will work between json and ordered_json.
But when it comes to function calls, it doesn't work. |
I'm not the author, but it seems it is. #include <nlohmann/json.hpp>
#include <iostream>
using nlohmann::json;
using ordered_json = nlohmann::ordered_json;
#define _NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
friend void to_json(ordered_json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
friend void from_json(const ordered_json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
class releaseInfo {
public:
uint64_t id = 0;
std::string key;
std::string author;// field especialy intended to be alphabetically lower than others
_NLOHMANN_DEFINE_TYPE_INTRUSIVE(releaseInfo, id, key, author);
};
int main() {
std::vector<releaseInfo> test(1, releaseInfo{});
ordered_json children = test;
std::cout << "ordered children: \n" << children.dump(4);
std::cout << "\n\n";
json parent;
parent["test"] = children;
std::cout << "unordered parent: \n" << parent.dump(4);
return 0;
} Output:
As you can see, values added in ordered_json being used in json become reordered. |
What is the issue you have?
I created a custom struct in unordered_json with vector, then assign it to an ordered json, it will throw error.
Please describe the steps to reproduce the issue.
Check the snippet below.
Actually it works if both of them are ordered json or unordered json, or I removed the vector.
Can you provide a small but working code example?
I defined my own
NLOHMANN_DEFINE_TYPE_INTRUSIVE
just to conveniently define them in unordered json.What is the expected behavior?
No error happened.
And what is the actual behavior instead?
Which compiler and operating system are you using?
Apple clang version 12.0.0 (clang-1200.0.32.27)
Darwin Hackintosh 20.1.0 Darwin Kernel Version 20.1.0: Sat Oct 31 00:07:11 PDT 2020; root:xnu-7195.50.7~2/RELEASE_X86_64 x86_64
Which version of the library did you use?
develop
branchIf you experience a compilation error: can you compile and run the unit tests?
The text was updated successfully, but these errors were encountered: