1use crate::config::{Config, SerializeMode};
16use crate::error::StamError;
17use crate::file::*;
18use crate::types::*;
19
20pub trait ToJson
21where
22 Self: TypeInfo + serde::Serialize,
23{
24 fn to_json_writer<W>(&self, writer: W, compact: bool) -> Result<(), StamError>
27 where
28 W: std::io::Write,
29 {
30 match compact {
31 false => serde_json::to_writer_pretty(writer, &self).map_err(|e| {
32 StamError::SerializationError(format!(
33 "Writing {} to file: {}",
34 Self::typeinfo(),
35 e
36 ))
37 }),
38 true => serde_json::to_writer(writer, &self).map_err(|e| {
39 StamError::SerializationError(format!(
40 "Writing {} to file: {}",
41 Self::typeinfo(),
42 e
43 ))
44 }),
45 }
46 }
47
48 fn to_json_file(&self, filename: &str, config: &Config) -> Result<(), StamError> {
51 debug(config, || {
52 format!(
53 "{}.to_json_file: filename={:?} workdir={:?}",
54 Self::typeinfo(),
55 filename,
56 config.workdir()
57 )
58 });
59 if let Type::TextResource | Type::AnnotationDataSet = Self::typeinfo() {
60 config.set_serialize_mode(SerializeMode::NoInclude); }
63 let compact = match config.dataformat {
64 DataFormat::Json { compact } => compact,
65 _ => {
66 if let Type::AnnotationStore = Self::typeinfo() {
67 return Err(StamError::SerializationError(format!(
68 "Unable to serialize to JSON for {} (filename {}) when config dataformat is set to {}",
69 Self::typeinfo(),
70 filename,
71 config.dataformat
72 )));
73 } else {
74 false
75 }
76 }
77 };
78 let writer = open_file_writer(filename, &config)?;
79 let result = self.to_json_writer(writer, compact);
80 if let Type::TextResource | Type::AnnotationDataSet = Self::typeinfo() {
81 config.set_serialize_mode(SerializeMode::AllowInclude); }
84 result
85 }
86
87 fn to_json_string(&self, config: &Config) -> Result<String, StamError> {
91 if let Type::TextResource | Type::AnnotationDataSet = Self::typeinfo() {
92 config.set_serialize_mode(SerializeMode::NoInclude); }
95 let result = match config.dataformat {
96 DataFormat::Json { compact: false } => {
97 serde_json::to_string_pretty(&self).map_err(|e| {
98 StamError::SerializationError(format!(
99 "Writing {} to string: {}",
100 Self::typeinfo(),
101 e
102 ))
103 })
104 }
105 DataFormat::Json { compact: true } => serde_json::to_string(&self).map_err(|e| {
106 StamError::SerializationError(format!(
107 "Writing {} to string: {}",
108 Self::typeinfo(),
109 e
110 ))
111 }),
112 _ => Err(StamError::SerializationError(format!(
113 "Unable to serialize to JSON for {} when config dataformat is set to {}",
114 Self::typeinfo(),
115 config.dataformat
116 ))),
117 };
118 if let Type::TextResource | Type::AnnotationDataSet = Self::typeinfo() {
119 config.set_serialize_mode(SerializeMode::AllowInclude); }
122 result
123 }
124
125 fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
128 serde_json::to_value(&self).map_err(|e| {
129 StamError::SerializationError(format!(
130 "Writing {} to JSON value: {}",
131 Self::typeinfo(),
132 e
133 ))
134 })
135 }
136}
137
138pub trait FromJson
139where
140 Self: TypeInfo + Sized,
141{
142 fn from_json_file(filename: &str, config: Config) -> Result<Self, StamError>;
143
144 fn from_json_str(string: &str, config: Config) -> Result<Self, StamError>;
145
146 fn merge_json_file(&mut self, _filename: &str) -> Result<(), StamError> {
147 unimplemented!("merge_json_file not implemented")
148 }
149
150 fn merge_json_str(&mut self, _string: &str) -> Result<(), StamError> {
151 unimplemented!("merge_json_str not implemented")
152 }
153}