Example #1
0
func main() {
	proc := ld.NewJsonLdProcessor()
	options := ld.NewJsonLdOptions("")

	// expanding remote document

	expanded, err := proc.Expand("http://json-ld.org/test-suite/tests/expand-0002-in.jsonld", options)
	if err != nil {
		log.Println("Error when expanding JSON-LD document:", err)
		return
	}

	ld.PrintDocument("JSON-LD expansion succeeded", expanded)

	// expanding in-memory document

	doc := make(map[string]interface{})
	doc["@context"] = "http://schema.org/"
	doc["@type"] = "Person"
	doc["name"] = "Jane Doe"
	doc["jobTitle"] = "Professor"
	doc["telephone"] = "(425) 123-4567"
	doc["url"] = "http://www.janedoe.com"

	expanded, err = proc.Expand(doc, options)
	if err != nil {
		panic(err)
	}

	ld.PrintDocument("JSON-LD expansion succeeded", expanded)
}
Example #2
0
func main() {
	proc := ld.NewJsonLdProcessor()
	options := ld.NewJsonLdOptions("")
	options.Format = "application/nquads"

	// this JSON-LD document was taken from http://json-ld.org/test-suite/tests/toRdf-0028-in.jsonld
	doc := map[string]interface{}{
		"@context": map[string]interface{}{
			"sec":        "http://purl.org/security#",
			"xsd":        "http://www.w3.org/2001/XMLSchema#",
			"rdf":        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
			"dc":         "http://purl.org/dc/terms/",
			"sec:signer": map[string]interface{}{"@type": "@id"},
			"dc:created": map[string]interface{}{"@type": "xsd:dateTime"},
		},
		"@id":                "http://example.org/sig1",
		"@type":              []interface{}{"rdf:Graph", "sec:SignedGraph"},
		"dc:created":         "2011-09-23T20:21:34Z",
		"sec:signer":         "http://payswarm.example.com/i/john/keys/5",
		"sec:signatureValue": "OGQzNGVkMzVm4NTIyZTkZDYMmMzQzNmExMgoYzI43Q3ODIyOWM32NjI=",
		"@graph": map[string]interface{}{
			"@id":      "http://example.org/fact1",
			"dc:title": "Hello World!",
		},
	}
	triples, err := proc.ToRDF(doc, options)
	if err != nil {
		log.Println("Error when transforming JSON-LD document to RDF:", err)
		return
	}

	os.Stdout.WriteString(triples.(string))
}
Example #3
0
func main() {
	proc := ld.NewJsonLdProcessor()
	options := ld.NewJsonLdOptions("")

	expanded, err := proc.Expand("http://json-ld.org/test-suite/tests/expand-0002-in.jsonld", options)
	if err != nil {
		log.Println("Error when expanding JSON-LD document:", err)
		return
	}

	ld.PrintDocument("JSON-LD expansion succeeded", expanded)
}
Example #4
0
/*
Canonicalize filters and transforms an unmarshalled JSON LD graph int o a consistent form for processing. This is done in three steps:

1. Expand removes all context if any has been included.

2. Frame extracts only the node(s) or value object(s) of the types specified. The outgoing edges of these nodes are
fully 'unrolled'. If the unrolled edges include a node with multiple incoming edges, a copy of the node is attached to each edge.

3. Compact is used to remove array 'wrappers' from singleton arrays.

The input must be unmarshalled JSON.
If only one node matches the typeFilter, it is returned; if no nodes are matched, the result is nil; otherwise an array of the matched nodes are returned.
*/
func Canonicalize(input interface{}, typeFilter []TypeID) (interface{}, error) {
	var (
		jsonLdProcessor = ld.NewJsonLdProcessor()
		err             error
		frame           = make(map[string]interface{}, 1)
		types           = make([]interface{}, len(typeFilter))
		expanded        []interface{}
		framed          map[string]interface{}
		graph           []interface{}
	)

	//Convert the array of TypeIDs to an array of their URI values
	for i, typeID := range typeFilter {
		types[i] = typeID.URI()
	}
	frame["@type"] = types

	expanded, err = jsonLdProcessor.Expand(input, nil)
	if err != nil {
		return nil, err
	}

	framed, err = jsonLdProcessor.Frame(expanded, frame, nil)
	if err != nil {
		return nil, err
	}
	graph = framed["@graph"].([]interface{})
	switch len(graph) {
	case 0:
		return nil, nil
	case 1:
		return graph[0], nil
	default:
		return graph, nil
	}
}