0

I created a custom converter extending from JsonConverter, which would be used for an ASP.NET MVC and a Web API.

public class MyCustomConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        ....
    }
}

And I created this CustomObject that uses that converter:

[JsonConverter(typeof(MyCustomJsonConverter))]
public class CustomObject
{
    ...
}

This converter work correctly for second application (WebApi), that means method of ReadJson are running after calling it in TestOfUsingJson. And in this case I didn't have to set up anything.

For the first application (ASP.NET MVC) I have a trouble, object are converted from json, but this object are not created from my custom converter. Method of ReadJson are not running.

Method which who use custom converter are looks same on the every application

public HttpResponseMessage TestOfUsingJson([FromBody] CustomObject objs)
{
    ...
}

Some settings for Json Serializer in the ASP.NET MVC's Global.asax.cs:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Converters = new List<JsonConverter> { new MyCustomJsonConverter() }
};

What am I doing wrong?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
neuser
  • 107
  • 13
  • How is this being used in the MVC application? the default serializer for mvc is JavaScriptSerializer, not Json.net. you need to tell mvc to user Json.net – Fran Jun 12 '18 at 17:56
  • Possible duplicate of [Setting the default JSON serializer in ASP.NET MVC](https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc) – Fran Jun 12 '18 at 17:57
  • @Fran: The default was changed from `JavascriptSerializer` to `Json.NET` at some point (at least with Core, possibly earlier). It's dependent on the version of MVC being used. – Sam Axe Jun 12 '18 at 18:00
  • @SamAxe The OP doesn't mention Core and as of mvc5 you still needed to replace it. – Fran Jun 12 '18 at 18:02

1 Answers1

0

I have prepared some solution that may be useful for someone

Create binding model

public class BindJson : IModelBinder
{
    public BindJson()
    {

    }

    public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null)
            throw new SysException("Missing controller context");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        controllerContext.HttpContext.Request.InputStream.Position = 0;

        using (var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream))
        {
            var data = reader.ReadToEnd();

            if (!String.IsNullOrEmpty(bodyText))
            {
                return JsonConvert.DeserializeObject(data, bindingContext.ModelType);
            }
        }

        return null;
    }

}

Assign binder in Global.asax.cs

..
ModelBinders.Binders.Add(typeof(BindJson), new BindJson());
..

Assign binder into class

[ModelBinder(typeof(BindJson))]
public class CustomObject
{
    ...
}

Call binder in method

public HttpResponseMessage TestOfUsingJson(CustomObject objs)
{
    ...
}
neuser
  • 107
  • 13
  • you've just recreated what [JsonDotNetValueProviderFactory](https://github.com/DalSoft/JsonDotNetValueProviderFactory/blob/master/JsonDotNetValueProviderFactory/JsonValueProviderFactory.cs) with more steps. – Fran Jun 13 '18 at 13:29