Skip to content
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

Implicit conversion from array to object #425

Closed
hxingpax opened this issue Jan 12, 2017 · 1 comment
Closed

Implicit conversion from array to object #425

hxingpax opened this issue Jan 12, 2017 · 1 comment
Assignees

Comments

@hxingpax
Copy link

hxingpax commented Jan 12, 2017

In the README I saw:

// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };

But on my Ubuntu 1604 with gcc 5.4 I got "{"currency":"USD","value":42.99}"

After I change "value" into a numeric, e.g.

json array_not_object = { json::array({"currency", "USD"}), json::array({4, 42.99}) };

I got "[["currency","USD"],[4,42.99]]"

Is this a bug? or feature?

@nlohmann
Copy link
Owner

You may want to use array as outmost function:

json array_not_object2 = json::array({{"currency", "USD"}, {"value", 42.99}});
std::cerr << array_not_object2 << std::endl;

yielding [["currency","USD"],["value",42.99]].

array is just a means to tell the library not to try to convert a list of pairs to an object even though it could.

@nlohmann nlohmann self-assigned this Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants