Skip to content

fix: add setter for FastjsonConfig.writer/readerContext, for issue #4000#4008

Open
jujn wants to merge 3 commits into
alibaba:mainfrom
jujn:fix_4000
Open

fix: add setter for FastjsonConfig.writer/readerContext, for issue #4000#4008
jujn wants to merge 3 commits into
alibaba:mainfrom
jujn:fix_4000

Conversation

@jujn

@jujn jujn commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

允许用户像 1.x 中那样限定自定义序列化器影响范围,如下所示:

        FastJsonConfig config = new FastJsonConfig();
        
        ObjectWriterProvider provider = new ObjectWriterProvider();
        provider.register(Long.class, ObjectWriters.ofToString(Object::toString));
        provider.register(Integer.class, ObjectWriters.ofToString(Object::toString));
        
        config.setWriterContext(JSONFactory.createWriteContext(provider));

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

@jujn

jujn commented Apr 25, 2026

Copy link
Copy Markdown
Collaborator Author

done

public void setReaderContext(JSONReader.Context context) {
this.readerContext = context;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] 新增的 setReaderContext() / setWriterContext() 缺少测试覆盖和 Javadoc。

  1. 测试缺失: PR 新增了 4 个公共方法,但 diff 中没有测试文件变更。建议在 FastJsonConfigTest 中补充:(a) setter/getter 往返一致性,(b) PR 描述中的自定义 ObjectWriterProvider 用例,(c) 废弃方法委托正确性,(d) clearContext() 后自定义 context 被清除的行为。

  2. 缓存失效行为未文档化: 这两个 setter 注入的自定义 context 会被后续 setReaderFeatures()setDateFormat() 等方法通过 clearContext() 静默清除。JavaBean 的 set/get 命名隐含"值会持续生效"的契约,建议添加 Javadoc 警告此行为:

/**
 * Sets a pre-built JSONReader.Context.
 * <p><b>Warning:</b> subsequent calls to {@link #setReaderFeatures},
 * {@link #setDateFormat}, etc. will invalidate this custom context.
 */
public void setReaderContext(JSONReader.Context context) {
    this.readerContext = context;
}

— qwen3.7-max via Qwen Code /review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants