In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use string to express the Number attribute value in Json. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
A weird situation caused by the long type
To make a long story short, when my colleagues used swagger to dock with the back-end API, they strangely found that the value of the json attribute shown in swaggerUI was not the value returned by api.
[HttpGet]
Public IActionResult QueryAsync ()
{
Var testJson = new
{
Id = 123123126964992223
Profile = "Please attention on Id"
}
Return new JsonResult (testJson)
}
The API is output in swagger:
{"Id": 123123126964992220
"Profile": "Please attention on Id"}
Further, the difference in the value of the long attribute is observed from Chrome- > [Network]-> [Preview] and [Response payload].
Give a direct conclusion: some long type values (maximum 2 ^ 63 ^-1) will exceed the maximum secure Number of Javascript (2 ^ 53 ^-1), and the browser / front end using JSON.parse (123123126964992223) will no longer guarantee accuracy.
The purpose of transmitting numeric values in JSON as strings is to eliminate loss of precision or ambiguity in transmission.
The numerical precision is not specified in the JSON specification, and the JSON parser is free to choose the appropriate numerical precision. If your application has specific precision requirements, different JSON parsers may not express precision correctly.
In addition, some long type values (maximum 263-1) exceed the maximum secure Number of Javascript (253-1), and errors can occur when the front-end json is deserialized.
Stackoverflow has a great explanation:
Override the. NET Core serialization framework to convert long to string
Write BigIntJsonConvert for NewtonsoftJson
Public class BigIntJsonConverter: JsonConverter
{
Public override long ReadJson (JsonReader reader, Type objectType, [AllowNull] long existingValue, bool hasExistingValue, JsonSerializer serializer)
{
Var flag = long.TryParse (reader.Value.ToString (), out long num)
Return flag = = true? Num: 0
}
Public override void WriteJson (JsonWriter writer, [AllowNull] long value, JsonSerializer serializer)
{
Writer.WriteValue (value.ToString ())
}
}
/ / truncated from Startup.cs ConfigureServices function
Context.Services.AddMvc () .AddNewtonsoftJson (options = >
{
Options.SerializerSettings.Converters.Add (new BigIntJsonConverter ())
});
On how to use string in Json to express the Number attribute value is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.