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

Value with spaces cannot be parsed #2781

Closed
oleksandrlynda opened this issue May 19, 2021 · 5 comments
Closed

Value with spaces cannot be parsed #2781

oleksandrlynda opened this issue May 19, 2021 · 5 comments
Labels
solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@oleksandrlynda
Copy link

I cannot parse simple JSON if it has spaces in the value:
{"testKey":"Value with spaces"}

I have exception:
terminate called after throwing an instance of 'nlohmann::detail::parse_error'
what(): [json.exception.parse_error.101] parse error at line 1, column 18: syntax error while parsing value - invalid string: missing closing quote; last read: '"Value'

Version: 3.9.1

@oleksandrlynda
Copy link
Author

prov.txt

The file

@oleksandrlynda
Copy link
Author

Also I can create this json using this library but cannot read it.

My system is: Linux o3 5.8.0-53-generic #60~20.04.1-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux

@oleksandrlynda
Copy link
Author

Data is parsed like that:

        std::ifstream i(path);
        std::string data;
        i >> data;
        nlohmann::json rootJson = nlohmann::json::parse(data);

@nlohmann
Copy link
Owner

Your code does not read the complete file into the string, but only until the first space is read. This is not related to this library.

If I output data before parsing, I get the following output:

{"testKey":"Value

Then, the error message makes sense, because indeed a closing quote is missing.

The following works:

#include <fstream>
#include <iostream>
#include "json.hpp"

int main()
{
    std::string path = "/Users/niels/Downloads/prov.txt";
    std::ifstream i(path);
    nlohmann::json rootJson = nlohmann::json::parse(i);
    std::cout << rootJson << std::endl;
}

Output:

{"testKey":"Value with spaces"}

@nlohmann nlohmann added solution: invalid the issue is not related to the library solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels May 19, 2021
@oleksandrlynda
Copy link
Author

Great, thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library 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