gojsonld
gojsonld copied to clipboard
List from RDF doesn't work
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"
}
]
}
]
}
]
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. :)
Hey @deiu! Thanks for the quick reply. I will try to get around this to unblock a PR in Cayley.
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.