jsonbrowserint128.net-8.0

.NET 8.0: Int128 and Uint128 equivalent in JSON?


With .NET 8.0 supporting 128 bit integers as Int128 and Uint128 (here),

How can this be possibly be handled in browsers?

[UPDATE]

I have two things primarily on my mind:

One, how possibly these can be handled in arithmetic calcs in browser (one of my earlier projects involved statistical analyses in JS in browser), and

Two, if .NET is providing these datatypes, what is the purpose and what most common adjacent building blocks/techs can possibly handle them.


Solution

  • Javascript has at least two build-in types applicable here. If you are ok with precision loss then you can go with Number (The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#), otherwise you need to look into using BigInt or some custom type.

    Difference between Number and BigInt:

    <h1>JavaScript Numbers</h1>
    <h2>Integer Precision</h2>
    
    <p>Integers (numbers without a period or exponent notation) are accurate up to 15 digits:</p>
    
    <p id="demo"></p>
    
    <script>
    let x = 999999999999999;
    let y = 9999999999999999;
    let z = 340282366920938463463374607431768211455;
    let zb = BigInt("340282366920938463463374607431768211455");
    document.getElementById("demo").innerHTML = x + "<br>" + y + "<br>" + z + "<br>" + zb;
    </script>

    JSON has a single "direct" node type for numbers and does not distinguish between number types, by default JSON.parse will use Number so you can encounter aforementioned precision loss in case of big numbers, in this case you can look into using some workarounds like json-bigint library or solution from this answer .