You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NJsonSchema v11中JsonSchemaGeneratorSettings变更后如何兼容v10.9旧逻辑?

NJsonSchema 11.x 兼容10.9版本Schema生成器配置方案

在NJsonSchema 11.x版本中,原通用的JsonSchemaGenerator和JsonSchemaGeneratorSettings被拆分为适配不同JSON序列化库的实现,你需要使用对应Newtonsoft.Json的生成器及配置类来还原10.9版本的行为,具体实现如下:

替代代码实现

private static NewtonsoftJsonSchemaGenerator MakeGenerator()
{
    var settings = new NewtonsoftJsonSchemaGeneratorSettings
    {
        DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull,
        AllowReferencesWithProperties = true,
        FlattenInheritanceHierarchy = true
    };
    // 仅当需要自定义反射逻辑时才需手动指定反射服务,默认已内置NewtonsoftJsonReflectionService
    // settings.ReflectionService = new NewtonsoftJsonReflectionService();
    return new NewtonsoftJsonSchemaGenerator(settings);
}

关键细节说明

  • 11.x版本将原10.x基于Newtonsoft.Json的JsonSchemaGenerator,拆分为独立的NewtonsoftJsonSchemaGenerator,另一个分支是适配System.Text.Json的SystemTextJsonSchemaGenerator,你的原有逻辑对应前者
  • 原JsonSchemaGeneratorSettings中的所有配置项,均完整迁移至NewtonsoftJsonSchemaGeneratorSettings中,包含你需要的三个核心配置
  • 原DefaultReflectionService已被替换为NewtonsoftJsonReflectionService,实例化NewtonsoftJsonSchemaGeneratorSettings时会自动初始化该服务,无需手动创建

内容的提问来源于stack exchange,提问作者Aaron Haspel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 14:13:16