use crate::{error::StixError as Error, types::get_object_type};
use serde_json::Value;
pub fn get_object_type_from_json(json_str: &str) -> Result<String, Error> {
let json_value: Value =
serde_json::from_str(json_str).map_err(|e| Error::DeserializationError(e.to_string()))?;
let (Some(sub_type), true) = (json_value.get("type"), json_value.get("id").is_some()) else {
return Err(Error::UnexpectedJsonFormat);
};
Ok(get_object_type(sub_type.to_string().trim_matches('\"')))
}
#[cfg(test)]
mod tests {
use crate::json::*;
#[test]
fn get_sdo_from_json() {
let json_str = r#"{
"type": "identity",
"name": "Identity",
"identity_class": "individual",
"description": "Responsible for managing personal digital identity",
"roles": ["User", "Administrator"],
"sectors": ["Technology","Aerospace"],
"spec_version": "2.1",
"contact_information": "alex.johnson@example.com",
"id": "identity--cc7fa653-c35f-43db-afdd-dce4c3a241d5",
"created": "2016-05-12T08:17:27Z",
"modified": "2016-05-12T08:17:27Z",
"external_references": [
{
"source_name": "capec",
"external_id": "CAPEC-163"
}
]
}"#;
let result = get_object_type_from_json(json_str).unwrap();
assert_eq!(&result, "sdo");
}
}