dicomweb_util/
encode.rs

1use std::collections::{BTreeMap, HashMap};
2
3use dicom::core::VR::*;
4use dicom::object::InMemDicomObject;
5use serde_json::{json, Value};
6
7pub type DICOMJsonObject = BTreeMap<String, HashMap<String, Value>>;
8
9// http://dicom.nema.org/medical/dicom/current/output/chtml/part18/sect_F.2.3.html#table_F.2.3-1
10pub fn encode_dicom_to_json(dicom: InMemDicomObject) -> DICOMJsonObject {
11    dicom
12        .into_iter()
13        .map(|elt| {
14            let mut eltmap = HashMap::new();
15            eltmap.insert("vr".to_string(), json!(elt.header().vr().to_string()));
16            eltmap.insert("Value".to_string(), {
17                match elt.header().vr() {
18                    AE | AS | AT | CS | DA | DS | DT | IS | LO | LT | SH | ST | SV | TM | UC
19                    | UI | UR | UT | UV => match elt.value().multiplicity() {
20                        0 => json!([]),
21                        1 => json!([elt.value().to_clean_str().unwrap()]),
22                        _ => json!(elt.value().to_multi_str().unwrap()),
23                    },
24                    FL => {
25                        json!([elt.value().to_float32().unwrap()])
26                    }
27                    FD => {
28                        json!([elt.value().to_float64().unwrap()])
29                    }
30                    OB | OD | OF | OL | OV | OW | UN => {
31                        let bytes = elt.value().to_bytes().unwrap();
32                        json!(base64::encode(bytes))
33                    }
34                    PN => {
35                        json!([{ "Alphabetic": elt.value().to_clean_str().unwrap() }])
36                    }
37                    SL => {
38                        json!([elt.value().to_int::<i64>().unwrap()])
39                    }
40                    SQ => match elt.value() {
41                        dicom::core::DicomValue::Sequence { items, size } => {
42                            let v: Vec<DICOMJsonObject> = items
43                                .into_iter()
44                                .map(|i| encode_dicom_to_json(i.clone()))
45                                .collect();
46                            json!(v)
47                        }
48                        _ => panic!(),
49                    },
50                    SS => {
51                        json!([elt.value().to_int::<i32>().unwrap()])
52                    }
53                    UL => {
54                        json!([elt.value().to_int::<u64>().unwrap()])
55                    }
56                    US => {
57                        json!([elt.value().to_int::<u32>().unwrap()])
58                    }
59                }
60            });
61            (
62                format!(
63                    "{:04X}{:04X}",
64                    elt.header().tag.group(),
65                    elt.header().tag.element()
66                ),
67                eltmap,
68            )
69        })
70        .collect()
71}