System.Text.Json 自定义 JsonConverter 从未被调用
System.Text.Json Custom JsonConverter never called
我想从 Newtonsoft 迁移一个 Asp 网络核心项目。我必须使用 JsonConverter 来保留一些旧功能。
我曾尝试在 属性、类型和启动时调用我的自定义转换器,正如我在 docs 中所读;它似乎从来没有被调用过。
我创建了一个示例项目以确保它不是别的东西。如果转换器执行 Write 或 Read 方法,它应该抛出异常。但是到目前为止我还没有能够让他们执行。
这是转换器的代码
public class MyCustomConverter : JsonConverter<FooProp>
{
public override FooProp Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) => throw new NotImplementedException();
public override void Write(
Utf8JsonWriter writer,
FooProp value,
JsonSerializerOptions options) => throw new NotImplementedException();
}
控制器代码
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
public WeatherForecastController()
{
}
[HttpGet]
public IActionResult Get()
{
return Ok(new FooClass());
}
public class FooClass
{
public string Always {get; set;}
// Adds on property
[JsonConverter(typeof(MyCustomConverter))]
public FooProp Sometimes {get; set;}
}
// Adds on type
[JsonConverter(typeof(MyCustomConverter))]
public class FooProp
{
public string Something { get; set; }
}
}
以及启动代码
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.AddJsonOptions(options => {
// Adds on startup
options.JsonSerializerOptions.Converters.Add(new MyCustomConverter());
});
}
.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
看起来很简单,我一定是犯了一个明显的错误,但是我在这上面弄了一段时间也找不到我做错了什么。
谁能帮帮我?
I have tried calling my custom converter on a property, on a type and
on startup as I read in the docs; It never seems to be called.
那是因为你的自定义JsonConverter
继承了JsonConverter<FooProp>
只能转换FooProp
class:
public class MyCustomConverter : JsonConverter<FooProp>
您返回了新的 FooClass,它将创建 null Sometimes
。
只需更改以下代码即可看到异常:
[HttpGet]
public IActionResult Get()
{
//return Ok(new FooProp());
return Ok(new FooClass { Sometimes = new FooProp() });
}
我想从 Newtonsoft 迁移一个 Asp 网络核心项目。我必须使用 JsonConverter 来保留一些旧功能。
我曾尝试在 属性、类型和启动时调用我的自定义转换器,正如我在 docs 中所读;它似乎从来没有被调用过。
我创建了一个示例项目以确保它不是别的东西。如果转换器执行 Write 或 Read 方法,它应该抛出异常。但是到目前为止我还没有能够让他们执行。
这是转换器的代码
public class MyCustomConverter : JsonConverter<FooProp>
{
public override FooProp Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) => throw new NotImplementedException();
public override void Write(
Utf8JsonWriter writer,
FooProp value,
JsonSerializerOptions options) => throw new NotImplementedException();
}
控制器代码
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
public WeatherForecastController()
{
}
[HttpGet]
public IActionResult Get()
{
return Ok(new FooClass());
}
public class FooClass
{
public string Always {get; set;}
// Adds on property
[JsonConverter(typeof(MyCustomConverter))]
public FooProp Sometimes {get; set;}
}
// Adds on type
[JsonConverter(typeof(MyCustomConverter))]
public class FooProp
{
public string Something { get; set; }
}
}
以及启动代码
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.AddJsonOptions(options => {
// Adds on startup
options.JsonSerializerOptions.Converters.Add(new MyCustomConverter());
});
}
.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
看起来很简单,我一定是犯了一个明显的错误,但是我在这上面弄了一段时间也找不到我做错了什么。
谁能帮帮我?
I have tried calling my custom converter on a property, on a type and on startup as I read in the docs; It never seems to be called.
那是因为你的自定义JsonConverter
继承了JsonConverter<FooProp>
只能转换FooProp
class:
public class MyCustomConverter : JsonConverter<FooProp>
您返回了新的 FooClass,它将创建 null Sometimes
。
只需更改以下代码即可看到异常:
[HttpGet]
public IActionResult Get()
{
//return Ok(new FooProp());
return Ok(new FooClass { Sometimes = new FooProp() });
}