Fonlow.IntegralExtensionsTextJson
1.0.0
See the version list below for details.
dotnet add package Fonlow.IntegralExtensionsTextJson --version 1.0.0
NuGet\Install-Package Fonlow.IntegralExtensionsTextJson -Version 1.0.0
<PackageReference Include="Fonlow.IntegralExtensionsTextJson" Version="1.0.0" />
paket add Fonlow.IntegralExtensionsTextJson --version 1.0.0
#r "nuget: Fonlow.IntegralExtensionsTextJson, 1.0.0"
// Install Fonlow.IntegralExtensionsTextJson as a Cake Addin #addin nuget:?package=Fonlow.IntegralExtensionsTextJson&version=1.0.0 // Install Fonlow.IntegralExtensionsTextJson as a Cake Tool #tool nuget:?package=Fonlow.IntegralExtensionsTextJson&version=1.0.0
For ASP.NET Core 7 and above, needed by JavaScript clients and any naughty client, since System.Text.Json is a bit less fault tolerant thant Newtonsoft.Json
ASP.NET by default will serialize all integral numeric types and BigInteger to JSON number, however for long and ulong of 64bit, a JavaScript client by default will have problems of keeping the precision due to the 53bit limitation.
.NET 7 and onward have 2 new integral types: Int128 and UInt128, and ASP.NET serializes them to JSON string, as long as a JavaScript gladly accept it as a string and transform the JSON string object to BigInt, the precision is kept.
The following derived classes of NewtonSoft.Json.JsonConverter
make ASP.NET serialize integral types of 64bit and BigInteger the way of handling Int128 and UInt128.
- Int64JsonConverter
- UInt64JsonConverter
- BigIntegerJsonConverter
Usage
Backend:
.AddJsonOptions(
options =>
{
options.JsonSerializerOptions.Converters.Add(new BigIntegerJsonConverter());
options.JsonSerializerOptions.Converters.Add(new Int64JsonConverter());
options.JsonSerializerOptions.Converters.Add(new UInt64JsonConverter());
options.JsonSerializerOptions.Converters.Add(new Int128JsonConverter());
options.JsonSerializerOptions.Converters.Add(new UInt128JsonConverter());
);
...
[HttpPost]
[Route("int64")]
public long PostInt64([FromBody] long int64)
{
return int64;
}
JavaScript client:
it('postInt64', (done) => {
service.postInt64('9223372036854775807').subscribe(
r => {
expect(BigInt(r)).toBe(BigInt('9223372036854775807'));
done();
},
error => {
fail(errorResponseToString(error));
done();
}
);
}
);
...
/**
* POST api/Numbers/int64
* @param {string} int64 Type: long, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
* @return {string} Type: long, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
*/
postInt64(int64?: string | null, headersHandler?: () => HttpHeaders): Observable<string> {
return this.http.post<string>(this.baseUri + 'api/Numbers/int64', JSON.stringify(int64), { headers: headersHandler ? headersHandler().append('Content-Type', 'application/json;charset=UTF-8') : new HttpHeaders({ 'Content-Type': 'application/json;charset=UTF-8' }) });
}
Remarks:
options.JsonSerializerOptions.NumberHandling
may adjust the behavior of serializing and deserializing numbers, however, if you serialize numbers into string in wholesale way, this could break existing clients. So generally for number types below Int64, better leave them as JSON number object as the old way.- Changing the serialization may be a breaking changes to existing clients. Please make sure you evaluate your technical context and carefully plan for versioning.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.