Search code examples
c#serializationjsonserializerjson-serializationjil

JIL json serializer does not serialize properties from derived class


JIL json serializer does not serialize properties from derived class

Below are the code snippet:

public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var response = context.HttpContext.Response; response.ContentType = "application/json";

            using (var writer = context.WriterFactory(response.Body, Encoding.UTF8))
            {
                Jil.JSON.Serialize(context.Object, writer);
                await writer.FlushAsync();
            }
        }

1) Model Type:

public class BaseBOResponse
{    
   public string pk { get; set; }
}

public class PaymentTypeBOResponse : BaseBOResponse
{          
    public string description { get; set; }
    public bool isSystem { get; set; }
    public bool isActive { get; set; }           
}

Here when i set something to BaseBOResponse's response property "pk", then JIL serializer eliminate this property.

Please suggest if you have any solution.


Solution

  • You have to tell Jil to include inherited properties as well:

    Jil.JSON.Serialize(context.Object, writer, Jil.Options.IncludeInherited);