-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonld.go
151 lines (130 loc) · 3.31 KB
/
jsonld.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package egdm
import (
"encoding/json"
"io"
)
type JsonLdRef struct {
ID string `json:"@id"`
}
type JsonLDWriter struct {
}
func (jsonLDWriter *JsonLDWriter) Write(ec *EntityCollection, writer io.Writer) error {
var err error
// write [
_, err = writer.Write([]byte("["))
if err != nil {
return err
}
// write context
mappings := ec.NamespaceManager.GetNamespaceMappings()
context := jsonLDWriter.makeContext(mappings)
contextJson, _ := json.Marshal(context)
_, err = writer.Write(contextJson)
if err != nil {
return err
}
// write entities
for _, entity := range ec.Entities {
_, err = writer.Write([]byte(","))
if err != nil {
return err
}
entityLD := jsonLDWriter.toJSONLD(entity)
entityJson, err := json.Marshal(entityLD)
if err != nil {
return err
}
_, err = writer.Write(entityJson)
if err != nil {
return err
}
}
// write continuation if not nil
if ec.Continuation != nil {
_, err = writer.Write([]byte(","))
if err != nil {
return err
}
contToken := jsonLDWriter.makeContinuationToken(ec.Continuation.Token)
_, err = writer.Write([]byte(contToken))
if err != nil {
return err
}
}
// write ]
_, err = writer.Write([]byte("]"))
if err != nil {
return err
}
return nil
}
func (jsonLDWriter *JsonLDWriter) makeContext(namespaceMappings map[string]string) map[string]interface{} {
namespaces := make(map[string]string, len(namespaceMappings)+2)
for k, v := range namespaceMappings {
namespaces[k] = v
}
namespaces["core"] = "http://data.mimiro.io/core/uda/"
namespaces["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
jsonLdContext := make(map[string]interface{})
jsonLdContext["@context"] = namespaces
return jsonLdContext
}
func (jsonLDWriter *JsonLDWriter) makeContinuationToken(token string) string {
contToken := make(map[string]interface{})
contToken["rdf:type"] = map[string]string{"@id": "core:continuation"}
contToken["core:token"] = token
jsonData, _ := json.Marshal(contToken)
return string(jsonData)
}
// Entity to JSON-LD representation
func (jsonLDWriter *JsonLDWriter) toJSONLD(entity *Entity) map[string]interface{} {
jsonLd := make(map[string]interface{})
// get the id and add that if not empty
if entity.ID != "" {
jsonLd["@id"] = entity.ID
}
// get props
for key, value := range entity.Properties {
// check the type of value
switch v := value.(type) {
case []interface{}:
// array of values
jsonLd[key] = jsonLDWriter.toJSONLDFromArray(v)
case *Entity:
// entity
jsonLd[key] = jsonLDWriter.toJSONLD(v)
default:
// assume we can just put out the value
jsonLd[key] = v
}
}
// get the refs
for key, value := range entity.References {
// check the type of value
switch v := value.(type) {
case []string:
refs := make([]JsonLdRef, len(v))
for _, ref := range v {
refs = append(refs, JsonLdRef{ID: ref})
}
jsonLd[key] = refs
case string:
jsonLd[key] = JsonLdRef{ID: v}
}
}
return jsonLd
}
func (jsonLDWriter *JsonLDWriter) toJSONLDFromArray(entityArray []interface{}) []interface{} {
jsonLd := make([]interface{}, len(entityArray))
for i, value := range entityArray {
switch v := value.(type) {
case []interface{}:
jsonLd[i] = jsonLDWriter.toJSONLDFromArray(v)
case *Entity:
jsonLd[i] = jsonLDWriter.toJSONLD(v)
default:
jsonLd[i] = value
}
}
return jsonLd
}