JSonConverter not starting property of my model in C # WebAPI

I have a model in my WebAPI application written in .NET 4.0 that has a type property System.Net.Mime.ContentType, for example:

[Serializable]
public class FileData
{
    private ContentType contentType;
    private long size;
    private string name;

    public ContentType ContentType
    {
        get { return  contentType; }
        set { contentType = value; } 
    }

    ...

    /* same getter/setter logic for the other fields  */
}

The model is in a separate assembly from my web project.

So, the client sends me a JSON message that I need to convert to this class:

{
    "size": 12345,
    "contentType": "image/png",
    "name": "avatar.png"
}

To tell Json.NET how to convert ContentType, I registered a user JsonConverterthat I wrote for this purpose:

JsonFormatter.SerializerSettings.Converters.Add(new ContentTypeJsonConverter());

In the above code, I mean the global obeject JsonFormatterfor the WebApi application.

That way, when the client sends me JSON, I expect the controller to parse the message correctly.

Sorry, error failed:

"Cannot convert from System.String to System.Net.Mime.ContentType."

, , FileData:

public class FileData
{
    ...

    [JsonConverter(typeof(ContentTypeJsonConverter))]
    public ContentType ContentType { /* Getter and Setter */ }
}

, JSON.NET , FileData.

ContentType FileData?


, :

JsonFormatter.SerializerSettings.ContractResolver = new CustomResolver();

CustomResolver:

class CustomResolver : DefaultContractResolver
{
    protected override JsonContract CreateContract(Type objectType)
    {
        var contract = base.CreateContract(objectType);
        if (objectType == typeof(ContentType))
        {
            contract.Converter = new ContentTypeJsonConverter();
        }
        return contract;
    }
}

.

+4
1

(Web API 2).

:

[Serializable]
public class FileData
{
    private ContentType contentType;

    public ContentType ContentType
    {
        get { return contentType; }
        set { contentType = value; }
    }
}

JSON:

public class ContentTypeJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(ContentType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return new ContentType((string)reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(((ContentType)value).ToString());
    }
}

(WebApiConfig.cs):

public static void Register(HttpConfiguration config)
{
    ...
    config
        .Formatters
        .JsonFormatter
        .SerializerSettings
        .Converters
        .Add(new ContentTypeJsonConverter());
}

:

public class TestController : ApiController
{
    public IHttpActionResult Post(FileData data)
    {
        return this.Ok(data);
    }
}

:

POST /api/test HTTP/1.1
Content-Type: application/json
Host: localhost:48278
Content-Length: 36

{
    "contentType": "image/png"
}

:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?ZDpcd29ya1xUb0REXGFwaVx0ZXN0?=
X-Powered-By: ASP.NET
Date: Mon, 25 Jul 2016 07:06:02 GMT
Content-Length: 27

{"contentType":"image/png"}
+1

Source: https://habr.com/ru/post/1648941/


All Articles