Skip to content

[BUG] parsing a string containing escaped double quotes during polymorphic deserialization #7634

@OnceEnchanted

Description

@OnceEnchanted

Problem Description

  • Parsing a string containing escaped double quotes throws exception during polymorphic deserialization if the string comes before a field defining the object's type.

Environment Information

  • JDK: Eclipse Temurin 25.0.1
  • Version Information: Fastjson2 2.0.61

Reproduction Steps

Example code:

@JSONType(typeKey = "objectType")
sealed interface Animal {
    @JSONType(typeName = "cat")
    record Cat(String field) implements Animal {
    }
}

String json = "{\"field\": \"some\\\"text\", \"objectType\": \"cat\"}";
// Actual JSON to parse: {"field": "some\"text", "objectType": "cat"}
JSON.parseObject(json, Animal.class); // throws com.alibaba.fastjson2.JSONException

Output:

com.alibaba.fastjson2.JSONException: illegal fieldName input116, offset 18, character t, line 1, column 18, fastjson-version 2.0.61 {"field": "some\"text", "objectType": "cat"}

	at com.alibaba.fastjson2.JSONReader.readFieldNameHashCodeError(JSONReader.java:6885)
	at com.alibaba.fastjson2.JSONReaderASCII.readFieldNameHashCode(JSONReaderASCII.java:129)
	at com.alibaba.fastjson2.reader.ObjectReaderNoneDefaultConstructor.readObject(ObjectReaderNoneDefaultConstructor.java:310)
	at com.alibaba.fastjson2.reader.ORG_1_1_Cat.readObject(Unknown Source)
	at com.alibaba.fastjson2.reader.ObjectReaderSeeAlso.readObject(ObjectReaderSeeAlso.java:312)
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:992)

Expected Correct Result

String in the field must be deserialized correctly as: some"text

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions