Skip to content

[json] Allow null for string values #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions json/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ where
| VR::TM
| VR::UC
| VR::UI => {
let items: Vec<String> =
let items: Vec<Option<String>> =
serde_json::from_value(value).map_err(A::Error::custom)?;
let items: Vec<String> =
items.into_iter().map(|v| v.unwrap_or_default()).collect();
values = Some(PrimitiveValue::Strs(items.into()).into());
}

Expand Down Expand Up @@ -370,7 +372,7 @@ impl<'de> Deserialize<'de> for DicomJson<Tag> {
#[cfg(test)]
mod tests {
use super::from_str;
use dicom_core::{DataElement, Tag, VR};
use dicom_core::{dicom_value, DataElement, Tag, VR};
use dicom_object::InMemDicomObject;

#[test]
Expand Down Expand Up @@ -428,4 +430,49 @@ mod tests {
Some(&DataElement::new(tag, VR::CS, "ISO_IR 192")),
)
}

#[test]
fn can_parse_null_values() {
let serialized = serde_json::json!({
"00080008": {
"Value": [
"DERIVED",
"PRIMARY",
"POST_PROCESSED",
"RT",
null,
null,
null,
null,
"100000"
],
"vr": "CS"
}
});

let obj: InMemDicomObject = super::from_value(serialized).unwrap();

let tag = Tag(0x0008, 0x0008);
assert_eq!(
obj.get(tag),
Some(&DataElement::new(
tag,
VR::CS,
dicom_value!(
Strs,
[
"DERIVED",
"PRIMARY",
"POST_PROCESSED",
"RT",
"",
"",
"",
"",
"100000",
]
)
)),
)
}
}