Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected Object readType(Type type, HttpInputMessage inputMessage) {
final long contentLength = inputMessage.getHeaders().getContentLength(); // -1 表示未知
try {
final byte[] body = fastRead(inputMessage.getBody(), contentLength);
return JSON.parseObject(body, type, config.readerContext());
return JSON.parseObject(body, type, config.getReaderContext());
} catch (JSONException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex, inputMessage);
} catch (IOException ex) {
Expand Down Expand Up @@ -211,7 +211,7 @@ protected void writeInternal(Object object, HttpOutputMessage outputMessage) thr
}

contentLength = JSON.writeTo(
baos, object, config.writerContext()
baos, object, config.getWriterContext()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void clearContext() {
writerContext = null;
}

public JSONReader.Context readerContext() {
public JSONReader.Context getReaderContext() {
Comment thread
jujn marked this conversation as resolved.
JSONReader.Context context = readerContext;
if (context == null) { // Concurrency may occur, but it will not cause any problems
context = new JSONReader.Context(JSONFactory.getDefaultObjectReaderProvider(), jsonb ? symbolTable : null, readerFilters, readerFeatures);
Expand All @@ -278,7 +278,7 @@ public JSONReader.Context readerContext() {
return context;
}

public JSONWriter.Context writerContext() {
public JSONWriter.Context getWriterContext() {
Comment thread
jujn marked this conversation as resolved.
JSONWriter.Context context = writerContext;
if (context == null) {
context = new JSONWriter.Context(dateFormat, writerFeatures);
Expand All @@ -293,4 +293,32 @@ public JSONWriter.Context writerContext() {
}
return context;
}

/**
* Gets the JSONReader context.
* @deprecated Use {@link #getReaderContext()} instead to comply with JavaBean naming conventions.
* @return the JSONReader context
*/
@Deprecated
public JSONReader.Context readerContext() {
return getReaderContext();
}

/**
* Gets the JSONWriter context.
* @deprecated Use {@link #getWriterContext()} instead to comply with JavaBean naming conventions.
* @return the JSONWriter context
*/
@Deprecated
public JSONWriter.Context writerContext() {
return getWriterContext();
}

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

public void setWriterContext(JSONWriter.Context context) {
this.writerContext = context;
}
}
Loading