Convert all numeric RDF literal (except xsd:decimals) to numbers
authorMarkus Lanthaler <mark_lanthaler@gmx.net>
Mon, 13 May 2013 21:43:02 -0300
changeset 1662 1a8642aa7c61
parent 1661 7945cd3d5ddf
child 1663 1a52e9b2c4d3
Convert all numeric RDF literal (except xsd:decimals) to numbers

... all numbers are converted to xsd:doubles as proposed by @sandhawke. I choose to not convert xsd:decimals as their primary use is for values where rounding is not acceptable (think, monetary values).

I thought I just go ahead and make those changes since I had some time now and LC2 should be published tomorrow. Feel free to revert the changes if you don't agree with them. The Data Round Tripping section in the API spec includes enough warnings and is clear enough to warn implementors/users of what's gonna happen. It is referenced by all the relevant algorithms.

I will update the LC2 draft in a minute and ensure it passes validation.

/cc @gkellogg @msporny @dlongley
spec/latest/json-ld-api/index.html
spec/latest/json-ld/index.html
--- a/spec/latest/json-ld-api/index.html	Mon May 13 20:54:20 2013 -0300
+++ b/spec/latest/json-ld-api/index.html	Mon May 13 21:43:02 2013 -0300
@@ -173,6 +173,9 @@
     <li>Remove default value of <code class="idlMemberName"><a href="#idl-def-JsonLdOptions">JsonLdOption's</a></code>
       <code class="idlMemberName"><a href="#widl-JsonLdOptions-base">base</a></code> member</li>
     <li>Support lists of lists when converting from RDF to JSON-LD</li>
+    <li>Convert all RDF numeric literals (except <code>xsd:decimal</code>) to <tref title="number">numbers</tref>
+      when converting from RDF; convert all <tref title="number">numbers</tref> to
+      <code>xsd:double</code>-typed literals when converting to RDF</li>
   </ul>
 </section>
 
@@ -3253,24 +3256,13 @@
           <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>
           If <i>datatype</i> is <tref>null</tref>, set it to
           <code>xsd:boolean</code>.</li>
-        <li>Otherwise, if <i>value</i> is a <tref>number</tref> with fractions
-          or <i>value</i> is a <tref>number</tref> and <i>datatype</i> equals
-          <code>xsd:double</code>, convert <i>value</i> to a
-          <tref>string</tref> in <tref>canonical lexical form</tref> of
+        <li>Otherwise, if <i>value</i> is a <tref>number</tref>, convert <i>value</i>
+          to a <tref>string</tref> in <tref>canonical lexical form</tref> of
           an <code>xsd:double</code> as defined in [[!XMLSCHEMA11-2]]
           and described in
           <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>.
           If <i>datatype</i> is <tref>null</tref>, set it to
           <code>xsd:double</code>.</li>
-        <li>Otherwise, if <i>value</i> is a <tref>number</tref> without fractions
-          or <i>value</i> is a <tref>number</tref> and <i>datatype</i> equals
-          <code>xsd:integer</code>, convert <i>value</i> to a
-          <tref>string</tref> in <tref>canonical lexical form</tref> of
-          an <code>xsd:integer</code> as defined in [[!XMLSCHEMA11-2]]
-          and described in
-          <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>.
-          If <i>datatype</i> is <tref>null</tref>, set it to
-          <code>xsd:integer</code>.</li>
         <li>Otherwise, if <i>datatype</i> is <tref>null</tref>, set it to
           <code>xsd:string</code> or <code>rdf:langString</code>, depending on if
           item has an <code>@language</code> member.</li>
@@ -3350,15 +3342,9 @@
         <tref title="RDF literal">RDF literals</tref>, <tref title="IRI">IRIs</tref>
         and <tref title="blank node identifier">blank node identifiers</tref>.
         If the <i>use native types</i> flag is set to <tref>true</tref>,
-        <tref title="RDF literal">RDF literals</tref> with a
-        <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
-        that equals <code>xsd:integer</code> or <code>xsd:double</code> are converted
-        to a <tref title="number">JSON numbers</tref> and <tref title="RDF literal">RDF literals</tref>
-        with a <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
-        that equals <code>xsd:boolean</code> are converted to <tref>true</tref> or
-        <tref>false</tref> based on their
-        <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form">lexical form</tref>
-        as described in
+        some <tref title="RDF literal">RDF literals</tref> are converted to
+        <tref title="number">JSON numbers</tref> or the boolean values <tref>true</tref>
+        or <tref>false</tref>  as described in
         <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>.</p>
     </section>
 
@@ -3535,14 +3521,22 @@
         If the <i>use native types</i> flag is set to <tref>true</tref>,
         <tref title="RDF literal">RDF literals</tref> with a
         <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
-        that equals <code>xsd:integer</code> or <code>xsd:double</code> are converted
+        that equals <code>xsd:integer</code>, <code>xsd:double</code>, <code>xsd:float</code>,
+        <code>xsd:byte</code>, <code>xsd:short</code>, <code>xsd:int</code>, <code>xsd:long</code>,
+        <code>xsd:unsignedByte</code>, <code>xsd:unsignedShort</code>,
+        <code>xsd:unsignedInt</code>, <code>xsd:unsignedLong</code>,
+        <code>xsd:positiveInteger</code>, <code>xsd:nonNegativeInteger</code>,
+        <code>xsd:negativeInteger</code>, or <code>xsd:nonPositiveInteger</code> are converted
         to a <tref title="number">JSON numbers</tref> and <tref title="RDF literal">RDF literals</tref>
         with a <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
         that equals <code>xsd:boolean</code> are converted to <tref>true</tref> or
         <tref>false</tref> based on their
         <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form">lexical form</tref>
-        as described in
-        <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>.</p>
+        as described in <a class="sectionRef" href="#data-round-tripping">Data Round Tripping</a>.
+        <tref title="RDF literal">RDF literals</tref> with a
+        <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
+        that equals <code>xsd:decimal</code> are not converted to
+        <tref title="number">JSON numbers</tref>.</p>
     </section>
 
     <section>
@@ -3580,12 +3574,16 @@
                   set <i>type</i> to <code>xsd:boolean</code>.</li>
                 <li>Otherwise, if the
                   <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
-                  of <i>value</i> equals <code>xsd:integer</code> or
-                  <code>xsd:double</code> and its
+                  of <i>value</i> equals <code>xsd:integer</code>, <code>xsd:double</code>,
+                  <code>xsd:float</code>, <code>xsd:byte</code>, <code>xsd:short</code>,
+                  <code>xsd:int</code>, <code>xsd:long</code>, <code>xsd:unsignedByte</code>,
+                  <code>xsd:unsignedShort</code>, <code>xsd:unsignedInt</code>,
+                  <code>xsd:unsignedLong</code>, <code>xsd:positiveInteger</code>,
+                  <code>xsd:nonNegativeInteger</code>, <code>xsd:negativeInteger</code>,
+                  or <code>xsd:nonPositiveInteger</code> and its
                   <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form">lexical form</tref>
-                  is a valid <code>xsd:integer</code> or <code>xsd:double</code>
-                  according [[!XMLSCHEMA11-2]], set <i>converted value</i>
-                  to the result of converting the
+                  is in the corresponding <tref>canonical lexical form</tref> as defined by
+                  [[!XMLSCHEMA11-2]], set <i>converted value</i> to the result of converting the
                   <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form">lexical form</tref>
                   to a JSON <tref>number</tref>.</li>
               </ol>
@@ -3614,30 +3612,14 @@
 
     <p>When <a href="#convert-to-rdf-algorithm">converting JSON-LD to RDF</a>
       JSON-native <tref title="number">numbers</tref> are automatically
-      type-coerced to <code>xsd:integer</code> or <code>xsd:double</code>
-      depending on whether the <tref>number</tref> has fractions or not,
-      the boolean values <tref>true</tref> and <tref>false</tref> are
-      coerced to <code>xsd:boolean</code>, and <tref title="string">strings</tref>
+      type-coerced to <code>xsd:double</code>, the boolean values
+      <tref>true</tref> and <tref>false</tref> are coerced to
+      <code>xsd:boolean</code>, and <tref title="string">strings</tref>
       are coerced to <code>xsd:string</code>. The numeric or boolean values
       itself are converted to <tdef>canonical lexical form</tdef>, i.e., a
       deterministic string representation as defined in
       [[!XMLSCHEMA11-2]].</p>
 
-    <p>The <tref>canonical lexical form</tref> of an <em>integer</em>, i.e.,
-      a <tref>number</tref> without fractions or a <tref>number</tref> coerced
-      to <code>xsd:integer</code>, is a finite-length sequence of decimal
-      digits (<code>0-9</code>) with an optional leading minus sign; leading
-      zeros are prohibited. In JavaScript, implementers can use the following
-      snippet of code to convert an integer to
-      <tref>canonical lexical form</tref>:</p>
-
-    <pre class="example" data-transform="updateExample"
-         title="Sample integer serialization implementation in JavaScript">
-    <!--
-    (value).toFixed(0).toString()
-    -->
-    </pre>
-
     <p>The <tref>canonical lexical form</tref> of a <em>double</em>, i.e., a
       <tref>number</tref> with fractions or a <tref>number</tref> coerced to
       <code>xsd:double</code>, consists of a mantissa followed by the
@@ -3676,13 +3658,13 @@
       errors might occur. When converting
       <a href="#convert-from-rdf-algorithm">RDF to JSON-LD</a>, similar
       rounding errors might occur. Furthermore, the datatype or the lexical
-      representation might be lost. An <code>xsd:double</code> with a value
-      of <code>2.0</code> will, e.g., result in an <code>xsd:integer</code>
-      with a value of <code>2</code> in <tref>canonical lexical form</tref>
+      representation might be lost. An <code>xsd:integer</code> with a value
+      of <code>2</code> will, e.g., result in an <code>xsd:double</code>
+      with a value of <code>2.0E0</code> in <tref>canonical lexical form</tref>
       when converted from RDF to JSON-LD and back to RDF. It is important
       to highlight that in practice it might be impossible to losslessly
-      convert an <code>xsd:integer</code> to a <tref>number</tref> because
-      its value space is not limited. While the JSON specification [[RFC4627]]
+      convert some RDF literals to <tref title="number">numbers</tref> because
+      their value space is not limited. While the JSON specification [[RFC4627]]
       does not limit the value space of <tref title="number">numbers</tref>
       either, concrete implementations typically do have a limited value
       space.</p>
@@ -3692,11 +3674,21 @@
       specifies a <i>use native types</i> flag which controls whether
       <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-literal">RDF literals</tref>
       with a <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
-      equal to <code>xsd:integer</code>, <code>xsd:double</code>, or
-      <code>xsd:boolean</code> are converted to their JSON-native
-      counterparts. If the <i>use native types</i> flag is set to
-      <tref>false</tref>, all literals remain in their original string
-      representation.</p>
+      equal to <code>xsd:integer</code>, <code>xsd:double</code>, <code>xsd:float</code>,
+      <code>xsd:byte</code>, <code>xsd:short</code>, <code>xsd:int</code>,
+      <code>xsd:long</code>, <code>xsd:unsignedByte</code>, <code>xsd:unsignedShort</code>,
+      <code>xsd:unsignedInt</code>, <code>xsd:unsignedLong</code>, <code>xsd:positiveInteger</code>,
+      <code>xsd:nonNegativeInteger</code>, <code>xsd:negativeInteger</code>,
+      <code>xsd:nonPositiveInteger</code>, or <code>xsd:boolean</code> are converted to
+      their JSON-native counterparts. If the <i>use native types</i> flag is set to
+      <tref>false</tref>, all
+      <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-literal">RDF literals</tref> remain
+      in their original string representation.
+      <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-literal">Literals</tref> with a
+      <tref href="http://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri">datatype IRI</tref>
+      that equals <code>xsd:decimal</code> are not automatically converted to
+      <tref title="number">numbers</tref> because their primary use is for values
+      where rounding errors have to be avoided.</p>
 
     <p>Some JSON serializers, such as PHP's native implementation in some versions,
       backslash-escape the forward slash character. For example, the value
--- a/spec/latest/json-ld/index.html	Mon May 13 20:54:20 2013 -0300
+++ b/spec/latest/json-ld/index.html	Mon May 13 21:43:02 2013 -0300
@@ -160,6 +160,9 @@
     <li>Allow keyword aliases to be defined using expanded term definitions</li>
     <li>Mention profile URIs also in the sections describing the various document forms</li>
     <li>Support relative IRIs in <code>@base</code></li>
+    <li>Convert all RDF numeric literals (except <code>xsd:decimal</code>) to <tref title="number">numbers</tref>
+      when converting from RDF; convert all <tref title="number">numbers</tref> to
+      <code>xsd:double</code>-typed literals when converting to RDF</li>
   </ul>
 </section>
 
@@ -950,7 +953,6 @@
   {
     "@context":
     {
-      "xsd": "http://www.w3.org/2001/XMLSchema#",
       ****"foaf": "http://xmlns.com/foaf/0.1/"****,
       ****"foaf:homepage"****: { "@type": "@id" },
       "picture": { "@id": ****"foaf:depiction"****, "@type": "@id" }
@@ -3378,9 +3380,8 @@
         that numbers and boolean values are represented as typed literals. Thus,
         to allow full round-tripping, the JSON-LD Processing Algorithms and API specification [[JSON-LD-API]]
         defines conversion rules between JSON-LD's native data types and RDF's
-        counterparts. <tref title="number">Numbers</tref> without fractions are
-        converted to <code>xsd:integer</code>-typed literals, numbers with fractions
-        to <code>xsd:double</code>-typed literals and the two boolean values
+        counterparts. <tref title="number">Numbers</tref> are converted to
+        <code>xsd:double</code>-typed literals and the two boolean values
         <tref>true</tref> and <tref>false</tref> to a <code>xsd:boolean</code>-typed
         literal. All typed literals are in canonical lexical form.</p>
 
@@ -3406,7 +3407,7 @@
       @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
 
       <http://example.com/>
-        ex:numbers "14"^^xsd:integer, "2.78E0"^^xsd:double ;
+        ex:numbers "1.4E1"^^xsd:double, "2.78E0"^^xsd:double ;
         ex:booleans "true"^^xsd:boolean, "false"^^xsd:boolean .
       -->
       </pre>