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

JsonReader fails to read string with escaped Backslashes #2370

Closed
Phibedy opened this issue Sep 23, 2014 · 5 comments
Closed

JsonReader fails to read string with escaped Backslashes #2370

Phibedy opened this issue Sep 23, 2014 · 5 comments

Comments

@Phibedy
Copy link
Contributor

@Phibedy Phibedy commented Sep 23, 2014

As I updated the project today I wondered about a json-parsing error.
Using 1.3.2-Snapshot (23.08.2014).

    String fails0 = "{text: meow\\meow}";
    String fails1 = "{text: \"meow \\ \" }";
    String fails2 = "{text: \"meow \\ \" }";
    String works = "{text: \"meow \\\"meow\\\"\" }";
    JsonReader jsonReader = new JsonReader();
    JsonValue jsonValue = jsonReader.parse(test);

fail0 worked in 1.2.0, (maybe in some 1.3.2 version but I am not sure)
fails1 throws: Illegal escaped character: \
fails2 throws: Error parsing JSON, unmatched brace.
Cheers

@NathanSweet
Copy link
Member

@NathanSweet NathanSweet commented Sep 23, 2014

JSON uses backslash for escaping.

http://json.org/

static public class Test {
    public String value;
}

public static void main (String[] args) throws Exception {
    String[] tests = {"meow\\meow", "meow \\ ", "meow \\ ", "meow \\\"meow\\\""};
    for (String s : tests) {
        Test test = new Test();
        test.value = s;
        System.out.println(test.value);
        String json = new Json().toJson(test);
        System.out.println(json);
        Test test2 = new Json().fromJson(Test.class, json);
        System.out.println(test2.value);
        if (!test2.value.equals(test.value)) throw new RuntimeException(s);
        System.out.println();
    }
}
@Phibedy
Copy link
Contributor Author

@Phibedy Phibedy commented Sep 23, 2014

I think there is a difference between your answer and my question. You are generating an object using Json and I am using the JsonParser.
As "meow \ " is an invalid json-String, I think this happens because json already knows that your value is a String and does something different.

For example, how would you create (clear text) [he\she\it] with JsonReader?
Java code:
fails: String t = "he\she\it"
creates two : String t = "he\she\it
testclass:

public static void main(String[] args) {
    String jsonWorking = "{text: meow\\meow}";//fails
    String fails001 = "{text: \"meow\\hi\" }";
    String fails01 = "{text: meow\\hi }";
    String works01 = "{text: meow\\\\hi}";
    String works1 = "{text: \"meow \\\"meow\\\"\"}";
    String fails0 = "{text: meow\\meow}";
    String fails1 = "{text: \"meow \\ \"}";
    String works = "{text: \"meow \\\\ \"}";
    String[] tests = new String[] {jsonWorking, fails0, fails1, works ,fails001,fails01, works01, works1 };
    for (String test : tests) {
        try {
            JsonReader jsonReader = new JsonReader();
            JsonValue jsonValue = jsonReader.parse(test);
            System.out.println("WORKED: " + test);
            System.out.println(jsonValue.toString());
        } catch (Exception e) {
            System.out.println("FAILED: " + test);
        }
    }
}
@dermetfan
Copy link
Contributor

@dermetfan dermetfan commented Sep 23, 2014

these String literals except works01 don't compile
(they do after your edit)

@NathanSweet
Copy link
Member

@NathanSweet NathanSweet commented Sep 23, 2014

I used Json to generate the JSON so the strings are escaped properly. "meow \ " is supposed to be invalid JSON because in JSON backslash is used to escape. "\ " (backslash and space) is invalid because the only characters allowed after backslash are those in the image I posted. To get a backslash in your JSON string you must use "meow \ ". This is further confused by describing the test using Java strings, where backslash must also be escaped. You need "meow \\ " in Java to get the string "meow \ " in JSON to get the actual string "meow \ ".

Edit: this is even worse because to get these examples to show up correctly in GitHub's markdown, I had to escape all the slashes! This is probably why @dermetfan reported that your example doesn't compile. This thread is backslash hell! I don't think there is any actual problem here though.

@Phibedy
Copy link
Contributor Author

@Phibedy Phibedy commented Sep 23, 2014

thanks made it clear 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.