Skip to content

Commit

Permalink
[dump] Make dump_object work for Json output
Browse files Browse the repository at this point in the history
+ add dump_json test
  • Loading branch information
Enet4 committed Feb 24, 2024
1 parent 9da65af commit 1eb4cf8
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions dump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,33 @@ impl DumpOptions {
where
D: DataDictionary,
{
match (self.color, to_stdout) {
(ColorMode::Never, _) => colored::control::set_override(false),
(ColorMode::Always, _) => colored::control::set_override(true),
(ColorMode::Auto, false) => colored::control::set_override(false),
(ColorMode::Auto, true) => colored::control::unset_override(),
}
match self.format {
DumpFormat::Text => {
match (self.color, to_stdout) {
(ColorMode::Never, _) => colored::control::set_override(false),
(ColorMode::Always, _) => colored::control::set_override(true),
(ColorMode::Auto, false) => colored::control::set_override(false),
(ColorMode::Auto, true) => colored::control::unset_override(),
}

let width = determine_width(self.width);
let width = determine_width(self.width);

let (no_text_limit, no_limit) = if to_stdout {
(self.no_text_limit, self.no_limit)
} else {
(true, true)
};
let (no_text_limit, no_limit) = if to_stdout {
(self.no_text_limit, self.no_limit)
} else {
(true, true)
};

dump(&mut to, obj, width, 0, no_text_limit, no_limit)?;
dump(&mut to, obj, width, 0, no_text_limit, no_limit)?;

Ok(())
Ok(())
}
DumpFormat::Json => {
let json_obj = DicomJson::from(obj);
serde_json::to_writer_pretty(to, &json_obj)?;
Ok(())
}
}
}
}

Expand Down Expand Up @@ -1103,4 +1112,33 @@ mod tests {
assert_eq!(value, expected.3);
}
}

#[test]
fn dump_json() {
// create object
let obj = InMemDicomObject::from_element_iter(vec![DataElement::new(
tags::SOP_INSTANCE_UID,
VR::UI,
PrimitiveValue::from("1.2.888.123"),
)]);

let mut out = Vec::new();
DumpOptions::new()
.color_mode(ColorMode::Never)
.format(crate::DumpFormat::Json)
.dump_object_to(&mut out, &obj)
.unwrap();

let json = std::str::from_utf8(&out).expect("output is not valid UTF-8");
assert_eq!(
json,
r#"{
"00080018": {
"vr": "UI",
"Value": [
"1.2.888.123"
]
}
}"#);
}
}

0 comments on commit 1eb4cf8

Please sign in to comment.