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

Yet another ordering question: char * and parse() #1209

Closed
asmaloney opened this issue Aug 22, 2018 · 4 comments
Closed

Yet another ordering question: char * and parse() #1209

asmaloney opened this issue Aug 22, 2018 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@asmaloney
Copy link

I've looked at the docs, the issues, and how to use the FIFO map. These examples are creating the JSON through assignment to the new type.

I have my JSON data in a regular old char *. How can I use the FIFO trick to keep it ordered when creating the nlohmann::json?

The only way I see to get from my char * to nlohmann::json is nlohmann::json::parse(), but then the FIFO map doesn't work - or am I missing something?

Thanks!

@nlohmann
Copy link
Owner

Could you post a small example with the error message?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Aug 23, 2018
@asmaloney
Copy link
Author

Sure. It doesn't produce an error - it just doesn't keep it sorted because parse doesn't know about the my_json type::

// A workaround to give to use fifo_map as map, we are just ignoring the 'less' compare
template<class K, class V, class dummy_compare, class A>
using my_workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using my_json = nlohmann::basic_json<my_workaround_fifo_map>;

...
{
   // The data comes from some other source as a char *
   const char  *cData = R"({ "foo":"bar", "blat" : { "ding" : {}, "baz" : {} } })";

   // How do I keep it ordered when deserializing the json?
   my_json object = nlohmann::json::parse( cData );

   std::cout << object.dump() << std::endl;
}

Result:

{"blat":{"baz":{},"ding":{}},"foo":"bar"}

@gregmarr
Copy link
Contributor

You need to use my_json::parse() instead of nlohmann::json::parse().

@asmaloney
Copy link
Author

Ahhhh!!!!

I'm obviously not a template guy, but I see it now :-)

Thank you.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels Aug 23, 2018
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

3 participants