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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(** Serialization to JSON-LD. *)
open T
type 'a smap = { mutable map : 'a SMap.t }
let smap_empty () = { map = SMap.empty }
let smap_singleton entry v = { map = SMap.singleton entry v }
let smap_find e m = SMap.find_opt e m.map
let smap_add e v m = m.map <- SMap.add e v m.map
type AnyMap.k +=
| T of Rdf.Term.term
| I of Iri.t
| B of Rdf.Term.blank_id
| N of string
let () =
let c a b =
match a, b with
| T t1, T t2 -> Some (Rdf.Term.compare t1 t2)
| T _, _ -> Some (-1)
| _, T _ -> Some (1)
| I i1, I i2 -> Some (Iri.compare i1 i2)
| I _, _ -> Some (-1)
| _, I _ -> Some 1
| B id1, B id2 -> Some (Stdlib.compare id1 id2)
| B _, _ -> Some (-1)
| _, B _ -> Some 1
| N s1, N s2 -> Some (String.compare s1 s2)
| N _ , _ -> Some (-1)
| _, N _ -> Some 1
| _ -> None
in
AnyMap.register_compare c
type 'a map = { mutable amap : 'a AnyMap.t }
let map_empty () = { amap = AnyMap.empty }
let map_singleton t v = { amap = AnyMap.singleton t v }
let map_find t m = AnyMap.find_opt t m.amap
let map_add t v m = m.amap <- AnyMap.add t v m.amap
type node = J.json list map
let j_iri iri = J.string (Iri.to_string iri)
let j_term = function
| Rdf.Term.Iri iri -> j_iri iri
| Blank -> assert false
| Blank_ id -> J.string ("_:"^(Rdf.Term.string_of_blank_id id))
| Literal _ -> assert false
let rdf_to_object options value =
match value with
| Rdf.Term.Iri _ | Blank_ _ ->
T.smap_to_json (smap_singleton "@id" (j_term value)).map
| Blank -> assert false
| Literal lit ->
let result = smap_empty () in
let conv_type =
if options.use_native_types then
(
match lit.lit_type with
| Some iri when Iri.equal iri Rdf.Rdf_.xsd_string ->
Some (J.string lit.lit_value, None)
| Some iri when Iri.equal iri Rdf.Rdf_.xsd_boolean ->
Some
(match lit.lit_value with
| "true" -> J.bool true, None
| "false" -> J.bool false, None
| _ -> J.string lit.lit_value, Some iri
)
| Some iri when Iri.equal iri Rdf.Rdf_.xsd_integer
|| Iri.equal iri Rdf.Rdf_.xsd_double ->
(match float_of_string lit.lit_value with
| exception _ -> None
| f -> Some (J.float f, None)
)
| _ -> None
)
else
None
in
let (conv, type_) =
match conv_type with
| Some (json, t) -> (json, Option.map Iri.to_string t)
| None ->
match lit.lit_type with
| Some iri when Iri.equal iri Rdf.Rdf_.dt_JSON &&
options.processing_mode <> "json-ld-1.0" ->
(
match J.from_string lit.lit_value with
| Ok json -> (json, Some "@json")
| Error _ -> error (Invalid_json_literal lit.lit_value)
)
| Some iri when options.rdf_direction = Some I18n_datatype
&& Rdf.Utf8.utf8_is_prefix (Iri.to_string iri) T.i18n_ns ->
(match Iri.fragment iri with
| None -> ()
| Some frag ->
(match Rdf.Utf8.utf8_strbefore frag "_" with
| "" -> ()
| tag -> smap_add "@language" (J.string tag) result
);
(match Rdf.Utf8.utf8_strafter frag "_" with
| "" -> ()
| s -> smap_add "@direction" (J.string s) result
)
);
(J.string lit.lit_value, None)
| _ ->
match lit.lit_language with
| Some tag ->
smap_add "@language" (J.string tag) result;
(J.string lit.lit_value, None)
| None ->
let type_ = match lit.lit_type with
| Some iri when Iri.equal iri Rdf.Rdf_.xsd_string -> None
| Some iri -> Some (Iri.to_string iri)
| None -> None
in
(J.string lit.lit_value, type_)
in
smap_add "@value" conv result ;
(match type_ with
| None -> ()
| Some s -> smap_add "@type" (J.string s) result);
T.smap_to_json result.map
let from_rdf options dataset =
let f_triple (sub, pred, obj) =
let id = j_term sub in
let obj = rdf_to_object options obj in
J.obj [
J.ranged "@id", id ;
J.ranged (Iri.to_string pred), obj ;
]
in
let f_graph acc name g =
let triples = List.map f_triple (g.Rdf.Graph.find()) in
match name with
| None-> acc @ triples
| Some name ->
let graph = [J.ranged "@graph", J.list triples] in
let fields =
let s = match name with
| `I iri -> Iri.to_string iri
| `B id -> "_:"^(Rdf.Term.string_of_blank_id id)
in
(J.ranged "@id", (J.string s)) :: graph
in
(J.obj fields) :: acc
in
let graphs = Rdf.Ds.fold f_graph [] dataset in
J.list graphs