fix: add serialVersionUID to JSONArray in fastjson1-compatible for Java serialization compatibility - #7698
Conversation
wenshao
left a comment
There was a problem hiding this comment.
Reviewed. Suggestions are inline.
— qwen3.8-max-preview via Qwen Code /review
|
awesome |
wenshao
left a comment
There was a problem hiding this comment.
Reviewed. 1 Suggestion-level finding(s) could not be anchored to a changed line and were dropped; nothing further to act on here.
— qwen3.7-max via Qwen Code /review
…va serialization compatibility (#7687)
46efe2f to
2c995ce
Compare
wenshao
left a comment
There was a problem hiding this comment.
— qwen3.8-max-preview via Qwen Code /review
Code ReviewOverviewAdds Verdict: correct and minimal — LGTM. Notes below are mostly about test strength and a residual (pre-existing) interop gap. Correctness — verified end-to-endI checked the actual interop scenario rather than relying on the round-trip tests:
Test coverage — the main suggestion
A stronger guard is a golden byte stream captured from real fastjson 1.x — it pins the whole serialized form (field names/types), not just the UID, so a future refactor that renames @Test
public void test_readFastjson1Bytes() throws Exception {
// produced by fastjson 1.2.83: JSON.parseArray("[1,\"hello\",{\"id\":123}]")
String base64 = "rO0ABXNyAB5jb20uYWxpYmFiYS5mYXN0anNvbi5KU09OQXJyYXkAAAAAAAAAAQIAAUwABGxpc3R0"
+ "ABBMamF2YS91dGlsL0xpc3Q7eHBzcgATamF2YS51dGlsLkFycmF5TGlzdHiB0h2Zx2GdAwABSQAE"
+ "c2l6ZXhwAAAAA3cEAAAAA3NyABFqYXZhLmxhbmcuSW50ZWdlchLioKT3gYc4AgABSQAFdmFsdWV4"
+ "cgAQamF2YS5sYW5nLk51bWJlcoaslR0LlOCLAgAAeHAAAAABdAAFaGVsbG9zcgAfY29tLmFsaWJh"
+ "YmEuZmFzdGpzb24uSlNPTk9iamVjdAAAAAAAAAABAgABTAADbWFwdAAPTGphdmEvdXRpbC9NYXA7"
+ "eHBzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hv"
+ "bGR4cD9AAAAAAAAMdwgAAAAQAAAAAXQAAmlkc3EAfgAFAAAAe3h4";
byte[] bytes = java.util.Base64.getDecoder().decode(base64);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
JSONArray array = (JSONArray) in.readObject();
assertEquals(JSON.parseArray("[1,\"hello\",{\"id\":123}]"), array);
}I ran this against the PR branch — it passes, and fails with Residual limitation (pre-existing, out of scope but worth knowing)The reverse direction is still broken for arrays built via the default constructor: JSONArray a = new JSONArray(); // list = com.alibaba.fastjson2.JSONArray
a.add(123);
// deserialize in real fastjson 1.2.83:
// java.lang.ClassNotFoundException: com.alibaba.fastjson2.JSONArrayBecause Style / minor
Performance / security
|
Issue
Fixes #7687
Problem
com.alibaba.fastjson.JSONArrayin the fastjson1-compatible module implementsSerializablebut was missing an explicitserialVersionUID. Java auto-computes a UID from the class structure, which doesn't match the original fastjson 1.x value (1L). This causesInvalidClassExceptionwhen deserializing objects previously serialized with fastjson 1.x:Fix
Added
private static final long serialVersionUID = 1L;toJSONArray, matching:JSONArrayvalueJSONObjectclass in the same module (which already declaresserialVersionUID = 1L)Test
Added
JSONArrayTest_readObjectverifying:serialVersionUIDequals1L(viaObjectStreamClass.lookup)