W3C home > Mailing lists > Public > public-linked-json@w3.org > January 2019

Re: Type coercion to decimals?

From: Gregg Kellogg <gregg@greggkellogg.net>
Date: Sat, 12 Jan 2019 12:19:46 -0800
Cc: public-linked-json@w3.org
Message-Id: <6E83AFF1-A035-4941-B92A-4575C23D13DB@greggkellogg.net>
To: Dave Reynolds <dave.e.reynolds@gmail.com>
> On Jan 11, 2019, at 3:41 AM, Dave Reynolds <dave.e.reynolds@gmail.com> wrote:
> 
> Hi,
> 
> I have json data with floating point numbers which I'm trying to interpret as jsonld where the jsonld context calls for the relevant properties to be interpreted as xsd:decimal.
> 
> In the tools I'm using, including https://json-ld.org/playground/, this is generating illegal RDF. I'm trying work out if this is a bug in the tools or whether this form of coercion is not allowed.
> 
> A test case is:
> 
> {
>  "@context": {
>    "@vocab": "http://example.com/vocab",
>    "long": {
>      "@id": "http://www.w3.org/2003/01/geo/wgs84_pos#long",
>      "@type": "http://www.w3.org/2001/XMLSchema#decimal"
>    },
>    "lat": {
>      "@id": "http://www.w3.org/2003/01/geo/wgs84_pos#lat",
>      "@type": "http://www.w3.org/2001/XMLSchema#decimal"
>    }
>  },
>  "@graph": [
>    {
>      "@id": "http://example.com/graph/1",
>      "@graph": {
>        "@id": "http://example.com/resource/1",
>        "lat": 51.449604,
>        "long": -2.601738
>      }
>    }
>  ]
> }
> 
> Using the json-ld playground, and using Jena (which in turn depends on jsonld-java), this generates the purported n-quads:
> 
> <http://example.com/resource/1> <http://www.w3.org/2003/01/geo/wgs84_pos#lat> "5.1449604E1"^^<http://www.w3.org/2001/XMLSchema#decimal> <http://example.com/graph/1> .
> 
> <http://example.com/resource/1> <http://www.w3.org/2003/01/geo/wgs84_pos#long> "-2.601738E0"^^<http://www.w3.org/2001/XMLSchema#decimal> <http://example.com/graph/1> .
> 
> The trouble is that the E notation is not legal for XSD decimals so this RDF is syntactically invalid (at least for datatype-aware processors).
> 
> In this case I have some control over the source json so I could pass the lat/long values as strings but that would break any consumers of the json data. If I treat lat/longs as xsd:double instead of xsd:decimal that at least gets me legal RDF, just not the RDF I was looking for.
> 
> Any thoughts on whether this is just a bug in the various tools or something deeper? Given that both jsonld-java and jsonld.js have the same behaviour I'm worried it might be deeper.

The issue stems from the JSON data model which has a single numeric type. The JSON-LD spec provides for native numbers to either be interpreted as integer or double, but not decimal, as you see. Type-casting with @type` does not work for native values. If you expressed the values as strings, then typecasting would work. For example:

{
 "@context": {
   "@vocab": "http://example.com/vocab",
   "long": {
     "@id": "http://www.w3.org/2003/01/geo/wgs84_pos#long",
     "@type": "http://www.w3.org/2001/XMLSchema#decimal"
   },
   "lat": {
     "@id": "http://www.w3.org/2003/01/geo/wgs84_pos#lat",
     "@type": "http://www.w3.org/2001/XMLSchema#decimal"
   }
 },
 "@graph": [
   {
     "@id": "http://example.com/graph/1",
     "@graph": {
       "@id": "http://example.com/resource/1",
       "lat": "51.449604",
       "long": "-2.601738"
     }
   }
 ]
}

Gregg

> Dave
> 
> 
Received on Saturday, 12 January 2019 20:20:12 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 16:18:52 UTC