How to implement a polymorphic Newtonsoft.Json.JsonConverter?

  Kiến thức lập trình

Given the following polymorphic type hierarchy:

[JsonConverter(typeof(PolymorphicConverter<Base>))]
public record Base
{
    private Base() {}

    public record A(string Value) : Base;
    public record B(int Foobar) : Base;
    public record C(Base Recursive) : Base;
}

with the desired serialized json for e.g. A:

{
  "type": "A",
  "content": {
    "value": "jalla"
  }
}

Is it possible to create an implementation for PolymorphicConverter that’s not tied to the type Base, is performant and is thread safe?

New contributor

user3305478 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT