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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Serialization / formatting / encoding (JSON, RDF, N-Triples)

use serde_json::Map;
use serde_json::Value as SerdeValue;

use crate::{
    datatype::DataType, errors::AtomicResult, resources::PropVals, Resource, Storelike, Value,
};

/// Serializes a vector or Resources to a JSON-AD string
pub fn resources_to_json_ad(resources: &[Resource]) -> AtomicResult<String> {
    let mut vec: Vec<serde_json::Value> = Vec::new();
    for r in resources {
        vec.push(crate::serialize::propvals_to_json_ad_map(
            r.get_propvals(),
            Some(r.get_subject().clone()),
        )?)
    }
    let serde_array = serde_json::Value::from(vec);
    serde_json::to_string_pretty(&serde_array).map_err(|_| "Could not serialize to JSON-AD".into())
}

/// Converts an Atomic Value to a Serde Value.
fn val_to_serde(value: Value) -> AtomicResult<SerdeValue> {
    let json_val: SerdeValue = match value {
        Value::AtomicUrl(val) => SerdeValue::String(val),
        Value::Date(val) => SerdeValue::String(val),
        // TODO: Handle big numbers
        Value::Integer(val) => serde_json::from_str(&val.to_string()).unwrap_or_default(),
        Value::Float(val) => serde_json::from_str(&val.to_string()).unwrap_or_default(),
        Value::Markdown(val) => SerdeValue::String(val),
        Value::ResourceArray(val) => {
            let mut vec: Vec<SerdeValue> = Vec::new();
            for resource in val {
                match resource {
                    crate::values::SubResource::Resource(r) => {
                        vec.push(crate::serialize::propvals_to_json_ad_map(
                            r.get_propvals(),
                            Some(r.get_subject().clone()),
                        )?);
                    }
                    crate::values::SubResource::Nested(pv) => {
                        vec.push(crate::serialize::propvals_to_json_ad_map(&pv, None)?);
                    }
                    crate::values::SubResource::Subject(s) => {
                        vec.push(SerdeValue::String(s.clone()))
                    }
                }
            }
            SerdeValue::Array(vec)
        }
        Value::Slug(val) => SerdeValue::String(val),
        Value::String(val) => SerdeValue::String(val),
        Value::Timestamp(val) => SerdeValue::Number(val.into()),
        Value::Unsupported(val) => SerdeValue::String(val.value),
        Value::Boolean(val) => SerdeValue::Bool(val),
        // TODO: fix this for nested resources in json and json-ld serialization, because this will cause them to fall back to json-ad
        Value::NestedResource(res) => match res {
            crate::values::SubResource::Resource(r) => crate::serialize::propvals_to_json_ad_map(
                r.get_propvals(),
                Some(r.get_subject().clone()),
            )?,
            crate::values::SubResource::Nested(propvals) => {
                propvals_to_json_ad_map(&propvals, None)?
            }
            crate::values::SubResource::Subject(s) => SerdeValue::String(s),
        },
        Value::Resource(_) => todo!(),
    };
    Ok(json_val)
}

/// Serializes a Resource to a Serde JSON Map
pub fn propvals_to_json_ad_map(
    propvals: &PropVals,
    subject: Option<String>,
) -> AtomicResult<serde_json::Value> {
    let mut root = Map::new();
    for (prop_url, value) in propvals.iter() {
        root.insert(prop_url.clone(), val_to_serde(value.clone())?);
    }
    if let Some(sub) = subject {
        root.insert("@id".into(), SerdeValue::String(sub));
    }
    let obj = SerdeValue::Object(root);
    Ok(obj)
}

/// Serializes a Resource to a Serde JSON Map
pub fn propvals_to_json_ld(
    propvals: &PropVals,
    subject: Option<String>,
    store: &impl Storelike,
    json_ld: bool,
) -> AtomicResult<serde_json::Value> {
    // Initiate JSON object
    let mut root = Map::new();
    // For JSON-LD serialization
    let mut context = Map::new();
    // For every atom, find the key, datatype and add it to the @context
    for (prop_url, value) in propvals.iter() {
        // The property is only needed in JSON-LD and JSON for shortnames
        let property = store.get_property(prop_url)?;
        if json_ld {
            // In JSON-LD, the value of a Context Item can be a string or an object.
            // This object can contain information about the translation or datatype of the value
            let ctx_value: SerdeValue = match value.datatype() {
                DataType::AtomicUrl => {
                    let mut obj = Map::new();
                    obj.insert("@id".into(), prop_url.as_str().into());
                    obj.insert("@type".into(), "@id".into());
                    obj.into()
                }
                DataType::Date => {
                    let mut obj = Map::new();
                    obj.insert("@id".into(), prop_url.as_str().into());
                    obj.insert(
                        "@type".into(),
                        "http://www.w3.org/2001/XMLSchema#date".into(),
                    );
                    obj.into()
                }
                DataType::Integer => {
                    let mut obj = Map::new();
                    obj.insert("@id".into(), prop_url.as_str().into());
                    // I'm not sure whether we should use XSD or Atomic Datatypes
                    obj.insert(
                        "@type".into(),
                        "http://www.w3.org/2001/XMLSchema#integer".into(),
                    );
                    obj.into()
                }
                DataType::Markdown => prop_url.as_str().into(),
                DataType::ResourceArray => {
                    let mut obj = Map::new();
                    obj.insert("@id".into(), prop_url.as_str().into());
                    // Plain JSON-LD Arrays are not ordered. Here, they are converted into an RDF List.
                    obj.insert("@container".into(), "@list".into());
                    obj.into()
                }
                _other => prop_url.as_str().into(),
            };
            context.insert(property.shortname.as_str().into(), ctx_value);
        }
        let key = property.shortname;

        root.insert(key, val_to_serde(value.clone())?);
    }

    if let Some(sub) = subject {
        root.insert("@id".into(), SerdeValue::String(sub));
    }

    if json_ld {
        root.insert("@context".into(), context.into());
    }
    let obj = SerdeValue::Object(root);
    // let string = serde_json::to_string_pretty(&obj).expect("Could not serialize to JSON");
    Ok(obj)
}

pub fn serialize_json_array(items: &[String]) -> AtomicResult<String> {
    let string = serde_json::to_string(items)?;
    Ok(string)
}

#[cfg(feature = "rdf")]
/// Serializes Atoms to Ntriples (which is also valid Turtle / Notation3).
pub fn atoms_to_ntriples(atoms: Vec<crate::Atom>, store: &impl Storelike) -> AtomicResult<String> {
    use rio_api::formatter::TriplesFormatter;
    use rio_api::model::{Literal, NamedNode, Term, Triple};
    use rio_turtle::NTriplesFormatter;

    let mut formatter = NTriplesFormatter::new(Vec::default());
    for atom in atoms {
        let subject = NamedNode { iri: &atom.subject }.into();
        let predicate = NamedNode {
            iri: &atom.property,
        };
        let datatype = store.get_property(&atom.property)?.data_type;
        let value = &atom.value.to_string();
        let datatype_url = datatype.to_string();
        let object: Term = match &datatype {
            DataType::AtomicUrl => NamedNode { iri: value }.into(),
            // Maybe these should be converted to RDF collections / lists?
            // DataType::ResourceArray => {}
            DataType::String => Literal::Simple { value }.into(),
            _dt => Literal::Typed {
                value,
                datatype: NamedNode { iri: &datatype_url },
            }
            .into(),
        };

        formatter.format(&Triple {
            subject,
            predicate,
            object,
        })?
    }
    let out = String::from_utf8(formatter.finish())?;
    Ok(out)
}

#[cfg(feature = "rdf")]
/// Serializes Atoms to Ntriples (which is also valid Turtle / Notation3).
pub fn atoms_to_turtle(atoms: Vec<crate::Atom>, store: &impl Storelike) -> AtomicResult<String> {
    use rio_api::formatter::TriplesFormatter;
    use rio_api::model::{Literal, NamedNode, Term, Triple};
    use rio_turtle::TurtleFormatter;

    let mut formatter = TurtleFormatter::new(Vec::default());

    for atom in atoms {
        let subject = NamedNode { iri: &atom.subject }.into();
        let predicate = NamedNode {
            iri: &atom.property,
        };
        let datatype = store.get_property(&atom.property)?.data_type;
        let value = &atom.value.to_string();
        let datatype_url = datatype.to_string();
        let object: Term = match &datatype {
            DataType::AtomicUrl => NamedNode { iri: value }.into(),
            // Maybe these should be converted to RDF collections / lists?
            // DataType::ResourceArray => {}
            DataType::String => Literal::Simple { value }.into(),
            _dt => Literal::Typed {
                value,
                datatype: NamedNode { iri: &datatype_url },
            }
            .into(),
        };

        formatter.format(&Triple {
            subject,
            predicate,
            object,
        })?
    }
    let out = String::from_utf8(formatter.finish()?)?;
    Ok(out)
}

/// Should list all the supported serialization formats
pub enum Format {
    Json,
    JsonAd,
    JsonLd,
    NTriples,
    Pretty,
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::Storelike;

    #[test]
    fn serialize_json_ad() {
        let store = crate::Store::init().unwrap();
        store.populate().unwrap();
        let json = store
            .get_resource(crate::urls::AGENT)
            .unwrap()
            .to_json_ad()
            .unwrap();
        println!("json-ad: {}", json);
        let correct_json = r#"{
  "@id": "https://atomicdata.dev/classes/Agent",
  "https://atomicdata.dev/properties/description": "An Agent is a user that can create or modify data. It has two keys: a private and a public one. The private key should be kept secret. The public key is used to verify signatures (on [Commits](https://atomicdata.dev/classes/Commit)) set by the of the Agent.",
  "https://atomicdata.dev/properties/isA": [
     "https://atomicdata.dev/classes/Class"
  ],
  "https://atomicdata.dev/properties/parent": "https://atomicdata.dev/classes",
  "https://atomicdata.dev/properties/recommends": [
    "https://atomicdata.dev/properties/name",
    "https://atomicdata.dev/properties/description",
    "https://atomicdata.dev/properties/drives"
  ],
    "https://atomicdata.dev/properties/requires": [
    "https://atomicdata.dev/properties/publicKey"
  ],
  "https://atomicdata.dev/properties/shortname": "agent"
}"#;
        let correct_value: serde_json::Value = serde_json::from_str(correct_json).unwrap();
        let our_value: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(our_value, correct_value)
    }

    #[test]
    fn serialize_json_ad_multiple() {
        let vec = vec![Resource::new("subjet".into())];
        let serialized = resources_to_json_ad(&vec).unwrap();
        let correct_json = r#"[
  {
    "@id": "subjet"
  }
]"#;
        assert_eq!(serialized, correct_json);
    }

    #[test]
    fn serialize_json() {
        let store = crate::Store::init().unwrap();
        store.populate().unwrap();
        let json = store
            .get_resource(crate::urls::AGENT)
            .unwrap()
            .to_json(&store)
            .unwrap();
        println!("json: {}", json);
        let correct_json = r#"{
            "@id": "https://atomicdata.dev/classes/Agent",
            "description": "An Agent is a user that can create or modify data. It has two keys: a private and a public one. The private key should be kept secret. The public key is used to verify signatures (on [Commits](https://atomicdata.dev/classes/Commit)) set by the of the Agent.",
            "is-a": [
              "https://atomicdata.dev/classes/Class"
            ],
            "parent": "https://atomicdata.dev/classes",
            "recommends": [
              "https://atomicdata.dev/properties/name",
              "https://atomicdata.dev/properties/description",
              "https://atomicdata.dev/properties/drives"
            ],
            "requires": [
              "https://atomicdata.dev/properties/publicKey"
            ],
            "shortname": "agent"
          }"#;
        let correct_value: serde_json::Value = serde_json::from_str(correct_json).unwrap();
        let our_value: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(our_value, correct_value)
    }

    #[test]
    fn serialize_json_ld() {
        let store = crate::Store::init().unwrap();
        store.populate().unwrap();
        let json = store
            .get_resource(crate::urls::AGENT)
            .unwrap()
            .to_json_ld(&store)
            .unwrap();
        println!("json: {}", json);
        let correct_json = r#"{
            "@context": {
              "description": "https://atomicdata.dev/properties/description",
              "is-a": {
                "@container": "@list",
                "@id": "https://atomicdata.dev/properties/isA"
              },
              "parent": {
                "@id": "https://atomicdata.dev/properties/parent",
                "@type": "@id"
              },
              "recommends": {
                "@container": "@list",
                "@id": "https://atomicdata.dev/properties/recommends"
              },
              "requires": {
                "@container": "@list",
                "@id": "https://atomicdata.dev/properties/requires"
              },
              "shortname": "https://atomicdata.dev/properties/shortname"
            },
            "@id": "https://atomicdata.dev/classes/Agent",
            "description": "An Agent is a user that can create or modify data. It has two keys: a private and a public one. The private key should be kept secret. The public key is used to verify signatures (on [Commits](https://atomicdata.dev/classes/Commit)) set by the of the Agent.",
            "is-a": [
              "https://atomicdata.dev/classes/Class"
            ],
            "parent": "https://atomicdata.dev/classes",
            "recommends": [
              "https://atomicdata.dev/properties/name",
              "https://atomicdata.dev/properties/description",
              "https://atomicdata.dev/properties/drives"
            ],
            "requires": [
              "https://atomicdata.dev/properties/publicKey"
            ],
            "shortname": "agent"
          }"#;
        let correct_value: serde_json::Value = serde_json::from_str(correct_json).unwrap();
        let our_value: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(our_value, correct_value)
    }

    #[test]
    #[cfg(feature = "rdf")]
    fn serialize_ntriples() {
        use crate::Storelike;
        let store = crate::Store::init().unwrap();
        store.populate().unwrap();
        let subject = crate::urls::DESCRIPTION;
        let resource = store.get_resource(subject).unwrap();
        let atoms = resource.to_atoms().unwrap();
        let serialized = atoms_to_ntriples(atoms, &store).unwrap();
        let _out = r#"
        <https://atomicdata.dev/properties/description> <https://atomicdata.dev/properties/description> "A textual description of the thing."^^<https://atomicdata.dev/datatypes/markdown> .
<https://atomicdata.dev/properties/description> <https://atomicdata.dev/properties/isA> "[\"https://atomicdata.dev/classes/Property\"]"^^<https://atomicdata.dev/datatypes/resourceArray> .
<https://atomicdata.dev/properties/description> <https://atomicdata.dev/properties/datatype> <https://atomicdata.dev/datatypes/markdown> .
<https://atomicdata.dev/properties/description> <https://atomicdata.dev/properties/shortname> "description"^^<https://atomicdata.dev/datatypes/slug> ."#;
        assert!(serialized.contains(r#""description"^^<https://atomicdata.dev/datatypes/slug>"#));
        // This could fail when the `description` resource changes
        assert!(serialized.lines().count() == 5);
    }
}