Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RDF to Object conversion (closing #555) #619

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# JetBrains IDEs
.idea/
154 changes: 141 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,48 @@
text-decoration: none;
color: #666;
}

table.simple {
border-collapse: collapse;
width: 100%;
font-size: 14px;
}

table.simple th,
table.simple td {
border: 1px solid #ddd;
padding: 8px;
vertical-align: top; /* Align text to the top */
}

table.simple th {
background-color: #f2f2f2; /* Light gray for headers */
font-weight: bold;
text-align: left;
}

table.simple tr:nth-child(even) {
background-color: #f9f9f9; /* Alternating row colors */
}

table.simple tr:hover {
background-color: #e9e9e9; /* Highlight on hover */
}

table.simple a {
color: #1a73e8; /* Make links a visible color */
text-decoration: none;
}

table.simple a:hover {
text-decoration: underline;
}

table.simple td.error {
background-color: #FFA500;
color: #000;
font-weight: bold;
}
</style>
</head>

Expand Down Expand Up @@ -5156,17 +5198,89 @@ <h3>Overview</h3>
and generating a JSON-LD document in expanded form for all
<a>RDF literals</a>, <a>IRIs</a>
and <a>blank node identifiers</a>.
If the <a data-link-for="JsonLdOptions">useNativeTypes</a> flag is set to <code>true</code>,
<a>RDF literals</a> with a
<a>datatype IRI</a>
that equals <code>xsd:integer</code> or <code>xsd:double</code> are converted
to a <a>JSON numbers</a> and <a>RDF literals</a>
with a <a>datatype IRI</a>
that equals <code>xsd:boolean</code> are converted to <code>true</code> or
<code>false</code> based on their
<a>lexical form</a>
as described in
<a class="sectionRef" href="#data-round-tripping"></a>.

If the <a data-link-for="JsonLdOptions">useNativeTypes</a> flag is set
to <code>true</code>, <a>RDF literals</a> are converted to JSON values
based on their <a>datatype IRI</a> and <a>lexical form</a>, as described
in <a class="sectionRef" href="#algorithm-16"></a>.
</p>

<table class="simple" id="literal-conversation-with-useNativeTypes>
<caption>Literal conversion when <a data-link-for="JsonLdOptions">useNativeTypes</a> is set to <code>true</code></caption>
<thead>
<tr>
<th>RDF datatype</th>
<th>RDF Literal</th>
<th>JSON</th>
</tr>
</thead>
<tbody>
<tr>
<th><code>xsd:string</code></th>
<td>An RDF string literal</td>
<td>JSON string literal</td>
</tr>

<tr>
<th rowspan="3"><code>xsd:integer</code></th>
<td>Whole number in the <code>[-2<sup>53</sup> + 1, 2<sup>53</sup> + 1]</code> range, representable as per [[IEEE-754-2008]]</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more constraints in the lexical form of an xsd:integer than this.

Also, while it's likely consistent with JCS/ECMAScript, such numbers also need to be representable as integers in JCS/ECMAScript. See Appendix B.
We should be careful to not mis-describe this, and just refer to XSD Datatypes 1.1 and JCS.

<td><a>JSON Number</a></td>
</tr>

<tr>
<td>Whole number out of that range</td>
<td>JSON string literal</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this needs to be serialized as a value object with @type. The integer 1e54 (1000000000000000078291540404596243842305360299886116864) is outside of that range, but the value object needs to include `"@type": "xsd:integer".

</tr>

<tr>
<td>Any other value</td>
<td class="error">Throw <a data-link-for="JsonLdErrorCode">invalid JSON literal</a></td>
</tr>
Comment on lines +5235 to +5238
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is a good idea, but I'll comment on that in the algorithm itself, below.


<tr>
<th rowspan="4"><code>xsd:double</code></th>
<td>Real number in the <code>[-2<sup>53</sup> + 1, 2<sup>53</sup> + 1]</code> range, representable as per [[IEEE-754-2008]]</td>
<td><a>JSON Number</a></td>
</tr>

<tr>
<td>Real number out of that range</td>
<td>JSON string literal</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment; the ranges should be consistent with XSD and JCS and probably shouldn't re-define the criteria.

</tr>

<tr>
<td>
Non-numeric values:

<ul>
<li><code>"NaN"^^xsd:double</code>,</li>
<li><code>"+INF"^^xsd:double</code>,</li>
<li><code>"-INF"^^xsd:double</code></li>
</ul>
</td>
<td class="error" rowspan="2">Throw <a data-link-for="JsonLdErrorCode">invalid JSON literal</a></td>
</tr>
Comment on lines +5261 to +5262
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; I don't think that is a good idea, see my comments in the algorithm.

<tr>
<td>Any other value</td>
</tr>

<tr>
<th rowspan="3"><code>xsd:boolean</code></th>
<td><code>true</code></td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>false</code></td>
<td><code>false</code></td>
</tr>
<tr>
<td>Any other value</td>
<td class="error">Throw <a data-link-for="JsonLdErrorCode">invalid JSON literal</a></td>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not what the algorithm is saying!

</tr>
</tbody>
</table>

<p>
Unless the <a data-link-for="JsonLdOptions">useRdfType</a> flag is set to true, <code>rdf:type</code>
predicates will be serialized as <code>@type</code> as long as the associated object is
either an <a>IRI</a> or <a>blank node identifier</a>.</p>
Expand Down Expand Up @@ -5477,10 +5591,24 @@ <h3>Algorithm</h3>
<code>xsd:double</code> and its
<a>lexical form</a>
is a valid <code>xsd:integer</code> or <code>xsd:double</code>
according [[XMLSCHEMA11-2]], set <var>converted value</var>
according to [[XMLSCHEMA11-2]], and if it is a numeric value
conformant to [[IEEE-754-2008]], then set <var>converted value</var>
to the result of converting the
<a>lexical form</a>
to a JSON <a>number</a>.</li>
to a JSON <a>number</a>.
</li>
<li>
Otherwise, if <a>lexical form</a> expresses a numeric value
outside of the ranges supported by [[IEEE-754-2008]], then set
<var>converted value</var> to a JSON string literal containing
said lexical form.
</li>
<li>
Otherwise, if <a>lexical form</a> expresses a non-numeric value,
such as <code>"+INF"^^xsd:double</code>, an
<a data-link-for="JsonLdErrorCode">invalid JSON literal</a>
error has been detected and processing is aborted.
</li>
Comment on lines +5594 to +5611
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right way to change the algorithm, sorry @anatoly-scherbakov .

  1. this is repurposing the invalid JSON literal error, which was initially about "literals of type @json", not about "JSON scalar values"...
  2. we should definitely not raise error on well-formed literals (such as big integers or non-finite doubles).
  3. I would not raise errors on ill-formed numbers either, but encode them as value objects -- this is what the spec currently says, and I don't think we should change that.
  4. to improve round-tripping, I suggest that we use the same rules to differentiate integers and doubles as in the Object to RDF algorithm (currently, an very big xsd:integer would round trip to an xsd:double, which is not great).

So I suggest that step 2.4.3 of the original algorithm should be replaced with:

  • Otherwise, if the datatype IRI of value equals xsd:integer, its lexical form is a valid xsd:integer according to [XMKSCHEMA11-2], and its absolute value is strictly lower than 10^21, the set converted value to the result of converting its lexical form to a JSON number.

  • Otherwise, if the datatype IRI of value equals xsd:double, its lexical form is a valid xsd:double according to [XMLSCHEMA11-2] and is neither "NaN", "INF" not "-INF", then set converted value to the result of converting the lexical form to a JSON number.

Of course, the table and the tests should then be updated accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific range examples should be careful to be informative, to not get ahead of XSD. Also, it will need to be consistent with JCS.

It might be that they way to do this is to just use JCS for serializing strings, and require that the result of serializing to JCS must result that is in the value space of xsd:integer or xsd:double, respectively.

</ol>
</li>
<li class="changed">Otherwise, if <a>processing mode</a> is not `json-ld-1.0`,
Expand Down
20 changes: 20 additions & 0 deletions tests/fromRdf-manifest.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@
"purpose": "Check list generation with rdf:first property and rdf:nil value.",
"input": "fromRdf/0026-in.nq",
"expect": "fromRdf/0026-out.jsonld"
}, {
"@id": "#t0027",
"@type": ["jld:PositiveEvaluationTest", "jld:FromRDFTest"],
"name": "native types flag set to true is ignored if the number provided is invalid in JSON",
"purpose": "Literals with datatype xsd:boolean, xsd:integer, and xsd:double are serialized using native scalar values, if possible",
"option": {
"useNativeTypes": true
},
"input": "fromRdf/0027-in.nq",
"expect": "fromRdf/0027-out.jsonld"
}, {
"@id": "#t0028",
"@type": ["jld:NegativeEvaluationTest", "jld:FromRDFTest"],
"name": "native types flag set to true is ignored if the number provided is invalid in JSON",
"purpose": "+INF^^xsd:double causes a failure to serialize in JSON.",
"option": {
"useNativeTypes": true
},
"input": "fromRdf/0028-in.nq",
"expectErrorCode": "invalid JSON literal"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not invalid, it just can't be serialized as a native value.

}, {
"@id": "#tdi01",
"@type": [ "jld:PositiveEvaluationTest", "jld:FromRDFTest" ],
Expand Down
5 changes: 5 additions & 0 deletions tests/fromRdf/0027-in.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<http://example.com/Subj1> <http://example.com/prop> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .
<http://example.com/Subj1> <http://example.com/prop> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> .
<http://example.com/Subj1> <http://example.com/prop> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com/Subj1> <http://example.com/prop> "1.1"^^<http://www.w3.org/2001/XMLSchema#decimal> .
<http://example.com/Subj1> <http://example.com/prop> "0.1e999999999999999"^^<http://www.w3.org/2001/XMLSchema#double> .
12 changes: 12 additions & 0 deletions tests/fromRdf/0027-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"@id": "http://example.com/Subj1",
"http://example.com/prop": [
{ "@value": true },
{ "@value": false },
{ "@value": 1 },
{ "@value": "1.1", "@type": "http://www.w3.org/2001/XMLSchema#decimal"},
{ "@value": "0.1e999999999999999", "@type": "http://www.w3.org/2001/XMLSchema#double"}
]
}
]
1 change: 1 addition & 0 deletions tests/fromRdf/0028-in.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.com/Subj1> <http://example.com/prop> "+INF"^^<http://www.w3.org/2001/XMLSchema#double> .
Loading