Skip to content

JSON-LD output writes xsd:int, xsd:short, xsd:float and integral xsd:double values as bare numbers, so they read back as a different datatype #2294

Closed

Description

@jdorety

What happens

The JSON-LD formatter writes every integer and every finite double as a bare JSON number, whatever its datatype. A JSON-LD reader gives a bare number its own datatype, so the value comes back as a different one:

Stored JSON-LD written Fluree reads it back as jsonld.js 8.3.3 reads it as
"4"^^xsd:int 4 xsd:integer xsd:integer
"7"^^xsd:short 7 xsd:integer xsd:integer
"1.5"^^xsd:float 1.5 xsd:double xsd:double
"2.0"^^xsd:double 2.0 xsd:double "2"^^xsd:integer
"1.0"^^xsd:decimal {"@value": "1.0", "@type": "…#decimal"} xsd:decimal xsd:decimal

Only the last row survives. The Turtle writer doesn't have this problem: it writes a literal bare only when the bare form reads back as the same value and datatype.

Reproduce

With the CLI (fluree 4.2.5), in an empty directory:

fluree init && fluree create src && fluree create dst
cat > model.ttl <<'TTL'
@prefix ex: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:w ex:parts "4"^^xsd:int ;
     ex:size "7"^^xsd:short ;
     ex:weight "1.5"^^xsd:float ;
     ex:ratio "2.0"^^xsd:double ;
     ex:dec "1.0"^^xsd:decimal .
TTL
fluree insert src -f model.ttl
fluree query src --sparql --format jsonld \
  -e 'CONSTRUCT { ?s ?p ?o } FROM <src:main> WHERE { ?s ?p ?o }' > construct.json
fluree upsert dst -f construct.json --format jsonld
fluree query dst --sparql --format table \
  -e 'SELECT ?p ?o (DATATYPE(?o) AS ?dt) FROM <dst:main> WHERE { ?s ?p ?o } ORDER BY ?p'

construct.json holds "http://example.org/parts": [4], "…/size": [7], "…/weight": [1.5] and "…/ratio": [2.0], and writes dec with its @type. The last query shows:

| http://www.w3.org/2001/XMLSchema#decimal | 1.0 | http://example.org/dec    |
| http://www.w3.org/2001/XMLSchema#integer | 4   | http://example.org/parts  |
| http://www.w3.org/2001/XMLSchema#double  | 2   | http://example.org/ratio  |
| http://www.w3.org/2001/XMLSchema#integer | 7   | http://example.org/size   |
| http://www.w3.org/2001/XMLSchema#double  | 1.5 | http://example.org/weight |

The same query on src shows #int, #short and #float for those three. Same result with fluree 4.2.4-beta.2. format_literal is unchanged at v4.2.5 (ca682e2b8) and on main (6f8ebfffa). fluree/solo pins the xsd:int case in a test through its query Lambda (src/rust/lambda/fluree-lambda-query/tests/model_export_round_trip.rs).

Cause

format_literal in fluree-graph-format/src/jsonld.rs (line 594 on main) matches on the value and ignores the datatype for numbers:

LiteralValue::Integer(i) => json!(i),
LiteralValue::Double(d) => { /* NaN and INF get "@type"; every other value: */ json!(d) }

Strings already check the datatype (if datatype.is_xsd_string() { bare } else { {"@value", "@type"} }). The numeric arms don't.

For comparison, push_shorthand in fluree-graph-format/src/rdf_text.rs (line 311 on main) writes a literal bare only when "Turtle's INTEGER, DECIMAL, DOUBLE or BOOLEAN form reads back as exactly this value and datatype".

The integral xsd:double case

JSON-LD 1.1 converts a native number to RDF by its value: xsd:double when it has a non-zero fractional part or a magnitude of at least 10^21, otherwise xsd:integer (JSON-LD 1.1 Processing Algorithms and API, §8.2.2 Object to RDF Conversion, steps 10–11). The exception is a property whose context declares "@type": "xsd:double", and the CONSTRUCT output has no such context. A JSON parser doesn't keep the .0 of 2.0, so a standard reader gets "2"^^xsd:integer. jsonld.js 8.3.3 toRDF on the engine's output does exactly that. Fluree's own JSON-LD reader keeps it as xsd:double (the ratio row above), so a Fluree-to-Fluree round trip hides it.

Suggested fix

Write a number bare only when a JSON-LD reader will give it back the same datatype, and otherwise write {"@value": "<lexical form>", "@type": "<datatype>"}, the way typed strings are written now:

  • LiteralValue::Integer: bare only when the datatype is xsd:integer.
  • LiteralValue::Double: bare only when the datatype is xsd:double, the value has a non-zero fractional part, and its magnitude is under 10^21.
  • LiteralValue::Boolean: bare only when the datatype is xsd:boolean, if a boolean can carry another datatype at all.

literal_value_to_json (the @value of a language-tagged literal) is not affected, since a language-tagged literal is a string.

Related: #2233 (the same writer compacting a namespace IRI to a bare prefix). Found from fluree/solo#1615.

🤖 Generated with Claude Code

Activity

  1. added this to the 5.0.0 milestone on Nov 12, 2026
  2. bplatz commented on Nov 14, 2026

    @bplatz
    Contributor

    Fixed by #2364 (merged into next): every JSON-LD writer now keeps a number's datatype. Closing by hand because the closing keyword only fires on the default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions