Json.NET 用最少的小数位序列化浮点/双精度,即没有多余的“.0"?

Json.NET serializing float/double with minimal decimal places, i.e. no redundant quot;.0quot;?(Json.NET 用最少的小数位序列化浮点/双精度,即没有多余的“.0?)
本文介绍了Json.NET 用最少的小数位序列化浮点/双精度,即没有多余的“.0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当序列化浮点数和双精度数时,如果数字不包含任何小数部分,Json.NET 总是在末尾添加.0".我想知道是否有一种简单的方法可以绕过它,从而产生更紧凑的表示?序列化包含许多数字的对象时,额外的句点和零会累加.

例如,运行这段代码时:

JsonConvert.SerializeObject(1.0);

我希望(并且想要)这个结果:

<代码>1"

但是我得到了:

"1.0"

我查看了源代码,发现它是在提交中故意添加的 0319263 ("...-修复 JsonConvert 始终写入带小数位的浮点数...") 它运行的代码基本上如下所示:

 private static string EnsureDecimalPlace(double value, string text){if (double.IsNaN(value) || double.IsInfinity(value) ||text.IndexOf('.') != -1 ||text.IndexOf('E') != -1 ||text.IndexOf('e') != -1){返回文本;}返回文本+.0";}

因此,我想知道:

  1. 这种变化的原因可能是什么?JSON 规范 似乎不需要它.

  2. 有没有简单的方法绕过它?

解决方案

1.发生这种变化的原因可能是什么?

规范没有要求,但也没有禁止.

我的猜测是,它允许对 Json.NET 进行更好的类型检查(如果他们在某处有它),或者它是一个以防万一"的东西,允许区分整数和浮点类型.

<块引用>

<强>2.有没有简单的方法绕过它?

没那么容易,但是如果你真的想要的话,你可以在将 EnsureDecimalPlace() 更改为简单的 return text;

When serializing floats and doubles, Json.NET always adds ".0" at the end if the number doesn't contain any fractional part. I was wondering if there was an easy way to bypass this, to result in a more compact representation? The extra periods and zeroes add up when serializing an object containing many numbers.

For example, when running this code:

JsonConvert.SerializeObject(1.0);

I would expect (and want) this result:

"1"

But instead I get:

"1.0"

I looked at the source code and noticed that it was purposely added in commit 0319263 ("...-Fixed JsonConvert to always write a floating point number with a decimal place...") where it runs code that looks basically like:

    private static string EnsureDecimalPlace(double value, string text)
    {
        if (double.IsNaN(value) || double.IsInfinity(value) ||
            text.IndexOf('.') != -1 || text.IndexOf('E') != -1 ||
            text.IndexOf('e') != -1)
        {
            return text;
        }

        return text + ".0";
    }

Consequently, I am wondering:

  1. What may have been the reason for that change? The JSON specification does not seem to require it.

  2. Is there an easy way to bypass it?

解决方案

1. What may have been the reason for that change?

The specs do not require it, but it also does not disallow it.

My guess is that it allows for better type checking for Json.NET (if they have it somewhere) or it's a "just-in-case" thing that allows differentiation between integer and floating point types.

2. Is there an easy way to bypass it?

Not that easy, but if you really want it, you can recompile your own version of Json.NET after changing EnsureDecimalPlace() to simply return text;

这篇关于Json.NET 用最少的小数位序列化浮点/双精度,即没有多余的“.0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)