Skip to content
Merged
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
31 changes: 21 additions & 10 deletions lib/pyld/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import copy
import json
import math
import re
import sys
import uuid
Expand All @@ -35,7 +36,12 @@
from pyld.__about__ import __copyright__, __license__, __version__
from pyld.canon import URDNA2015, URGNA2012, UnknownFormatError
from pyld.identifier_issuer import IdentifierIssuer
from pyld.nquads import ParserError, parse_nquads, serialize_nquad, serialize_nquads
from pyld.nquads import (
ParserError,
parse_nquads,
serialize_nquad,
serialize_nquads,
)

from .context_resolver import ContextResolver
from .iri_resolver import resolve, unresolve
Expand Down Expand Up @@ -4113,19 +4119,24 @@ def _rdf_to_object(self, o, use_native_types, rdf_direction):

# use native types for certain xsd types
if use_native_types:
converted = False
if type_ == XSD_BOOLEAN:
if rval['@value'] == 'true':
if rval['@value'] in ['true', '1']:
rval['@value'] = True
elif rval['@value'] == 'false':
converted = True
elif rval['@value'] in ['false', '0']:
rval['@value'] = False
elif _is_numeric(rval['@value']):
if type_ == XSD_INTEGER:
if rval['@value'].isdigit():
rval['@value'] = int(rval['@value'])
elif type_ == XSD_DOUBLE:
rval['@value'] = float(rval['@value'])
converted = True
elif type_ == XSD_INTEGER and re.match(r'^[+-]?\d+$', rval['@value']):
rval['@value'] = int(rval['@value'])
converted = True
elif type_ == XSD_DOUBLE and _is_numeric(rval['@value']):
value = float(rval['@value'])
if math.isfinite(value):
rval['@value'] = value
converted = True
# do not add native type
if type_ not in [XSD_BOOLEAN, XSD_INTEGER, XSD_DOUBLE, XSD_STRING]:
if not converted and type_ != XSD_STRING:
rval['@type'] = type_
elif rdf_direction == 'i18n-datatype' and type_.startswith(
'https://www.w3.org/ns/i18n#'
Expand Down
5 changes: 1 addition & 4 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,7 @@ def write(self, filename):
'jld:FromRDFTest': {
'skip': {
'specVersion': ['json-ld-1.0'],
'idRegex': [
# uncategorized
'.*fromRdf-manifest#t0027$',
],
'idRegex': [],
},
'fn': 'from_rdf',
'params': [
Expand Down
Loading