ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
732 stars 186 forks source link

"Expected valid escape sequence" for Windows Paths #99

Open rhuankarlus opened 6 years ago

rhuankarlus commented 6 years ago

When I try to parse a String that contains a Windows Path like "C:\MyPath\Wtv Folder" I get a Expected valid escape sequence error.

Example:

{
      "a": "foo",
      "b": "bar",
      "c": 1526,
      "d": "C:\\MyPath\\Wtv Folder"
}

I realized that the library is trying to find out which is the scape inserted but I want to avoid this behavior.

Do you guys noticed that?

blongstreth commented 6 years ago

Well, depends on what you are asking. If I treat your example document as a script file and read it in (or whatever), it works just fine (i.e. no errors). If I try to translate that to a Java String directly, then you need to escape it further, otherwise you will get the error you mentioned.

This works as expected:

final String jsonText = "{\n\t\"a\": \"foo\",\n\t\"b\": \"bar\",\n\t\"c\": 1526,\n\t\"d\": \"C:\\\\MyPath\\\\Wtv Folder\"\n}";

final JsonValue jsonValue = Json.parse(jsonText);

System.err.println("jsonText: " + jsonText);
System.err.println("jsonValue: " + jsonValue);

This does not work which is expected:

final String jsonText = "{\n\t\"a\": \"foo\",\n\t\"b\": \"bar\",\n\t\"c\": 1526,\n\t\"d\": \"C:\\MyPath\\Wtv Folder\"\n}";

final JsonValue jsonValue = Json.parse(jsonText);

System.err.println("jsonText: " + jsonText);
System.err.println("jsonValue: " + jsonValue);

As a sanity check, you can try one of the many JSON tools, linters. Here are a few: JSON Checker JSONLint