CastException when serializing derived class with System.Text.Json

huangapple go评论133阅读模式
英文:

CastException when serializing derived class with System.Text.Json

问题

我有一个简单的CLI应用程序,根据args获取不同的数据,然后使用System.Text.Json进行JSON序列化。

我遇到的问题是,在序列化派生类(例如,在这种情况下是List<DerivedImage>)时,如何使用基类的自定义转换器(例如ImageConverter)来解决这个问题?

  1. public static int Main(string[] args)
  2. {
  3. object data = "images ls" switch
  4. {
  5. "images ls" => new List<DerivedImage>() { new DerivedImage() },
  6. // ... 其他情况 ...
  7. };
  8. var options = new JsonSerializerOptions()
  9. {
  10. Converters = { new ImageConverter() }
  11. };
  12. var stringified = JsonSerializer.Serialize(data, options);
  13. // ^ 这里出错了!
  14. // 异常消息:
  15. // System.InvalidCastException: 无法将类型为 'ImageConverter'
  16. // 的对象强制转换为类型 'JsonConverter[DerivedImage]'
  17. return 1;
  18. }
  19. public class ImageConverter : JsonConverter<Image>
  20. {
  21. public override bool CanConvert(Type typeToConvert)
  22. {
  23. return typeof(Image).IsAssignableFrom(typeToConvert);
  24. }
  25. public override Image Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public override void Write(Utf8JsonWriter writer, Image value, JsonSerializerOptions options)
  30. {
  31. // 从不会执行到这里。
  32. }
  33. }
  34. public abstract class Image { }
  35. public class DerivedImage : Image { }

在上面的代码片段中,如何让序列化器在传递data时使用ImageConverter呢?

英文:

I've got a simple CLI app that gets some different data depending on args and then JSON serializes it using System.Text.Json.

The problem I'm running into is when serializing a derived class (ie. List<DerivedImage> in this case) with a custom converter for the base class (ie. ImageConverter).

  1. public static int Main(string[] args)
  2. {
  3. object data = "images ls" switch
  4. {
  5. "images ls" => new List<DerivedImage>() { new DerivedImage() },
  6. // ... etc ...
  7. };
  8. var options = new JsonSerializerOptions()
  9. {
  10. Converters = { new ImageConverter() }
  11. };
  12. var stringified = JsonSerializer.Serialize(data, options);
  13. // ^ THIS ERRORS!
  14. // Exception message:
  15. // System.InvalidCastException: 'Unable to cast object of type 'ImageConverter'
  16. // to type 'JsonConverter[DerivedImage]'
  17. return 1;
  18. }
  19. public class ImageConverter : JsonConverter<Image>
  20. {
  21. public override bool CanConvert(Type typeToConvert)
  22. {
  23. return typeof(Image).IsAssignableFrom(typeToConvert);
  24. }
  25. public override Image Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public override void Write(Utf8JsonWriter writer, Image value, JsonSerializerOptions options)
  30. {
  31. // It never gets here.
  32. }
  33. }
  34. public abstract class Image { }
  35. public class DerivedImage : Image { }

With the snippet above, how can I get the serializer to use the ImageConverter when passed data?

答案1

得分: 0

升级项目配置中的 TargetFramework.net6.net7 已解决。

英文:

Resolved by upgrading TargetFramework in the project config from .net6 to .net7.

huangapple
  • 本文由 发表于 2023年8月4日 02:26:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定