OPCFoundation / UA-.NETStandard

OPC Unified Architecture .NET Standard
Other
1.91k stars 930 forks source link

Support for BigInteger cast in TypeInfo ? #2326

Open htbmw opened 9 months ago

htbmw commented 9 months ago

Type of issue

Current Behavior

Would it make sense to add support for BigInteger to the TypeInfo class?

My application receives a value from a JSON message that it then writes to an OPC UA node. The value is deserialized as a BigInteger when the value is a large number like UInt64.MaxValue.

I already use TypeInfo.Cast for all other values that can be received via the JSON message, but BigInteger currently cannot be converted and the cast returns null.

using Opc.Ua;
using System.Numerics;

Console.WriteLine("Converting BigInteger....");

var bigInteger = new BigInteger(UInt64.MaxValue);

var uint64Integer = TypeInfo.Cast(bigInteger, BuiltInType.UInt64);

Console.WriteLine($"Converted: {uint64Integer}");

**Actual Output:**

Converting BigInteger....
Converted: 

Expected Behavior

Expected Output:

Converting BigInteger.... Converted: 18446744073709551615

Steps To Reproduce

No response

Environment

- OS:
- Environment:
- Runtime:
- Nuget Version:
- Component:
- Server:
- Client:

Anything else?

No response

mregen commented 9 months ago

like the idea...

mregen commented 8 months ago

Hi @htbmw, a BigInteger can be smaller / bigger than a Int64/UInt64. How do you envision the cast when the overflow condition is hit?

htbmw commented 8 months ago

Hi @mregen

To be honest I haven't thought about this beyond using UInt64.MaxValue/MinValue. In my test I assert that the Min and MaxValue for a node of a particular numeric type can be handled by the json. OPC UA won't allow a value outside the range of that type to be written to the node. Also it won't give you a value outside that range on a read. Therefor I didn't think beyond the Min and Max ranges.

I think an overflow exception should suffice?

mregen commented 8 months ago

Hi @htbmw, the Overflow exception would work, could you provide the implementation with a few test cases?