Re: Type coercion to native JSON numbers via JSON-LD API?

On 01/15/2015 04:48 PM, Stian Soiland-Reyes wrote:
> No, I don't believe there is an specified mechanism for that currently.

There is if converting from RDF, see below.

> 
> Probably there should be. Given your context, the
> http://json-ld.org/playground/ seems to keep any incoming JSON numbers
> 42  as the number 42 (even if it is as { "@type": "xsd:integer",
> "@value": 42} ), and any incoming strings "42" as "42".
> 
> I would argue that in this case it should at least consistently end up
> with always the string or always the integer version.

It is consistent in that expansion/compaction won't transform valid JSON
primitives, they are left alone. The expansion and compaction algorithms
don't know anything about the meaning of "xsd:integer". Data types are
opaque to those algorithms. They are, however, dealt with in a minor way
in the to/from RDF conversion algorithms:

If you're converting from RDF and you specify the `use native types`
flag as `true`, then xsd:integers will be converted to native JSON numbers.

>From the spec:

"To ensure lossless round-tripping the Serialize RDF as JSON-LD
algorithm specifies a use native types flag which controls whether RDF
literals with a datatype IRI equal to xsd:integer, xsd:double, or
xsd:boolean are converted to their JSON-native counterparts. If the use
native types flag is set to false, all literals remain in their original
string representation."

http://www.w3.org/TR/json-ld-api/#data-round-tripping

> 
> 
> Could it be the job of a JSON-LD Frame to specify this? Currently it
> can't say much about the values, e.g. if it should be expanded with
> @value or not.
> 
> 
> 
> On 15 January 2015 at 16:55, Jindřich Mynarz <mynarzjindrich@gmail.com> wrote:
>> Hi,
>>
>> is it possible to use JSON-LD API to coerce number-like strings into native
>> JSON numbers (http://www.w3.org/TR/json-ld/#dfn-number)? The section of the
>> specification on type coercion (http://www.w3.org/TR/json-ld/#type-coercion)
>> seems to suggest that this is possible, but so far I haven't realized how.
>>
>> For example, is it possible to coerce the values of the "age" attribute to
>> numbers in the following JSON-LD?
>>
>> {
>>   "@context": {
>>     "@vocab": "http://xmlns.com/foaf/0.1/",
>>     "xsd": "http://www.w3.org/2001/XMLSchema#",
>>     "age": { "@type": "xsd:integer" }
>>   },
>>   "age": "42"
>> }
>>
>> Setting a numeric datatype (such as xsd:integer) for the property via @type
>> in @context doesn't do the job.
>>
>> Is there a way to go about this?
>>
>> Best,
>>
>> Jindřich
>>
>> --
>> http://mynarz.net/#jindrich
> 
> 
> 


-- 
Dave Longley
CTO
Digital Bazaar, Inc.

Received on Thursday, 15 January 2015 23:12:58 UTC