Json: Yet another ordering question: char * and parse()

Created on 23 Aug 2018  路  4Comments  路  Source: nlohmann/json

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!

question proposed fix

All 4 comments

Could you post a small example with the error message?

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"}

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

Ahhhh!!!!

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

Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Prati369 picture Prati369  路  4Comments

MariaRamos89 picture MariaRamos89  路  4Comments

SimplyLiz picture SimplyLiz  路  3Comments

zkelo picture zkelo  路  3Comments

alienzj picture alienzj  路  4Comments