Fonlow.IntegralExtensions
1.2.0
dotnet add package Fonlow.IntegralExtensions --version 1.2.0
NuGet\Install-Package Fonlow.IntegralExtensions -Version 1.2.0
<PackageReference Include="Fonlow.IntegralExtensions" Version="1.2.0" />
paket add Fonlow.IntegralExtensions --version 1.2.0
#r "nuget: Fonlow.IntegralExtensions, 1.2.0"
// Install Fonlow.IntegralExtensions as a Cake Addin #addin nuget:?package=Fonlow.IntegralExtensions&version=1.2.0 // Install Fonlow.IntegralExtensions as a Cake Tool #tool nuget:?package=Fonlow.IntegralExtensions&version=1.2.0
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:
.AddNewtonsoftJson(
options =>
{
options.SerializerSettings.Converters.Add(new Int64JsonConverter());
options.SerializerSettings.Converters.Add(new Int64NullableJsonConverter());
options.SerializerSettings.Converters.Add(new UInt64JsonConverter());
options.SerializerSettings.Converters.Add(new UInt64NullableJsonConverter());
options.SerializerSettings.Converters.Add(new BigIntegerJsonConverter());
options.SerializerSettings.Converters.Add(new BigIntegerNullableJsonConverter());
}
);
...
[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:
- 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 | net9.0 is compatible. 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. |
-
net9.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.