Fonlow.IntegralExtensions 1.0.0

dotnet add package Fonlow.IntegralExtensions --version 1.0.0
NuGet\Install-Package Fonlow.IntegralExtensions -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fonlow.IntegralExtensions" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fonlow.IntegralExtensions --version 1.0.0
#r "nuget: Fonlow.IntegralExtensions, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Fonlow.IntegralExtensions as a Cake Addin
#addin nuget:?package=Fonlow.IntegralExtensions&version=1.0.0

// Install Fonlow.IntegralExtensions as a Cake Tool
#tool nuget:?package=Fonlow.IntegralExtensions&version=1.0.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.

  1. Int64JsonConverter
  2. UInt64JsonConverter
  3. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 99 2/22/2024