1

我无法使用 JIL 的 Exclude Null 选项。相反,我得到一个例外:

JIL.DeserializationException:'预期的数字'

以下是代码片段。

public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
{
    if (context == null) throw new ArgumentNullException(nameof(context));

    var request = context.HttpContext.Request; if (request.ContentLength == 0)
    {
        if (context.ModelType.GetTypeInfo().IsValueType)
            return InputFormatterResult.SuccessAsync(Activator.CreateInstance(context.ModelType));
        else return InputFormatterResult.SuccessAsync(null);
    }

    var encoding = Encoding.UTF8;//do we need to get this from the request im not sure yet 

    using (var reader = new StreamReader(context.HttpContext.Request.Body))
    {
        var model =  Jil.JSON.Deserialize(reader, context.ModelType, Jil.Options.ExcludeNulls);
        return InputFormatterResult.SuccessAsync(model);
    }
}

1) 型号类型

public class PaymentTypeBORequest
{   
    public int pkId { get; set; }        
    public string description { get; set; }
    public bool isSystem { get; set; }
    public bool isActive { get; set; }           
}

2)JSON字符串:

{
    "pkId":null,
    "description": "Adjustment",
    "isSystem": true,
    "isActive": true
}
4

1 回答 1

1

选项的描述excludeNulls是:

是否对象成员,其值为null

(强调我的)

这表明它只影响序列化操作而不影响反序列化操作。

序列化excludeNulls设置为的对象时,true如果属性具有值,Jil 不会将属性写入 JSON null。在您的示例中,您将反序列化为一个PaymentTypeBORequest对象,该对象本身不支持nullpkId属性的值,因为它不可为空。

为了解决您的特定问题,您可以简单地设置pkId为 nullable int,如下所示:

public class PaymentTypeBORequest
{   
    public int? pkId { get; set; }
    ...
}

如果您还想允许null不可为空isSystemisActive属性,您可以对这些字段执行相同的操作。

于 2018-10-21T13:14:18.323 回答