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

Does json support converting an instance of a struct into json string? #1143

Closed
GingerMoon opened this issue Jun 24, 2018 · 7 comments
Closed
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@GingerMoon
Copy link

Does json support converting an instance of a struct into json string?
And if yes, can the order of the fields be kept in the order of the declaration in the struct?
For example,
struct S {
int Z;
int A;
};
S s = {1,2};

I tried to read the doc but not able to find the answer.
The expect json string is:
{"Z":1, "A":2}

Any sample code would be much appreciated.

Some background information for this question:
I am developing a Qt client for a blockchain. I need to sign for a transaction struct json. But the json fields are in the order of alphabet, however on the server side, golang json packeg, the order is the declaration order of the struct fields. Hence the verification for the signature failed.

@nlohmann
Copy link
Owner

Do you really mean reapidjson?

@GingerMoon GingerMoon changed the title Does reapidjson support converting an instance of a struct into json string? Does json support converting an instance of a struct into json string? Jun 24, 2018
@GingerMoon
Copy link
Author

Thank you for the quick response and sorry for the typo.
I really mean nlohmann/json :) .
I did ask a similar question for rapidjson...
Tencent/rapidjson#1299

@nlohmann
Copy link
Owner

From the README:

  • By default, the library does not preserve the insertion order of object elements. This is standards-compliant, as the JSON standard defines objects as "an unordered collection of zero or more name/value pairs". If you do want to preserve the insertion order, you can specialize the object type with containers like tsl::ordered_map (integration) or nlohmann::fifo_map (integration).

@nlohmann
Copy link
Owner

What I mean is that the library does support serializing user-defined types to JSON (https://github.com/nlohmann/json#arbitrary-types-conversions), but does not preserve the order of object keys by default. This can be achieved by the containers linked in the README snippet above.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 24, 2018
@GingerMoon
Copy link
Author

I did get similar information via searching, and I also remember that you mentioned in one of your reply that you will not recommend fifo_map.
It seemed that I missed these important information. Thank you very much!
I am really appreciated!

@nlohmann
Copy link
Owner

Using the fifo_map header is not really tested and there may be issues with that. But the more people try it, the better we know ;-)

@nlohmann
Copy link
Owner

If you experience any issues, please let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants