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

Parse method doesn't handle newlines. #659

Closed
jaslo opened this issue Jul 18, 2017 · 24 comments
Closed

Parse method doesn't handle newlines. #659

jaslo opened this issue Jul 18, 2017 · 24 comments
Labels
solution: invalid the issue is not related to the library

Comments

@jaslo
Copy link

jaslo commented Jul 18, 2017

JSON strings containing newlines don't parse correctly with the ::parse method!

@nlohmann
Copy link
Owner

New lines must be escaped to \n:

So instead of

"This is a line
and this another"

you need to pass

"This is a line\nand this another"

@jaslo
Copy link
Author

jaslo commented Jul 18, 2017

Outside of a quoted string, newlines cause the parser to fail.

{"a":1  ,  "b": 2\n
   "c":   3\n
}
string s = "{\"a\":\"b\",\n \"c\":\"d\"}";

when passed in as a string to json::parse(s)

@gregmarr
Copy link
Contributor

You're missing a comma between 2 and "c" in the first. The second isn't valid C++. What is the actual code?

@jaslo
Copy link
Author

jaslo commented Jul 19, 2017 via email

@gregmarr
Copy link
Contributor

Have you validated the json file? What is the error message?

@jaslo
Copy link
Author

jaslo commented Jul 19, 2017

The document in the file was very simple, but how can I get an error message from the parser? Doesn't the parse method just return a json document?

@nlohmann
Copy link
Owner

If there is a parse error, parse will throw an exception which has a brief description of the error in its what argument. Could you please provide a concrete example of the failure so we can diagnose it?

@nlohmann
Copy link
Owner

@jaslo Any news on this?

@jaslo
Copy link
Author

jaslo commented Jul 21, 2017 via email

@nlohmann
Copy link
Owner

I really need to see the example. So far, I cannot reproduce any issues with newlines in the parser.

@nlohmann
Copy link
Owner

(If your example is reproducible and you cannot share the JSON file, maybe you can provide a smaller version?)

@jaslo
Copy link
Author

jaslo commented Jul 21, 2017

ok, I will post today.

@jaslo
Copy link
Author

jaslo commented Jul 21, 2017

I could not repro with a standalone app in windows, but this bug was reported on mac. I will try the test app on mac.

@gregmarr
Copy link
Contributor

That sounds like a line ending issue. If this file was written on Windows, it might have cr/nl pairs, which causes it to choke on mac.

@jaslo
Copy link
Author

jaslo commented Jul 21, 2017

Could not repro on mac either in my standalone test, with either type of line endings. I'm going to have to get the JSON file from the guy who reported the bug to me. He edited the file with "Power JSON" and then it started failing.

@nlohmann
Copy link
Owner

@jaslo That would be great.

@nlohmann
Copy link
Owner

@jaslo Any news?

@jaslo jaslo closed this as completed Jul 25, 2017
@jaslo
Copy link
Author

jaslo commented Jul 25, 2017 via email

@jaslo
Copy link
Author

jaslo commented Jul 25, 2017

Got the "bad file" and could not repro.

@nlohmann
Copy link
Owner

Thanks for checking back!

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Jul 26, 2017
@aholzinger
Copy link

Hi,

I'd like to continue this conversation. I wanted to put an SDP (https://tools.ietf.org/html/rfc4566) into a JSON variable inside a JSON string. Unfortunately the parser stumbles upon the newlines (regardless if \r\n 0x0D0A or \n 0x0A only) with a parse_error.

Here is a three liner with which I can reproduce it on VS2017:
#include <nlohmann/json.hpp>
#include

int main()
{
const std::string js("{"Sdp":"v=0\no=- 123456789 0123456789 IN IP4 192.168.1.1"}");
auto j = nlohmann::json::parse(js);
}

Shouldn't the "\n" or even "\r\n" be legal?

@aholzinger
Copy link

Sorry for the noise, I got it.

For completenes sake:
#include <nlohmann/json.hpp>
#include

int main()
{
const std::string js("{"Sdp":"v=0\no=- 123456789 0123456789 IN IP4 192.168.1.1"}");
auto j = nlohmann::json::parse(js);
}

@aholzinger
Copy link

Didn't take the second backslash. Don't know how this works on this website.
const std::string js("{"Sdp":"v=0<--backslah backslash n -->o=- 123456789 0123456789 IN IP4 192.168.1.1"}");

@t-b
Copy link
Contributor

t-b commented Sep 2, 2019

@aholzinger Use code blocks with triple backticks, see https://help.github.com/en/articles/basic-writing-and-formatting-syntax#quoting-code, to post code.

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
Projects
None yet
Development

No branches or pull requests

5 participants