-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
std::pair wrong serialization #2655
Comments
MUORI COGLIONE
Il mar 23 feb 2021, 10:37 Tiregram <notifications@github.com> ha scritto:
… What is the issue you have?
My pair is not serialized as espected and a
Please describe the steps to reproduce the issue.
std::pair<std::pair<std::string, int>, std::pair<std::string, float>> p = {
{"a", 2}, {"a", 2}};
nlohmann::json j;
j = p;
std::cout << j << "\n"; // i get {a:2} but i want [["a":2],["a",2]]
What is the expected behavior?
i want [["a":2],["a",2]] and not {"a":2}
And what is the actual behavior instead?
i get {"a":2} and if i serialized/deserialized, I lost my data and I get a
execution error:
[json.exception.type_error.304] cannot use at() with object.
Which compiler and operating system are you using?
.
- Compiler: gcc (GCC) 10.2.0
- Operating system: Linux 5.4.96-1-lts
Which version of the library did you use?
- [ x] latest release version 3.9.1
If you experience a compilation error: can you compile and run the unit
tests </~https://github.com/nlohmann/json#execute-unit-tests>?
- yes
- no - please copy/paste the error message below
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2655>, or unsubscribe
</~https://github.com/notifications/unsubscribe-auth/AOWD6E7XX6677OSOB4HFJ2DTANZMFANCNFSM4YCENEMQ>
.
|
This is working as intended, objects are preferred over arrays when both are possible to create from the given data. If you explicitly want to create an array, use #include <iostream>
#include "json.hpp"
int main() {
nlohmann::json j;
j = nlohmann::json::array({{"a", 2}, {"a", 2}});
std::cout << j << std::endl;
} |
not this is a bug due to the utilization of the string as first argument In this situation json will consider it as a dict and not as a array. |
with this bug a serialisation/deserialisation will failled and not provide the same c++ structure. |
Lists of pairs create JSON objects if the first element is a string. This should be documented properly as this example shows that the results may be surprising. However, changing this behavior would be a breaking change. |
This problematic for us that have complex structures, I have a mechanism that exports a std::variant of std::tuples of various lengths, something like: std::variant< std::tuple<int,int,int>, std::tuple< std::string, int >, std::tuple< T, U, K, M >, .... >; The second tuple will get serialized differently, which is problematic and counterintuitive behavior. (But I understand the backwards compability concerns) The real problem is that even though I force it to be arrays, it is still converted to object later. That means that I can't prevent this from happening. (I would end up with one tuple being converted to object and rest being arrays) Thanks to people on irc ##C++-general@freenode I have minimal example of the problematic situation: #include "nlohmann/json.hpp"
#include <iostream>
#include <tuple>
using json = nlohmann::json;
struct T {
std::tuple< std::string, int > tpl;
};
void to_json(json & j, const T & val )
{
j = json::array({ std::get<0>(val.tpl), std::get<1>(val.tpl) });
std::cout << "tmp res: " << j << "\n";
}
int main()
{
T item{std::tuple< std::string, int >{"test", 123}};
std::cout << json{item}.dump() << '\n';
} This outputs array in Is there some way to prevent this from happening? |
After some discusison on IRC, there is one way to avoid this: std::cout << json{item}.dump() << '\n';
std::cout << json(item).dump() << '\n'; The () constructor does not convert the item into an object, {} does. |
I added a FAQ entry for this bug: https://json.nlohmann.me/home/faq/#brace-initialization-yields-arrays |
What is the issue you have?
My pair is not serialized as espected and a
Please describe the steps to reproduce the issue.
What is the expected behavior?
i want [["a":2],["a",2]] and not {"a":2}
And what is the actual behavior instead?
i get {"a":2} and if i serialized/deserialized, I lost my data and I get a execution error:
[json.exception.type_error.304] cannot use at() with object.
Which compiler and operating system are you using?
.
Which version of the library did you use?
If you experience a compilation error: can you compile and run the unit tests?
The text was updated successfully, but these errors were encountered: