You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Typed Map with bean values throws on inner @type when auto type is disabled #7970
A typed map with bean values throws when the JSON contains an inner @type and auto type is off.
Repro on current main (fastjson2 2.0.64, JDK 21):
classVal {
publicStringname;
}
classParamVal {
publicMap<String, Val> varsMap;
}
Stringjson = "{\"varsMap\":{\"@type\":\"java.util.LinkedHashMap\",\"1\":{\"name\":\"x\"}}}";
// auto type not enabled: no SupportAutoType feature, no autoTypeFilterJSON.parseObject(json, ParamVal.class);
Result:
com.alibaba.fastjson2.JSONException: expect ':', but 44, offset 21, character ", line 1, column 21
at com.alibaba.fastjson2.JSONReaderASCII.readFieldNameHashCodeE(JSONReaderASCII.java:308)
at com.alibaba.fastjson2.reader.ORG_3_1_Val.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:429)
Why it surprises me
The same JSON parses fine in three neighbouring cases:
untyped map: JSON.parseObject(json) -> {"varsMap":{"@type":"java.util.LinkedHashMap","1":{"name":"x"}}}, the @type entry is kept as a normal key.
Map<String, String>: keeps @type as a key, no error.
Map<String, Object>: keeps @type as a key, no error.
Only when the value type is a bean does it blow up. My guess: auto type is off, so the reader skips the @type key, but the string value "java.util.LinkedHashMap" is still handed to the bean reader, which expects { and a field name, and dies on the comma.
What I would expect
With auto type off, an inner @type entry should be skipped like any other unknown metadata, not turn into an exception. Either drop it, or keep it as a plain key the way the untyped map does.
Related
#7964 fixes the opposite side: auto type ON plus keyType == String used to ignore the inner @type. This case is auto type OFF, which that PR does not touch.
I ran one more case: Map<String, Val> where Val can be built from a string through @JSONCreator. It still fails. But so does the same map with a plain string value and no @type in the JSON at all:
JSON.parseObject("{\"varsMap\":{\"1\":\"hello\"}}", ParamValFromString.class);
// JSONException: expect ':', but 125, offset 17
So the error has nothing to do with @type. A map whose value type is a bean cannot read a bare string, and the @type string is just one of those strings.
The policy is consistent: with auto type off, @type is treated as a normal entry. String and Object value types can hold it; a bean value type cannot, so it throws. That is a type mismatch, not an @type bug.
Activity
liango2 commentedon Nov 25, 2026
Closing this. My analysis was wrong.
I ran one more case:
Map<String, Val>whereValcan be built from a string through@JSONCreator. It still fails. But so does the same map with a plain string value and no@typein the JSON at all:So the error has nothing to do with
@type. A map whose value type is a bean cannot read a bare string, and the@typestring is just one of those strings.The policy is consistent: with auto type off,
@typeis treated as a normal entry.StringandObjectvalue types can hold it; a bean value type cannot, so it throws. That is a type mismatch, not an@typebug.Sorry for the noise.