-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
mongodb: nan, inf #1599
Comments
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. |
The library does not support extensions like this. As NAN or INFINITY are not part of the JSON specification, they will yield parse errors. There is no elegant way in changing this other than touching the lexer and parser I'm afraid. |
One dirty hack is to replace @nlohmann What is your thoughts on providing an option so to allow and serialize/deserialize these values? I understand that these are not supported in JSON standard and the default behavior should be to reject them, but given that they are common is some use-cases, supporting them (with an opt-in option) could be very beneficial to the users since there is no clean workaround for them. |
I honestly do not like extending JSON in any way (even with an option). |
"replace NaN, inf, -inf in the original string" - this is what I have done. It works in my project because I don't have any strings which contains "NaN, inf". But it is just so ugly and error prone :( But I was asking myself: if I have a 'double' value in my C++ code, and the value is allowed to be a finite, +/- inf real or NAN, how would I store it in JSON?! To build a proper JSON object I probably need to introduce a new structure to hold inf/NAN values. And an array of real numbers can easily become:
Anyway, the core of the problem is incomplete JSON specs on how to represent NAN and Inf. The library (nlohmann::json) may do something to help, but if it is too difficult... Ok, let it be... |
@WarShoe
That's exactly the issue! While
Your proposal about encoding the NaN values as objects seems a good approach since the resulting string remains as a valid JSON string. |
@nlohmann I imagine there are three approaches to support such customization (in order of discreetness):
Approach 1 is not desirable since it impacts the performance of lexer/parser by introducing more branching. Approach 2 is less intrusive and doesn't impact the performance for the users who don't enable the option. It adds more complexity to the current lexer/parser code though. For Approach 3, the user has to do some work but it is a general solution and gives more flexibility since the whole lexer can be substituted. This can be helpful in other circumstances too. While Approach 1 is off the table, I wonder whether Approach 2 or 3 can be implement without any negative impact either on the performance or design of the library. Nevertheless, I understand that regardless of which approach we choose, there still could be a considerable amount of work that should be done in other places too (e.g. handling |
I do not like any extension of this library that makes it possible to process non-conforming JSON text. The reasons are similar to those regarding comments, see https://github.com/nlohmann/json#comments-in-json. If anything is possible with little effort, I would however reconsider it. |
I don't think that support for comments (major change) and adding 'nan', 'inf' keywords (minor, the library already has the relevant code) are comparable. I also think it should be quite simple (for the library maintainer) to do. When I looked at the code, my starting point was here:
But then I decided to simplify my life, and instead of changing the library code, I have started this thread :) |
Yes, you're right, and I forgot how Python's
I'll check. |
While it is easy to extend the internal parser/lexer to cope with NaN and the like, adding another bool to the
So maybe a |
That would be great! This also makes the addition of parameters easier in the future. The code on the call site will be also more readable. I believe the same thing can be done for |
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. |
Stale, old, but a rationale for needing to store |
I have a document from mongodb which contains an array of floats: "[123, nan, inf, -inf]". It is not standard JSON, so the library correctly throws json.exception.parse_error.101
But the array has a meaning, it contains a finite float 123, not-a-number value, and +/- infinity. So the array can be converted into std::vector.
My question is: how can I achieve that in a most elegant way?! Probably the best would be to add user-defined literals (like true,false,null) which are automatically converted into floats (macros NAN and INFINITY). But I don't see that the library allows that...
Thanks a lot in advance!
The text was updated successfully, but these errors were encountered: