Leff
Leff

Reputation: 582

JsonConverter does not work on model binding

I got the following model

public class SignDocumentsModel
{
    [JsonProperty(ItemConverterType = typeof(BinaryConverter))]
    public byte[][] Documents { get; set; }

    public bool Detached { get; set; }
}

and the controller code

[HttpPost]
[Route("{requestId}/sign")]
public Task<IHttpActionResult> SignDocuments([FromUri] Guid requestId, SignDocumentsModel parameters)
{
    return SomeKindOfProcessing(requestGuid, parameters);
}

Now, when I perform a request with the Postman

POST
Content-Type: application/json
{
    "Detached": "true",
    "Documents": [
        "bG9weXN5c3RlbQ=="
    ]
}

I suppose the Documents property should be populated with byte arrays decoded from Base64 strings posted in request content, although in fact the property is empty (in case its type in model is List<byte[]> or byte[][], and null in case of IEnumerable<byte[]>).

Why JsonConverter doesn't being called on request body deserialization during model binding? How it can be fixed?

Upvotes: 1

Views: 521

Answers (1)

xander
xander

Reputation: 1709

Have you tried removing [JsonProperty(ItemConverterType = typeof(BinaryConverter))]?

In my test setup, the model binds successfully after I remove that attribute.

Edit: a bit more info...

According to the Json.NET Serialization Guide, a byte[] will serialize to a base64 string by default. Judging by the source code, it looks like BinaryConverter is meant to be used with System.Data.Linq.Binary or System.Data.SqlTypes.SqlBinary--not byte[].

Upvotes: 3

Related Questions