|
| 1 | +# JSON-goLD |
| 2 | + |
| 3 | +This library is an implementation of the [JSON-LD 1.0](http://json-ld.org/) specification in Go. |
| 4 | +It supports both URDNA2015 and URGNA2012 RDF dataset normalisation algorithms. |
| 5 | + |
| 6 | +## MAINTAINER CHANGE NOTIFICATION ## |
| 7 | + |
| 8 | +We are planning to add new features to this library, including support for Linked Data Signatures, content-addressable JSON-LD, etc. In order to facilitate this development, the maintainer of JSON-goLD (@kazarena) is going to copy this repository to the GitHub organisation of his company (@piprate). See the details and further discussion [here](https://github.com/kazarena/json-gold/issues/20). Feedback welcome. |
| 9 | + |
| 10 | +## Testing & Compliance ## |
| 11 | + |
| 12 | +As of December 1, 2017: |
| 13 | + |
| 14 | +* all JSON-LD 1.0 tests from the [official JSON-LD test suite](https://github.com/json-ld/json-ld.org/tree/master/test-suite) pass |
| 15 | +* all RDF Dataset Normalisation tests from the [current test suite](https://json-ld.github.io/normalization/tests/index.html) pass |
| 16 | +* JSON-LD 1.1 spec is not currenty supported |
| 17 | + |
| 18 | +## Inspiration ## |
| 19 | + |
| 20 | +This implementation was heavily influenced by [JSONLD-Java](https://github.com/jsonld-java/jsonld-java) with some techniques borrowed from [PyLD](https://github.com/digitalbazaar/pyld) and [gojsonld](https://github.com/linkeddata/gojsonld). Big thank you to the contributors of the forementioned libraries for figuring out implementation details of the core algorithms. |
| 21 | + |
| 22 | +## Examples ## |
| 23 | + |
| 24 | +### Expand ### |
| 25 | + |
| 26 | +See complete code in [examples/expand.go](examples/expand.go). |
| 27 | + |
| 28 | +```go |
| 29 | +proc := ld.NewJsonLdProcessor() |
| 30 | +options := ld.NewJsonLdOptions("") |
| 31 | + |
| 32 | +// expanding remote document |
| 33 | + |
| 34 | +expanded, err := proc.Expand("http://json-ld.org/test-suite/tests/expand-0002-in.jsonld", options) |
| 35 | +if err != nil { |
| 36 | + log.Println("Error when expanding JSON-LD document:", err) |
| 37 | + return |
| 38 | +} |
| 39 | + |
| 40 | +// expanding in-memory document |
| 41 | + |
| 42 | +doc := map[string]interface{}{ |
| 43 | + "@context": "http://schema.org/", |
| 44 | + "@type": "Person", |
| 45 | + "name": "Jane Doe", |
| 46 | + "jobTitle": "Professor", |
| 47 | + "telephone": "(425) 123-4567", |
| 48 | + "url": "http://www.janedoe.com", |
| 49 | +} |
| 50 | + |
| 51 | +expanded, err = proc.Expand(doc, options) |
| 52 | +``` |
| 53 | + |
| 54 | +### Compact ### |
| 55 | + |
| 56 | +See complete code in [examples/compact.go](examples/compact.go). |
| 57 | + |
| 58 | +```go |
| 59 | +proc := ld.NewJsonLdProcessor() |
| 60 | +options := ld.NewJsonLdOptions("") |
| 61 | + |
| 62 | +doc := map[string]interface{}{ |
| 63 | + "@id": "http://example.org/test#book", |
| 64 | + "http://example.org/vocab#contains": map[string]interface{}{ |
| 65 | + "@id": "http://example.org/test#chapter", |
| 66 | + }, |
| 67 | + "http://purl.org/dc/elements/1.1/title": "Title", |
| 68 | +} |
| 69 | + |
| 70 | +context := map[string]interface{}{ |
| 71 | + "@context": map[string]interface{}{ |
| 72 | + "dc": "http://purl.org/dc/elements/1.1/", |
| 73 | + "ex": "http://example.org/vocab#", |
| 74 | + "ex:contains": map[string]interface{}{ |
| 75 | + "@type": "@id", |
| 76 | + }, |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +compactedDoc, err := proc.Compact(doc, context, options) |
| 81 | +``` |
| 82 | + |
| 83 | +### Flatten ### |
| 84 | + |
| 85 | +See complete code in [examples/flatten.go](examples/flatten.go). |
| 86 | + |
| 87 | +```go |
| 88 | +proc := ld.NewJsonLdProcessor() |
| 89 | +options := ld.NewJsonLdOptions("") |
| 90 | + |
| 91 | +doc := map[string]interface{}{ |
| 92 | + "@context": []interface{}{ |
| 93 | + map[string]interface{}{ |
| 94 | + "name": "http://xmlns.com/foaf/0.1/name", |
| 95 | + "homepage": map[string]interface{}{ |
| 96 | + "@id": "http://xmlns.com/foaf/0.1/homepage", |
| 97 | + "@type": "@id", |
| 98 | + }, |
| 99 | + }, |
| 100 | + map[string]interface{}{ |
| 101 | + "ical": "http://www.w3.org/2002/12/cal/ical#", |
| 102 | + }, |
| 103 | + }, |
| 104 | + "@id": "http://example.com/speakers#Alice", |
| 105 | + "name": "Alice", |
| 106 | + "homepage": "http://xkcd.com/177/", |
| 107 | + "ical:summary": "Alice Talk", |
| 108 | + "ical:location": "Lyon Convention Centre, Lyon, France", |
| 109 | +} |
| 110 | + |
| 111 | +flattenedDoc, err := proc.Flatten(doc, nil, options) |
| 112 | +``` |
| 113 | + |
| 114 | +### Frame ### |
| 115 | + |
| 116 | +See complete code in [examples/frame.go](examples/frame.go). |
| 117 | + |
| 118 | +```go |
| 119 | +proc := ld.NewJsonLdProcessor() |
| 120 | +options := ld.NewJsonLdOptions("") |
| 121 | + |
| 122 | +doc := map[string]interface{}{ |
| 123 | + "@context": map[string]interface{}{ |
| 124 | + "dc": "http://purl.org/dc/elements/1.1/", |
| 125 | + "ex": "http://example.org/vocab#", |
| 126 | + "ex:contains": map[string]interface{}{"@type": "@id"}, |
| 127 | + }, |
| 128 | + "@graph": []interface{}{ |
| 129 | + map[string]interface{}{ |
| 130 | + "@id": "http://example.org/test/#library", |
| 131 | + "@type": "ex:Library", |
| 132 | + "ex:contains": "http://example.org/test#book", |
| 133 | + }, |
| 134 | + map[string]interface{}{ |
| 135 | + "@id": "http://example.org/test#book", |
| 136 | + "@type": "ex:Book", |
| 137 | + "dc:contributor": "Writer", |
| 138 | + "dc:title": "My Book", |
| 139 | + "ex:contains": "http://example.org/test#chapter", |
| 140 | + }, |
| 141 | + map[string]interface{}{ |
| 142 | + "@id": "http://example.org/test#chapter", |
| 143 | + "@type": "ex:Chapter", |
| 144 | + "dc:description": "Fun", |
| 145 | + "dc:title": "Chapter One", |
| 146 | + }, |
| 147 | + }, |
| 148 | +} |
| 149 | + |
| 150 | +frame := map[string]interface{}{ |
| 151 | + "@context": map[string]interface{}{ |
| 152 | + "dc": "http://purl.org/dc/elements/1.1/", |
| 153 | + "ex": "http://example.org/vocab#", |
| 154 | + }, |
| 155 | + "@type": "ex:Library", |
| 156 | + "ex:contains": map[string]interface{}{ |
| 157 | + "@type": "ex:Book", |
| 158 | + "ex:contains": map[string]interface{}{ |
| 159 | + "@type": "ex:Chapter", |
| 160 | + }, |
| 161 | + }, |
| 162 | +} |
| 163 | + |
| 164 | +framedDoc, err := proc.Frame(doc, frame, options) |
| 165 | +``` |
| 166 | + |
| 167 | +### To RDF ### |
| 168 | + |
| 169 | +See complete code in [examples/to_rdf.go](examples/to_rdf.go). |
| 170 | + |
| 171 | +```go |
| 172 | +proc := ld.NewJsonLdProcessor() |
| 173 | +options := ld.NewJsonLdOptions("") |
| 174 | +options.Format = "application/nquads" |
| 175 | + |
| 176 | +// this JSON-LD document was taken from http://json-ld.org/test-suite/tests/toRdf-0028-in.jsonld |
| 177 | +doc := map[string]interface{}{ |
| 178 | + "@context": map[string]interface{}{ |
| 179 | + "sec": "http://purl.org/security#", |
| 180 | + "xsd": "http://www.w3.org/2001/XMLSchema#", |
| 181 | + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", |
| 182 | + "dc": "http://purl.org/dc/terms/", |
| 183 | + "sec:signer": map[string]interface{}{"@type": "@id"}, |
| 184 | + "dc:created": map[string]interface{}{"@type": "xsd:dateTime"}, |
| 185 | + }, |
| 186 | + "@id": "http://example.org/sig1", |
| 187 | + "@type": []interface{}{"rdf:Graph", "sec:SignedGraph"}, |
| 188 | + "dc:created": "2011-09-23T20:21:34Z", |
| 189 | + "sec:signer": "http://payswarm.example.com/i/john/keys/5", |
| 190 | + "sec:signatureValue": "OGQzNGVkMzVm4NTIyZTkZDYMmMzQzNmExMgoYzI43Q3ODIyOWM32NjI=", |
| 191 | + "@graph": map[string]interface{}{ |
| 192 | + "@id": "http://example.org/fact1", |
| 193 | + "dc:title": "Hello World!", |
| 194 | + }, |
| 195 | +} |
| 196 | +triples, err := proc.ToRDF(doc, options) |
| 197 | +``` |
| 198 | + |
| 199 | +### From RDF ### |
| 200 | + |
| 201 | +See complete code in [examples/from_rdf.go](examples/from_rdf.go). |
| 202 | + |
| 203 | +```go |
| 204 | +proc := ld.NewJsonLdProcessor() |
| 205 | +options := ld.NewJsonLdOptions("") |
| 206 | + |
| 207 | +triples := ` |
| 208 | + <http://example.com/Subj1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Type> . |
| 209 | + <http://example.com/Subj1> <http://example.com/prop1> <http://example.com/Obj1> . |
| 210 | + <http://example.com/Subj1> <http://example.com/prop2> "Plain" . |
| 211 | + <http://example.com/Subj1> <http://example.com/prop2> "2012-05-12"^^<http://www.w3.org/2001/XMLSchema#date> . |
| 212 | + <http://example.com/Subj1> <http://example.com/prop2> "English"@en . |
| 213 | +` |
| 214 | + |
| 215 | +doc, err := proc.FromRDF(triples, options) |
| 216 | +``` |
| 217 | + |
| 218 | +### Normalize ### |
| 219 | + |
| 220 | +See complete code in [examples/normalize.go](examples/normalize.go). |
| 221 | + |
| 222 | +```go |
| 223 | +proc := ld.NewJsonLdProcessor() |
| 224 | +options := ld.NewJsonLdOptions("") |
| 225 | +options.Format = "application/nquads" |
| 226 | +options.Algorithm = "URDNA2015" |
| 227 | + |
| 228 | +doc := map[string]interface{}{ |
| 229 | + "@context": map[string]interface{}{ |
| 230 | + "ex": "http://example.org/vocab#", |
| 231 | + }, |
| 232 | + "@id": "http://example.org/test#example", |
| 233 | + "@type": "ex:Foo", |
| 234 | + "ex:embed": map[string]interface{}{ |
| 235 | + "@type": "ex:Bar", |
| 236 | + }, |
| 237 | +} |
| 238 | + |
| 239 | +normalizedTriples, err := proc.Normalize(doc, options) |
| 240 | +``` |
0 commit comments