You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treat xs:decimal and integer types the same way in the ValueOnly JSON serialization #694
Currently, ValueOnlyJsonEncoder._value_to_json_type() in basyx.aas.adapter.json.formats.value_only_serialization (introduced in #660) handles numeric types inconsistently:
xs:decimal values are converted to float before serialization, because the stdlib json cannot serialize Decimal. This rounds them to the nearest IEEE double, so we lose precision ourselves before the client sees the value:
Decimal("0.10000000000000000000001") → 0.1
Decimal(2**53 + 1) → 9007199254740992.0 (off by one, and gets a .0)
Integer types are passed through unchanged, and json writes them exactly at any size (2**53 + 1 → 9007199254740993). Precision is only lost if the client parses JSON numbers as doubles.
The specification allows both: IDTA-01001 maps xs:decimal to a JSON number and says that values that cannot be represented in JSON "cannot be used" (IDTA-01001 v3.1.2, Note 1). So this is not a spec violation, but the behavior should be consistent across all numeric types.
We should choose one of these approaches:
Keep the exact value for all types: Write the Decimal digits as a JSON number instead of going through float, and leave any rounding to the client, as we already do for integers.
Round all types: Also convert the unbounded integer types to float, so that every number in our output can be represented as an IEEE double.
Whichever we choose, document it in the docstring of ValueOnlyJsonEncoder, next to the note on NaN/INF.
Activity
Document `xs:decimal` precision loss in ValueOnly
sdk/server: Implement JSON serialization formats (#660)