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

Treating floating point as string #1627

Closed
MarkoPaul0 opened this issue Jun 6, 2019 · 3 comments
Closed

Treating floating point as string #1627

MarkoPaul0 opened this issue Jun 6, 2019 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@MarkoPaul0
Copy link

Hey everyone I have been trying to figure out if there was a way for me to access the underlying "raw text" of a numeric value.
Let's say I have the following json:

{
  "some_number": 123443567.8982593898749283794837298749832798798
}

Here I want to treat some_number as a "bunch of digits", meaning the node is still of numeric type but I don't want to rely on any parsing capabilities from this library. Would it be possible for me to get the underlying array of characters that is contained in the json file for a given numeric node?
In other words, I would like to do something like this

std::string raw_value = some_number_node.rawDump();
std::cout << "Raw value: {" << raw_value << "}" << std::endl;

Which would print:

{123443567.8982593898749283794837298749832798798}

Is this doable?

Thanks a lot!

@nlohmann
Copy link
Owner

You could achieve this via the SAX interface (see https://nlohmann.github.io/json/structnlohmann_1_1json__sax.html). The parser will call function number_float (https://nlohmann.github.io/json/structnlohmann_1_1json__sax_ae7c31614e8a82164d2d7f8dbf4671b25.html#ae7c31614e8a82164d2d7f8dbf4671b25) whenever it encounters a floating-point number. Beside the value, it also passes the raw string from the lexer.

With this, you could implement your own version of a SAX DOM parser (see https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/json_sax.hpp#L193) and pass the passed string to handle_value rather than the floating-point value val.

You would then need to create an object of this SAX parser and pass it to sax_parse (see https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a8a3dd150c2d1f0df3502d937de0871db.html#a8a3dd150c2d1f0df3502d937de0871db).

Please let me know if you need further assistance.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 22, 2019
@nlohmann
Copy link
Owner

nlohmann commented Jul 3, 2019

@MarkoPaul0 Did you have a chance to try the approach I described?

@MarkoPaul0
Copy link
Author

@nlohmann thank you for your reply, and sorry for not replying in a timely manner. Unfortunately I haven't tried, but I think I will git it a try when I get back to this issue. For now I decided I was fine with the current behavior.
If and when I do try it out I will make sure to update this thread.

Thanks again @nlohmann and have a good one!

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