Skip to content

Conversation

anatoly-scherbakov
Copy link
Contributor

@anatoly-scherbakov anatoly-scherbakov commented Oct 2, 2025

Why

Here's a JSON-LD document.

{
  "@context": {
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "geoLongitude": "http://www.w3.org/2003/01/geo/wgs84_pos#longitude"
  },
  "@graph": [
    {
      "@id": "http://www.wikidata.org/entity/Q399",
      "geoLongitude": {
        "@type": "xsd:double",
        "@value": "45"
      }
    }
  ]
}

Conversion of this document to a RDF dataset works at JSON-LD playground, but it will crash pyld 2.0.4:

    def _object_to_rdf(self, item, issuer, triples, rdfDirection):
        """
        Converts a JSON-LD value object to an RDF literal or a JSON-LD string
        or node object to an RDF resource.
    
        :param item: the JSON-LD value or node object.
        :param issuer: the IdentifierIssuer for issuing blank node identifiers.
        :param triples: the array of triples to append list entries to.
        :param rdfDirection: for creating datatyped literals.
        :param rdfDirection: for creating datatyped literals.
    
        :return: the RDF literal or RDF resource.
        """
        object = {}
    
        if _is_value(item):
            object['type'] = 'literal'
            value = item['@value']
            datatype = item.get('@type')
    
            # convert to XSD datatypes as appropriate
            if item.get('@type') == '@json':
                object['value'] = canonicalize(value).decode('UTF-8')
                object['datatype'] = RDF_JSON_LITERAL
            elif _is_bool(value):
                object['value'] = 'true' if value else 'false'
                object['datatype'] = datatype or XSD_BOOLEAN
            elif _is_double(value) or datatype == XSD_DOUBLE:
                # canonical double representation
                object['value'] = re.sub(
                    r'(\d)0*E\+?0*(\d)', r'\1E\2',
>                   ('%1.15E' % value))
E               TypeError: must be real number, not str

What

This PR

  • Creates a unit test demonstrating the problem,
  • Contains a fix to it.

@anatoly-scherbakov anatoly-scherbakov changed the title to rdf crashing on string xsd double String xsd:double value crash toRrdf() Oct 2, 2025
@anatoly-scherbakov
Copy link
Contributor Author

@BigBlueHat @davidlehn please let me know what you think. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant