gojsonld icon indicating copy to clipboard operation
gojsonld copied to clipboard

List from RDF doesn't work

Open iddan opened this issue 5 years ago • 3 comments

FromRDF() doesn't parse lists

Example

N-Quads:

<http://example.com/alice> <http://example.com#friends> _:1 .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.com/bob> .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .

Go: Playground

package main

import (
	"encoding/json"
	"fmt"

	"github.com/linkeddata/gojsonld"
)

func main() {
	nquads := `<http://example.com/alice> <http://example.com#friends> _:1 .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.com/bob> .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .`
	dataset, err := gojsonld.ParseDataset([]byte(nquads))
	if err != nil {
		panic(err)
	}
	doc := gojsonld.FromRDF(dataset, &gojsonld.Options{})
	d, _ := json.MarshalIndent(doc, "", "    ")
	fmt.Printf("%v\n", string(d))
}

Result:

[
    {
        "@id": "1",
        "http://www.w3.org/1999/02/22-rdf-syntax-ns#first": [
            {
                "@id": "http://example.com/bob"
            }
        ],
        "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest": [
            {
                "@list": []
            }
        ]
    },
    {
        "@id": "http://example.com/alice",
        "http://example.com#friends": [
            {
                "@id": "1"
            }
        ]
    }
]

Node.js: RunKit

const jsonld = require("jsonld");
const result = await jsonld.fromRDF(`<http://example.com/alice> <http://example.com#friends> _:1 .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <http://example.com/bob> .
	_:1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .`);
JSON.stringify(result, null, 4)

Result:

[
    {
        "@id": "http://example.com/alice",
        "http://example.com#friends": [
            {
                "@list": [
                    {
                        "@id": "http://example.com/bob"
                    }
                ]
            }
        ]
    }
]

iddan avatar Mar 19 '20 02:03 iddan

Hi @iddan, thank you for opening this issue!

I'm afraid I won't have time to look into it short term, so it would be super awesome if you think you can provide a PR with the fix. :)

deiu avatar Mar 19 '20 12:03 deiu

Hey @deiu! Thanks for the quick reply. I will try to get around this to unblock a PR in Cayley.

iddan avatar Mar 19 '20 15:03 iddan

I found out https://github.com/piprate/json-gold got it right. Will consider whether it's better implementing it here or use their implementation.

iddan avatar Mar 21 '20 13:03 iddan