-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Using fifo_map #485
Comments
Can you please provide the compiler error messages? |
From GCC:
From Clang:
|
Thanks, I shall have a look. |
I can confirm the error, but I have no idea how to fix it. I haven't tried |
Using Visual Studio C++ 2017, I get the following error: |
Hi, I think I found out the problem, I think it's because when you are declaring object_t,
now if you want to use fifo_map you can by using the follow:
please note that I haven't fully tested it, just basic example, not sure if it's the right way to do it, just an idea. Also I can offer a workaround without changing the json library itself but it's a bit more ugly and will break if you will fix it using the patch above. Let me know if you want this patch a pull request. |
Same error under VS2015. |
Related to #660 (comment): I really need to remove the hint to fifo_map from the documentation... :-( |
Hmm. This is unfortunate as I would like to use fifo for improving the readability of json formatted file.
…On 2017/09/10 23:24, Niels Lohmann wrote:
Related to #660 (comment) <#660 (comment)>:
I really need to remove the hint to fifo_map from the documentation... :-(
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#485 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ASymehOR4graqW5LASjgCRijQwJ5i-PJks5sg_E4gaJpZM4MU-ha>.
|
Hmm. This is unfortunate as I would like to use fifo for improving the human readability of json formatted file.
…On 2017/09/10 23:24, Niels Lohmann wrote:
Related to #660 (comment) <#660 (comment)>:
I really need to remove the hint to fifo_map from the documentation... :-(
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#485 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ASymehOR4graqW5LASjgCRijQwJ5i-PJks5sg_E4gaJpZM4MU-ha>.
|
The fifo_map library uses an uncommon comparator type, which needs to hold a key storage. It is not possible to solve this without breaking compatibility with one of the libraries. |
As I said before in my previous comment, there is a way to do it without a change in the libraries, it's a workaround which I don't like that much since it will break if the API will change. #include "json.hpp"
#include "fifo_map.hpp"
#include <iostream>
using namespace nlohmann;
// 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 = fifo_map<K, V, fifo_map_compare<K>, A>;
using my_json = basic_json<my_workaround_fifo_map>;
int main()
{
my_json j;
j["f"] = 5;
j["a"] = 2;
my_json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
std::cout << j << std::endl;
std::cout << j2 << std::endl;
return 0;
} output:
I don't like this workaround very much since it isn't clean and really patchy, but maybe this will give others a better idea about what to do. |
Yes, sure. I wasn't clear about that. I meant to say, it isn't possible if the goal is to allow You are absolutely right. And I think it's worth thinking about documenting your work around in the Readme. |
@Daniel599 Thanks for sharing the workaround. I shall link it in the README until we find a better way. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@Daniel599 's solution fails on this example:
I'm using GNU G++ compiler. |
Could you please provide the error message? Which version of GCC are you using? |
|
I think I found the problem:
Need to add
I hope it helps, need to make pull request to fifo_map I guess, maybe with some tests for this ctor. |
Thanks for looking into at. A PR would be greatly appreciated! |
@Daniel599 Can you please check if the issue can be closed as nlohmann/fifo_map#4 is merged? |
after the merge, @zaycakitayca 's code now works. |
Thanks a lot! |
If i define a fifo_map based json .. how can i have a method take as argument either a regular json or a fifo_map json value ?
|
it doesn't compile because they are different types. template<class JSONType>
void processJson(JSONType &j)
{
}
json j1;
my_json j2;
processJson(j1);
processJson(j2); |
I am sorry if this has been already solved, but I am trying to build specialized #include <nlohmann/json.hpp>
#include <nlohmann/fifo_map.hpp>
// for convenience
using json = nlohmann::basic_json<nlohmann::fifo_map>; and I am hit by the error already reported by @arvidsson above:
I wonder, was it resolved, or is it a know bug? If the latter, is there any other solution? |
@risa2000 it doesn't seems like you used my workaround.
|
@Daniel599 thanks for the nudge. Indeed I did not apply the "fix" as I did not understand that it was one! After following your example, it works as expected. Unfortunately, it turned out that I am stuck anyway. The reason is that I am using |
hey @nlohmann and @Daniel599 ! I am currently trying to generate JSON with ordered keys and therefore used the workaround method from @Daniel599 . I just copy-pasted your workaround code and it worked as aspected. However, if I try to use it within my base and derived classes, I get an error which I do not really understand. It seems like it fails to call the to_Json methods (because the error appears if I try to map a DerivedClass-instance (test and test2) to my_json. I have already tried the example without ordered keys (just by using json = nlohmann::json;) and it works completely fine. The keys in the output are sorted alphabetically and looks like this:
What I am trying to achieve through using the nlohmann fifo_map is to keep the insertion order and the final output therefore should look like this:
Executing the following code outputs two errors:
Please have a look at the following code: In BaseClass.h:
In DerivedClass.h:
In main.cpp:
|
Hi @theresa95, after some investigation I found out the cause of your error is due to multiple "my_json" types: one inside
|
Hey @Daniel599 , first of all, thank you for the quick answer! I edited the codesnippet of my question to fit your answer.
But I still get the same error messages in the main.cpp file: Removing the "using namespace nlohmann" in the BaseClass.h file results in other errors so I have left it there: What should I do with my_json definition and using namespace nlohmann in the main.cpp file? Thank you so much for your help in advance! |
@theresa95
note the addition of in main.cpp:
a bit more details: |
@Daniel599 That did the trick! Thank you so much! |
Working with the latest nlohmann/json.hpp and nlohmann/fifo_map.hpp, I was also seeing the add_key() problem:
This is how I'm using the library:
I found that this one-line change to json.hpp fixed the compile problem, allowing the library to preserve the insertion order of object elements:
This was using nlohmann-json-dev 2.1.1-1.1 and the latest commit of fifo_map:
With this change, the default (std::less) seems to work as before, but I haven't done any deep testing or tried this with other compilers, so this could be a works-for-me. FWIW... |
Compile and system info for my previous comment:
HTH. |
@ccpearson Thanks for sharing. I am confused - is |
Well, it didn't compile for me, using the version of g++ I mentioned. But I installed the json library as an apt package (for Debian), rather than pulling the source from GitHub. Maybe the package repo hasn't been updated? IDK.
…-------- Original Message --------
On Jul 30, 2021, 5:55 AM, Niels Lohmann < ***@***.***> wrote:
@ccpearson Thanks for sharing. I am confused - is fifo_map broken with the latest release? We use object_comparator_t now.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
[ { ***@***.***": "http://schema.org", ***@***.***": "EmailMessage", "potentialAction": { ***@***.***": "ViewAction", "target": "[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)", "url": "[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { ***@***.***": "Organization", "name": "GitHub", "url": "https://github.com" } } ]
|
I'll just pull from GitHub and find out...
…-------- Original Message --------
On Jul 30, 2021, 9:45 AM, Chris Pearson < ***@***.***> wrote:
Well, it didn't compile for me, using the version of g++ I mentioned. But I installed the json library as an apt package (for Debian), rather than pulling the source from GitHub. Maybe the package repo hasn't been updated? IDK.
-------- Original Message --------
On Jul 30, 2021, 5:55 AM, Niels Lohmann < ***@***.***> wrote:
@ccpearson Thanks for sharing. I am confused - is fifo_map broken with the latest release? We use object_comparator_t now.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
[ { ***@***.***": "http://schema.org", ***@***.***": "EmailMessage", "potentialAction": { ***@***.***": "ViewAction", "target": "[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)">[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)", "url": "[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)">[https://github.com](<a href=)/nlohmann/json/issues/485#issuecomment-889873443">#485 (comment)", "name": "View Issue" }, "description": "View this Issue on GitHub", "publisher": { ***@***.***": "Organization", "name": "GitHub", "url": "https://github.com" } } ]
|
|
The document says "you can specialize the object type with containers like
tsl::ordered_map
ornlohmann::fifo_map
". I had a try with simple code like:It fails the compilation noisily. Would you please fix the issue, and/or clarify in the document how this should be done?
The text was updated successfully, but these errors were encountered: