Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
388d0ab
fix: convert an integer beyond the double range to infinity in to_rdf
jdsika Nov 4, 2026
ee5d035
test: convert integers beyond the double range to infinity in to_rdf
jdsika Nov 4, 2026
d3050dd
docs: add the large integer fix to the changelog
jdsika Nov 4, 2026
5882481
refactor: type _canonicalize_double for integers as well
jdsika Nov 4, 2026
675a798
test: check large integers exactly and in json-ld-1.0 mode in to_rdf
jdsika Nov 4, 2026
fb52591
test: expand an integer beyond the double range
jdsika Nov 4, 2026
da6b529
docs: describe the large integer fix more precisely in the changelog
jdsika Nov 4, 2026
0aafefa
fix: write the xsd:double NaN as NaN, its XSD canonical form
jdsika Nov 4, 2026
6bbd3f3
test: write special xsd:double values in their XSD canonical forms
jdsika Nov 4, 2026
f932bbe
docs: add the NaN canonical form fix to the changelog
jdsika Nov 4, 2026
35fe25c
refactor: map the special xsd:double values explicitly in _canonicali…
jdsika Nov 4, 2026
6b25388
test: describe the typed strings in the special double test accurately
jdsika Nov 4, 2026
19ae388
docs: correct the large integer and NaN changelog entries
jdsika Nov 4, 2026
f7ce712
docs: link the specs in the xsd:double helper docstrings
jdsika Nov 10, 2026
a3675ce
test: describe the expected behaviour in the large integer and specia…
jdsika Nov 10, 2026
53bb718
test: describe the expected behaviour in the large integer expand test
jdsika Nov 10, 2026
8995b4c
docs: drop the RDFC-1.0 mention from the NaN changelog entry
jdsika Nov 10, 2026
5f1efef
refactor: canonicalize xsd:double typed strings with a single conversion
jdsika Nov 10, 2026
d16f219
docs: state the inputs and the overflow rounding of the xsd:double he…
jdsika Nov 10, 2026
c4cbabf
docs: state where @json still overflows and -INF in the large integer…
jdsika Nov 10, 2026
57c1988
docs: say the operations no longer fail with an OverflowError, which …
jdsika Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
- IRI validation now detects multiple # fragments so these IRI's are skipped during toRDF. Fixes [toRdf#te111](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te111) and [toRdf#te112](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te112).
- In `_create_node_map()`, ignored `@id: None` subjects are now dropped before they enter the node map to prevent `_graph_to_rdf()` from crashing when sorting mixed `None` and string keys. Fixes [toRdf#te122](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te122).
- Use a helper for generalized N-Quads normalization in the test runner, since rdflib cannot parse blank-node predicates. Fixes [toRdf#te075](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te075).
- `jsonld.expand()`, `jsonld.to_rdf()` and the other operations no longer fail with an `OverflowError` for an integer too large for a double. `jsonld.to_rdf()` writes it as `INF` or `-INF`, the infinite `xsd:double` values, or, in `json-ld-1.0` mode and not typed as `xsd:double`, as an exact `xsd:integer`. Inside `@json` literals, `jsonld.to_rdf()` and `jsonld.normalize()` still raise `OverflowError` for such numbers, as RFC 8785 only allows numbers that fit in a double.
- `jsonld.to_rdf()` writes the `xsd:double` NaN as `NaN`, its XSD canonical form, instead of `NAN`, so `jsonld.normalize()` output changes for documents with such a value.

### Changed
- **BREAKING**: Developer install, command execution, documentation, and release artifact
Expand Down
57 changes: 45 additions & 12 deletions lib/pyld/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -4016,17 +4016,11 @@ def _object_to_rdf(self, item, issuer, triples, options):
elif datatype == str(XSD.double):
# since the previous branch did not activate, we know that `value` is not a float number.
try:
float_value = float(value)
lexical = _canonicalize_double(value)
except (ValueError, TypeError):
# if `value` is not convertible to float, we will return it as-is.
return Literal(value, datatype=XSD.double, normalize=False)
else:
# we have a float, and canonicalization may proceed.
return Literal(
_canonicalize_double(float_value),
datatype=XSD.double,
normalize=False,
)
lexical = value
return Literal(lexical, datatype=XSD.double, normalize=False)
elif (
options['processingMode'] == 'json-ld-1.1'
and _is_integer(value)
Expand Down Expand Up @@ -6460,8 +6454,47 @@ def _is_double(v):
return not isinstance(v, Integral) and isinstance(v, Real) and not float(v).is_integer()


def _canonicalize_double(value: float) -> str:
"""Convert a float value to canonical lexical form of `xsd:double`."""
def _to_double(value) -> float:
"""
Converts a number to a double.

An integer that rounds beyond the largest double becomes positive or
negative infinity, as the lexical mapping of `xsd:double` in XML Schema
1.1 Part 2 rounds it with floatingPointRound:
https://www.w3.org/TR/xmlschema11-2/#f-doubleLexmap

:param value: the number or numeric string to convert; anything else
raises ValueError or TypeError, as float() does.

:return: the double.
"""
try:
return float(value)
except OverflowError:
return math.inf if value > 0 else -math.inf


def _canonicalize_double(value: float | int | str) -> str:
"""
Converts a number to the canonical lexical form of `xsd:double`.

A finite number is written as JSON-LD 1.1 describes it:
https://www.w3.org/TR/json-ld11-api/#data-round-tripping

The special values are written 'INF', '-INF' and 'NaN', as
specialRepCanonicalMap in XML Schema 1.1 Part 2 maps them:
https://www.w3.org/TR/xmlschema11-2/#f-specValCanMap

:param value: the number or numeric string to convert, as for
_to_double().

:return: the canonical lexical form.
"""
value = _to_double(value)
if math.isnan(value):
return 'NaN'
if math.isinf(value):
return 'INF' if value > 0 else '-INF'
return re.sub(r'(\d)0*E\+?(-)?0*(\d)', r'\1E\2\3', (f'{value:1.15E}'))


Expand All @@ -6474,7 +6507,7 @@ def _is_numeric(v):
:return: True if the value is numeric, False if not.
"""
try:
float(v)
_to_double(v)
return True
except (TypeError, ValueError):
return False
Expand Down
17 changes: 17 additions & 0 deletions tests/jsonld/test_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,20 @@ def test_default_direction_null_reset_is_noop_when_unset():
]

assert jsonld.expand(input) == expected


def test_integer_beyond_double_range_is_kept():
"""
Integers too large for a double should be kept as numbers during expansion
instead of raising OverflowError.
"""
input = {"@id": "http://example.com/s", "http://example.com/p": 10**309}

expected = [
{
"@id": "http://example.com/s",
"http://example.com/p": [{"@value": 10**309}],
},
]

assert jsonld.expand(input) == expected
79 changes: 79 additions & 0 deletions tests/jsonld/test_to_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,85 @@ def test_large_integer_to_rdf_double_conversion_processing_mode():
"""
assert nquads == expected


def test_integer_beyond_double_range_to_rdf_is_infinite():
"""
Integers too large for a double should be converted to INF and -INF
xsd:double values instead of raising OverflowError.
"""
for value, lexical in ((10**309, 'INF'), (-(10**309), '-INF')):
input = {'@id': 'http://example.com/s', 'http://example.com/p': value}

nquads = jsonld.to_rdf(input, options={'format': 'application/n-quads'})

assert nquads == (
f'<http://example.com/s> <http://example.com/p> "{lexical}"'
'^^<http://www.w3.org/2001/XMLSchema#double> .\n\n'
)


def test_integer_beyond_double_range_typed_as_double_to_rdf_is_infinite():
"""
Integers too large for a double, typed as xsd:double, should be marked as infinite.
"""
input = {
'@id': 'http://example.com/s',
'http://example.com/p': {
'@value': 10**309,
'@type': 'http://www.w3.org/2001/XMLSchema#double',
},
}

nquads = jsonld.to_rdf(input, options={'format': 'application/n-quads'})

assert nquads == (
'<http://example.com/s> <http://example.com/p> "INF"'
'^^<http://www.w3.org/2001/XMLSchema#double> .\n\n'
)


def test_integer_beyond_double_range_in_json_ld_1_0_stays_an_integer():
"""
In json-ld-1.0 processing mode, integers too large for a double should be
kept as exact xsd:integer values.
"""
input = {'@id': 'http://example.com/s', 'http://example.com/p': 10**309}

nquads = jsonld.to_rdf(
input,
options={'format': 'application/n-quads', 'processingMode': 'json-ld-1.0'},
)

assert nquads == (
f'<http://example.com/s> <http://example.com/p> "{10**309}"'
'^^<http://www.w3.org/2001/XMLSchema#integer> .\n\n'
)


def test_double_special_values_to_rdf_use_xsd_canonical_forms():
"""
The special xsd:double values should be written as NaN, INF and -INF, their
XSD canonical forms, whether given as floats or as xsd:double typed strings.
"""
double = 'http://www.w3.org/2001/XMLSchema#double'
for value, lexical in (
(float('nan'), 'NaN'),
(float('inf'), 'INF'),
(float('-inf'), '-INF'),
({'@value': 'NaN', '@type': double}, 'NaN'),
({'@value': 'INF', '@type': double}, 'INF'),
({'@value': '-INF', '@type': double}, '-INF'),
):
input = {'@id': 'http://example.com/s', 'http://example.com/p': value}

nquads = jsonld.to_rdf(input, options={'format': 'application/n-quads'})

assert nquads == (
f'<http://example.com/s> <http://example.com/p> "{lexical}"'
f'^^<{double}> .\n\n'
)


def test_to_rdf_uses_identifier_issuer_option():
input = {'http://example.org/p': [{'@list': ['a', 'b']}]}
issuer = IdentifierIssuer('_:custom')
Expand Down
Loading