在反序列化Web API 2 C#中删除NaN值 [英] Removing NaN values in deserialization Web API 2 C#

查看:287
本文介绍了在反序列化Web API 2 C#中删除NaN值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想知道是否有人可以帮助我,我试图在Web API 2中自动反序列化时自动将DoubleN中的NaN自动替换为0.我试图使用JSON.NET,但没有成功.任何帮助将不胜感激.我将以下内容放入我的WebApiConfig

Hello I am wondering if anyone could help me, I am trying to automatically replace NaN from double values automatically with 0 upon deserializing automatically in Web API 2. I am trying to use JSON.NET but I am having no success. Any help would be greatly appreciated. I am placing the below into my WebApiConfig

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

MediaTypeHeaderValue appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.FloatFormatHandling = Newtonsoft.Json.FloatFormatHandling.DefaultValue;
jsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
jsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonFormatter.SerializerSettings.FloatParseHandling =  FloatParseHandling.Double;
jsonFormatter.SerializerSettings.DefaultValueHandling =  DefaultValueHandling.Populate;

NaN值不会被删除,而是放在

The NaN values are not being removed and being put inside the class in a

public double Price { get; set; }

所以在数字里面我会得到NaN.

So inside of a number I get NaN inside.

推荐答案

我最终想出了如何解决读写问题的方法.

I eventually figured out how to solve the issue for both read and write.

jsonFormatter.SerializerSettings.Converters.Add(new FloatConverter());

public class FloatConverter : JsonConverter
{
    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (value == null)
        {
            writer.WriteNull();
            return;
        }

        var val = Convert.ToDouble(value);
        if (Double.IsNaN(val) || Double.IsInfinity(val))
        {
            writer.WriteNull();
            return;
        }
        if (value is float)
            writer.WriteValue((float)value);
        else
            writer.WriteValue((double)value);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        var value = JValue.Load(reader);
        var val = Convert.ToDouble(value);

        if (objectType == typeof(Double))
        {
            if (Double.IsNaN(val) || Double.IsInfinity(val))
                return (Double)0.00;
            else
                return (Double)value;
        }

        if (objectType == typeof(float?))
            return (float?)value;
        else
            return (float)value;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(double) || objectType == typeof(float);
    }
}

这篇关于在反序列化Web API 2 C#中删除NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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