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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)