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
use crate::{Error, Result};
use serde::{Serialize, de::DeserializeOwned};
use std::io::Write;
/// Create a STAC object from JSON.
pub trait FromJson: DeserializeOwned {
/// Creates an object from JSON bytes.
///
/// # Examples
///
/// ```
/// use std::{fs::File, io::Read};
/// use stac::{Item, FromJson};
///
/// let mut buf = Vec::new();
/// File::open("examples/simple-item.json").unwrap().read_to_end(&mut buf).unwrap();
/// let item = Item::from_json_slice(&buf).unwrap();
/// ```
fn from_json_slice(slice: &[u8]) -> Result<Self> {
serde_json::from_slice(slice).map_err(Error::from)
}
}
/// Writes a STAC object to JSON bytes.
pub trait ToJson: Serialize {
/// Writes a value as JSON.
///
/// # Examples
///
/// ```
/// use stac::{ToJson, Item};
///
/// let mut buf = Vec::new();
/// Item::new("an-id").to_json_writer(&mut buf, true).unwrap();
/// ```
fn to_json_writer(&self, writer: impl Write, pretty: bool) -> Result<()> {
if pretty {
serde_json::to_writer_pretty(writer, self).map_err(Error::from)
} else {
serde_json::to_writer(writer, self).map_err(Error::from)
}
}
/// Writes a value as JSON bytes.
///
/// # Examples
///
/// ```
/// use stac::{ToJson, Item};
///
/// Item::new("an-id").to_json_vec(true).unwrap();
/// ```
fn to_json_vec(&self, pretty: bool) -> Result<Vec<u8>> {
if pretty {
serde_json::to_vec_pretty(self).map_err(Error::from)
} else {
serde_json::to_vec(self).map_err(Error::from)
}
}
}
impl<T: DeserializeOwned> FromJson for T {}
impl<T: Serialize> ToJson for T {}