定制的WebAPI不JsonConverter叫 [英] WebApi custom JsonConverter not called

查看:237
本文介绍了定制的WebAPI不JsonConverter叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了自定义JsonConverter为的GUID。

如果我宣布它的属性序列化,像这样的类(类型GUID的)

  [JsonConverter(typeof运算(JsonGuidConverter))]

那么它被调用和工作正常。

不过,我想为自动使用它,没有需要的属性,所以我这样做:

  VAR格式化= GlobalConfiguration.Configuration.Formatters;
VAR jsonFormatter = formatters.JsonFormatter;
jsonFormatter.SerializerSettings.Converters.Add(新JsonGuidConverter());

不幸的是这导致我从未器获取调用。我使用的WebAPI 2.1在MVC 5.1项目。

任何想法?

编辑:这里是转换器code

 公共类JsonGuidConverter:JsonConverter
{
    公众覆盖布尔的CanRead
    {
        得到
        {
            //我们只需要转换器编写的GUID没有破折号,读取默认的机制是罚款
            返回false;
        }
    }
    公众覆盖布尔CanWrite
    {
        得到
        {
            返回true;
        }
    }    公众覆盖布尔CanConvert(类型的objectType)
    {
        返回的objectType == typeof运算(GUID)|| ==的objectType typeof运算(GUID);
    }    公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue,Newtonsoft.Json.JsonSerializer串行)
    {
        //我们宣布上述虚假的CanRead所以默认的序列化将使用
        抛出新NotImplementedException();
    }    公共覆盖无效WriteJson(JsonWriter作家,对象的值,Newtonsoft.Json.JsonSerializer串行)
    {
        如果(价值== NULL || Guid.Empty.Equals(值))
        {
            writer.WriteValue(的String.Empty);
        }
        其他
        {
            writer.WriteValue(((GUID)值).ToStringNoDashes());
        }
    }
}

补充说明,甚至没有的CanRead / CanWrite属性和方法CanConvert试图将它添加到转换器采集到使用它的时候被调用。

或许这是与我是如何从控制器的WebAPI返回数据?

 公共异步任务< IHttpActionResult>的getSettings()
{
    ...
    返回JSON(东西);
}


解决方案

由于您使用的是格式化,不要使用 JSON(东西)从动作返回时,而是使用在这种情况下内容(东西)。在内容助手将信守格式化的设置。

我同意的Json 助手在这里混淆和一些东西,我希望我们从来没有包括在我们的产品。

I implemented a custom JsonConverter for Guids.

If I declare it on the properties (of type Guid) of the class serialized like so

[JsonConverter(typeof(JsonGuidConverter))]

then it gets called and works fine.

However, I'd like to use it "automatically", without needed the attributes, so I do this:

var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
jsonFormatter.SerializerSettings.Converters.Add(new JsonGuidConverter());

Unfortunately this results in my converter never getting called. I'm using WebApi 2.1 in a MVC 5.1 project.

Any ideas?

Edit: here is the converter code

public class JsonGuidConverter : JsonConverter
{
    public override bool CanRead
    {
        get
        {
            // We only need the converter for writing Guids without dashes, for reading the default mechanism is fine
            return false;
        }
    }
    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Guid) || objectType == typeof(Guid?);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
    {
        // We declared above CanRead false so the default serialization will be used
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        if (value == null || Guid.Empty.Equals(value))
        {
            writer.WriteValue(string.Empty);
        }
        else
        {
            writer.WriteValue(((Guid)value).ToStringNoDashes());
        }
    }
}

Additional note, not even the CanRead/CanWrite properties and CanConvert method are called when trying to use it by adding it to the converters collection.

Maybe it has something to do with how I return data from the webapi controller?

public async Task<IHttpActionResult> GetSettings()
{
    ...
    return Json(something);
}

解决方案

Since your are using formatters, do not use Json(something) when returning from action, but rather use Content(something) in this case. The Content helper will honor the formatters' settings.

I agree that Json helper is confusing here and something which I wished we never included in our product.

这篇关于定制的WebAPI不JsonConverter叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆