Skip to content

Commit

Permalink
implemented to_json_value() method
Browse files Browse the repository at this point in the history
Useful for other APIs that consume serde_json::Value , prevents
unnecessary serialisation/deserialisation.
  • Loading branch information
proycon committed Nov 14, 2024
1 parent 6cd14da commit 7dda1f9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ impl Annotation {
StamError::SerializationError(format!("Writing annotation to string: {}", e))
})
}

/// Returns JSON value for the Annotation
pub fn to_json_value(&self, store: &AnnotationStore) -> Result<serde_json::Value, StamError> {
//note: this function is not invoked during regular serialisation via the store
let wrapped: ResultItem<Self> = ResultItem::new_partial(self, store);
serde_json::to_value(&wrapped)
.map_err(|e| StamError::SerializationError(format!("Producing Json Value: {}", e)))
}
}

impl<'a> AnnotationBuilder<'a> {
Expand Down
6 changes: 6 additions & 0 deletions src/annotationdataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ impl AnnotationDataSet {
StamError::SerializationError(format!("Writing annotationdataset to string: {}", e))
})
}

/// Writes a dataset to a JSON value
pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
serde_json::to_value(self)
.map_err(|e| StamError::SerializationError(format!("Producing Json Value: {}", e)))
}
}
impl ToJson for AnnotationDataSet {}

Expand Down
5 changes: 5 additions & 0 deletions src/api/annotationdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ impl<'store> ResultItem<'store, AnnotationData> {
StamError::SerializationError(format!("Writing annotationdata to string: {}", e))
})
}

pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
serde_json::to_value(self)
.map_err(|e| StamError::SerializationError(format!("Producing Json Value: {}", e)))
}
}

/// Holds a collection of [`AnnotationData`] (by reference to an [`AnnotationStore`] and handles). This structure is produced by calling
Expand Down
17 changes: 17 additions & 0 deletions src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,23 @@ impl<'store> QueryResultItem<'store> {
)),
}
}

pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
match self {
Self::Annotation(annotation) => annotation.as_ref().to_json_value(annotation.store()),
Self::DataKey(key) => key.as_ref().to_json_value(),
Self::TextResource(resource) => resource.as_ref().to_json_value(),
Self::AnnotationData(data) => data.to_json_value(),
Self::AnnotationDataSet(dataset) => dataset.as_ref().to_json_value(),
Self::TextSelection(textselection) => textselection.to_json_value(),
Self::None => Err(StamError::OtherError(
"QueryResultItem::None can not be serialised",
)),
Self::AnnotationSubStore(_) => Err(StamError::OtherError(
"Serialisation of substores not yet implemented",
)),
}
}
}

#[derive(Clone, Debug)]
Expand Down
13 changes: 13 additions & 0 deletions src/api/textselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl<'store> ResultTextSelection<'store> {
/// Writes a textselection to a STAM JSON string, with appropriate formatting, note that
/// this is not used in normal serialisation (Text Selections aren't serialised).
pub fn to_json(&self) -> Result<String, StamError> {
//MAYBE TODO: do we need this? isn't to_json_string better?
serde_json::to_string_pretty(&self).map_err(|e| {
StamError::SerializationError(format!("Serializing textselection to string: {}", e))
})
Expand Down Expand Up @@ -358,6 +359,18 @@ impl<'store> ResultTextSelection<'store> {
StamError::SerializationError(format!("Writing textannotation to string: {}", e))
})
}

pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
let json = TextSelectionJson {
resource: self.resource().id().expect("resource must have ID"),
begin: self.begin(),
end: self.end(),
text: self.text(),
};
serde_json::to_value(&json).map_err(|e| {
StamError::SerializationError(format!("Writing textannotation to JSON value: {}", e))
})
}
}

#[derive(Serialize)]
Expand Down
6 changes: 6 additions & 0 deletions src/datakey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ impl DataKey {
serde_json::to_string_pretty(self)
.map_err(|e| StamError::SerializationError(format!("Writing key to string: {}", e)))
}

/// Writes a datakey to a JSON value
pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
serde_json::to_value(self)
.map_err(|e| StamError::SerializationError(format!("Producing Json Value: {}", e)))
}
}

/// [Handle] to an instance of [`DataKey`] in the store ([`AnnotationDataSet`])
Expand Down
12 changes: 12 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ where
}
result
}

/// Serializes this structure to a JSON value
/// If `config` not not specified, an attempt to fetch the AnnotationStore's initial config is made
fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
serde_json::to_value(&self).map_err(|e| {
StamError::SerializationError(format!(
"Writing {} to JSON value: {}",
Self::typeinfo(),
e
))
})
}
}

pub trait FromJson
Expand Down
6 changes: 6 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ impl TextResource {
StamError::SerializationError(format!("Writing resource to string: {}", e))
})
}

/// Writes a Resource to a JSON value
pub fn to_json_value(&self) -> Result<serde_json::Value, StamError> {
serde_json::to_value(self)
.map_err(|e| StamError::SerializationError(format!("Producing Json Value: {}", e)))
}
}

impl PartialOrd for TextResource {
Expand Down

0 comments on commit 7dda1f9

Please sign in to comment.