1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! DICOM JSON serialization module

use std::io::Write;

use crate::DicomJson;
use dicom_core::{header::Header, DicomValue, PrimitiveValue, Tag, VR};
use dicom_dictionary_std::StandardDataDictionary;
use dicom_object::{mem::InMemElement, DefaultDicomObject, InMemDicomObject};
use serde::{ser::SerializeMap, Serialize, Serializer};

use self::value::{AsNumbers, AsPersonNames, AsStrings, InlineBinary};
mod value;

/// Serialize a piece of DICOM data as a string of JSON.
pub fn to_string<T>(data: T) -> Result<String, serde_json::Error>
where
    DicomJson<T>: From<T> + Serialize,
{
    serde_json::to_string(&DicomJson::from(data))
}

/// Serialize a piece of DICOM data as a pretty-printed string of JSON.
pub fn to_string_pretty<T>(data: T) -> Result<String, serde_json::Error>
where
    DicomJson<T>: From<T> + Serialize,
{
    serde_json::to_string_pretty(&DicomJson::from(data))
}

/// Serialize a piece of DICOM data as a serde JSON value.
pub fn to_value<T>(data: T) -> Result<serde_json::Value, serde_json::Error>
where
    DicomJson<T>: From<T> + Serialize,
{
    serde_json::to_value(&DicomJson::from(data))
}

/// Serialize a piece of DICOM data to a vector of bytes.
pub fn to_vec<T>(data: T) -> Result<Vec<u8>, serde_json::Error>
where
    DicomJson<T>: From<T> + Serialize,
{
    serde_json::to_vec(&DicomJson::from(data))
}

/// Serialize a piece of DICOM data to a byte writer.
pub fn to_writer<W, T>(writer: W, data: T) -> Result<(), serde_json::Error>
where
    DicomJson<T>: From<T> + Serialize,
    W: Write,
{
    serde_json::to_writer(writer, &DicomJson::from(data))
}

impl<'a, D> From<&'a DefaultDicomObject<D>> for DicomJson<&'a DefaultDicomObject<D>> {
    fn from(value: &'a DefaultDicomObject<D>) -> Self {
        Self(value)
    }
}

impl<'a, D> Serialize for DicomJson<&'a DefaultDicomObject<D>>
where
    D: 'a,
{
    /// Serializes the DICOM file as a JSON map
    /// containing one entry per data element (indexed by tag),
    /// _plus_ the data elements described by its file meta table.
    ///
    /// To exclude the file meta group data instead,
    /// dereference the value into the underlying DICOM object first
    /// (e.g. via `&*obj`).
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut ser = serializer.serialize_map(None)?;

        for e in self.0.meta().to_element_iter() {
            let tag = e.tag();
            let DicomValue::Primitive(value) = e.value() else {
                continue;
            };
            let e = InMemElement::<StandardDataDictionary>::new(e.tag(), e.vr(), value.clone());
            ser.serialize_entry(&DicomJson(tag), &DicomJson(&e))?;
        }

        let inner: &InMemDicomObject<_> = &**self.0;
        for e in inner {
            let tag = e.tag();
            ser.serialize_entry(&DicomJson(tag), &DicomJson(e))?;
        }

        ser.end()
    }
}

impl<D> From<DefaultDicomObject<D>> for DicomJson<DefaultDicomObject<D>> {
    fn from(value: DefaultDicomObject<D>) -> Self {
        Self(value)
    }
}

impl<D> Serialize for DicomJson<DefaultDicomObject<D>> {
    /// Serializes the DICOM file as a JSON map
    /// containing one entry per data element (indexed by tag),
    /// _plus_ the data elements described by its file meta table.
    ///
    /// To exclude the file meta group data instead,
    /// dereference the value into the underlying DICOM object first
    /// (e.g. via `&*obj`).
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        DicomJson(&self.0).serialize(serializer)
    }
}

impl<'a, D> From<&'a InMemDicomObject<D>> for DicomJson<&'a InMemDicomObject<D>> {
    fn from(value: &'a InMemDicomObject<D>) -> Self {
        Self(value)
    }
}

impl<'a, D> Serialize for DicomJson<&'a InMemDicomObject<D>>
where
    D: 'a,
{
    /// Serializes the DICOM object as a JSON map
    /// containing one entry per data element,
    /// indexed by tag.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_map(self.0.into_iter().map(|e| {
            let tag = e.tag();
            (DicomJson(tag), DicomJson(e))
        }))
    }
}

impl<D> From<InMemDicomObject<D>> for DicomJson<InMemDicomObject<D>> {
    fn from(value: InMemDicomObject<D>) -> Self {
        Self(value)
    }
}

impl<D> Serialize for DicomJson<InMemDicomObject<D>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        DicomJson(&self.0).serialize(serializer)
    }
}

impl<'a, D> From<&'a [InMemDicomObject<D>]> for DicomJson<&'a [InMemDicomObject<D>]> {
    fn from(value: &'a [InMemDicomObject<D>]) -> Self {
        Self(value)
    }
}

impl<'a, D> Serialize for DicomJson<&'a [InMemDicomObject<D>]> {
    /// Serializes the sequence of DICOM objects into a JSON array.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_seq(self.0.iter().map(DicomJson::from))
    }
}

impl<D> From<Vec<InMemDicomObject<D>>> for DicomJson<Vec<InMemDicomObject<D>>> {
    fn from(value: Vec<InMemDicomObject<D>>) -> Self {
        Self(value)
    }
}

impl<D> Serialize for DicomJson<Vec<InMemDicomObject<D>>> {
    /// Serializes the sequence of DICOM objects into a JSON array.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        DicomJson(self.0.as_slice()).serialize(serializer)
    }
}

impl<'a, D> From<&'a InMemElement<D>> for DicomJson<&'a InMemElement<D>> {
    fn from(value: &'a InMemElement<D>) -> Self {
        Self(value)
    }
}

impl<D> Serialize for DicomJson<&'_ InMemElement<D>> {
    /// Serializes the data element as a single JSON map.
    ///
    /// The fields present will be:
    /// - `"vr"`, containing the value representation;
    /// - Either `"Value"` (as an array of values)
    ///   or `"InlineBinary"` (binary data in base64),
    ///   if the value is not empty.
    ///
    /// The DICOM tag is not encoded,
    /// as it is typically serialized as the entry key within a data set.
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut serializer = serializer.serialize_map(None)?;
        let vr = self.0.vr();
        serializer.serialize_entry("vr", vr.to_string())?;

        match self.0.value() {
            DicomValue::Sequence(seq) => {
                serializer.serialize_entry("Value", &DicomJson(seq.items()))?;
            }
            DicomValue::PixelSequence(_seq) => {
                panic!("serialization of encapsulated pixel data is not supported")
            }
            DicomValue::Primitive(PrimitiveValue::Empty) => {
                // no-op
            }
            DicomValue::Primitive(v) => match vr {
                VR::AE
                | VR::AS
                | VR::AT
                | VR::CS
                | VR::DA
                | VR::DT
                | VR::LO
                | VR::LT
                | VR::SH
                | VR::UC
                | VR::UI
                | VR::UR
                | VR::TM
                | VR::ST
                | VR::UT => {
                    serializer.serialize_entry("Value", &AsStrings::from(v))?;
                }
                VR::PN => {
                    serializer.serialize_entry("Value", &AsPersonNames::from(v))?;
                }
                VR::FD
                | VR::IS
                | VR::FL
                | VR::DS
                | VR::SL
                | VR::SS
                | VR::SV
                | VR::UL
                | VR::US
                | VR::UV => {
                    serializer.serialize_entry("Value", &AsNumbers::from(v))?;
                }
                VR::OB | VR::OD | VR::OF | VR::OL | VR::OV | VR::OW | VR::UN => {
                    serializer.serialize_entry("InlineBinary", &InlineBinary::from(v))?;
                }
                VR::SQ => unreachable!("unexpected VR SQ in primitive value"),
            },
        }

        serializer.end()
    }
}

impl<D> From<InMemElement<D>> for DicomJson<InMemElement<D>> {
    fn from(value: InMemElement<D>) -> Self {
        Self(value)
    }
}

impl<D> Serialize for DicomJson<InMemElement<D>> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        DicomJson(&self.0).serialize(serializer)
    }
}

impl From<Tag> for DicomJson<Tag> {
    fn from(value: Tag) -> Self {
        Self(value)
    }
}

impl Serialize for DicomJson<Tag> {
    /// Serializes the DICOM tag as a single string in uppercase hexadecimal,
    /// with no separators or delimiters (`"GGGGEEEE"`).
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let Tag(g, e) = self.0;
        serializer.serialize_str(&format!("{:04X}{:04X}", g, e))
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use dicom_core::value::DataSetSequence;
    use dicom_core::{dicom_value, value::DicomDate, Tag};
    use dicom_core::{Length, VR};
    use dicom_dictionary_std::tags;
    use serde_json::json;

    use super::*;

    #[test]
    fn serialize_simple_data_elements() {
        let all_data = vec![
            InMemElement::new(
                Tag(0x0008, 0x0005),
                VR::CS,
                PrimitiveValue::from("ISO_IR 192"),
            ),
            InMemElement::new(
                Tag(0x0008, 0x0020),
                VR::DA,
                PrimitiveValue::from(DicomDate::from_ymd(2013, 4, 9).unwrap()),
            ),
            InMemElement::new(
                Tag(0x0008, 0x0061),
                VR::CS,
                dicom_value!(Strs, ["CT", "PET"]),
            ),
            InMemElement::new(
                Tag(0x0008, 0x0090),
                VR::PN,
                PrimitiveValue::from("^Bob^^Dr."),
            ),
            InMemElement::new(
                Tag(0x0009, 0x1002),
                VR::UN,
                dicom_value!(U8, [0xcf, 0x4c, 0x7d, 0x73, 0xcb, 0xfb]),
            ),
            InMemElement::new(tags::PATIENT_AGE, VR::AS, PrimitiveValue::from("30Y")),
        ];

        let obj = InMemDicomObject::from_element_iter(all_data);

        assert_eq!(
            to_value(&obj).unwrap(),
            json!({
                "00080005": {
                    "vr": "CS",
                    "Value": [ "ISO_IR 192" ]
                },
                "00080020": {
                    "vr": "DA",
                    "Value": [ "20130409" ]
                },
                "00080061": {
                    "vr": "CS",
                    "Value": [
                        "CT",
                        "PET"
                    ]
                },
                "00080090": {
                    "vr": "PN",
                    "Value": [
                      {
                        "Alphabetic": "^Bob^^Dr."
                      }
                    ]
                },
                "00091002": {
                    "vr": "UN",
                    "InlineBinary": "z0x9c8v7"
                },
                "00101010": {
                    "vr": "AS",
                    "Value": [ "30Y" ]
                }
            }),
        );
    }

    #[test]
    fn serialize_sequence_elements() {
        let obj = InMemDicomObject::from_element_iter([InMemElement::new(
            tags::SHARED_FUNCTIONAL_GROUPS_SEQUENCE,
            VR::SQ,
            DataSetSequence::new(
                vec![
                    // Item 0
                    InMemDicomObject::from_element_iter([InMemElement::new(
                        tags::CT_ACQUISITION_TYPE_SEQUENCE,
                        VR::SQ,
                        DataSetSequence::new(
                            vec![
                                // Item 0
                                InMemDicomObject::from_element_iter([
                                    InMemElement::new(
                                        tags::ACQUISITION_TYPE,
                                        VR::CS,
                                        PrimitiveValue::from("SEQUENCED"),
                                    ),
                                    InMemElement::new(
                                        tags::CONSTANT_VOLUME_FLAG,
                                        VR::CS,
                                        PrimitiveValue::from("NO"),
                                    ),
                                    InMemElement::new(
                                        tags::FLUOROSCOPY_FLAG,
                                        VR::CS,
                                        PrimitiveValue::from("NO"),
                                    ),
                                ]),
                            ],
                            Length::UNDEFINED,
                        ),
                    )]),
                    // Item 1
                    InMemDicomObject::from_element_iter([InMemElement::new(
                        tags::CT_ACQUISITION_DETAILS_SEQUENCE,
                        VR::SQ,
                        DataSetSequence::new(
                            vec![InMemDicomObject::from_element_iter([
                                InMemElement::new(
                                    tags::DATA_COLLECTION_DIAMETER,
                                    VR::DS,
                                    PrimitiveValue::from("500.08"),
                                ),
                                InMemElement::new(
                                    tags::GANTRY_DETECTOR_TILT,
                                    VR::DS,
                                    PrimitiveValue::from("0.00"),
                                ),
                                InMemElement::new(
                                    tags::TABLE_HEIGHT,
                                    VR::DS,
                                    PrimitiveValue::from("160.000"),
                                ),
                                InMemElement::new(
                                    tags::ROTATION_DIRECTION,
                                    VR::CS,
                                    PrimitiveValue::from("CW"),
                                ),
                            ])],
                            Length::UNDEFINED,
                        ),
                    )]),
                ],
                Length::UNDEFINED,
            ),
        )]);

        assert_eq!(
            to_value(obj).unwrap(),
            json!({
                // shared functional groups
                "52009229": {
                    "vr": "SQ",
                    "Value": [
                        // CT acquisition type
                        {
                            "00189301": {
                                "vr": "SQ",
                                "Value": [
                                    {
                                        "00189302": {
                                            "vr": "CS",
                                            "Value": ["SEQUENCED"]
                                        },
                                        "00189333": {
                                            "vr": "CS",
                                            "Value": ["NO"]
                                        },
                                        "00189334": {
                                            "vr": "CS",
                                            "Value": ["NO"]
                                        }
                                    }
                                ]
                            }
                        },
                        // CT acquisition details
                        {
                            "00189304": {
                                "vr": "SQ",
                                "Value": [
                                    {
                                        "00180090": {
                                            "vr": "DS",
                                            "Value": ["500.08"]
                                        },
                                        "00181120": {
                                            "vr": "DS",
                                            "Value": ["0.00"]
                                        },
                                        "00181130": {
                                            "vr": "DS",
                                            "Value": ["160.000"]
                                        },
                                        "00181140": {
                                            "vr": "CS",
                                            "Value": ["CW"]
                                        },
                                    }
                                ]
                            }
                        }
                    ]
                }
            }),
        );
    }

    #[test]
    fn write_full_file_to_json() {
        let sc_rgb_rle = dicom_test_files::path("pydicom/SC_rgb_rle.dcm").unwrap();

        let obj = dicom_object::OpenFileOptions::new()
            .read_until(Tag(0x0010, 0))
            .open_file(sc_rgb_rle)
            .expect("Failed to open test file");

        let value = serde_json::to_value(DicomJson::from(obj)).unwrap();

        assert_eq!(
            value,
            json!({
                "00020000": {
                    "vr": "UL",
                    "Value": [238]
                },
                "00020001": {
                    "vr": "OB",
                    "InlineBinary": "AAE="
                },
                "00020002": {
                    "vr": "UI",
                    "Value": ["1.2.840.10008.5.1.4.1.1.7"]
                },
                "00020003": {
                    "vr": "UI",
                    "Value": ["1.2.826.0.1.3680043.8.498.49043964482360854182530167603505525116"]
                },
                "00020010": {
                    "vr": "UI",
                    "Value": ["1.2.840.10008.1.2.5"]
                },
                "00020012": {
                    "vr": "UI",
                    "Value": ["1.2.826.0.1.3680043.2.1143.107.104.103.115.2.8.4"]
                },
                "00020013": {
                    "vr": "SH",
                    "Value": ["GDCM 2.8.4"]
                },
                "00020016": {
                    "vr": "AE",
                    "Value": ["gdcmconv"]
                },
                "00080005": {
                    "vr": "CS",
                    "Value": ["ISO_IR 192"]
                },
                "00080008": {
                    "vr": "CS",
                    "Value": ["DERIVED", "SECONDARY", "OTHER"]
                },
                "00080016": {
                    "vr": "UI",
                    "Value": ["1.2.840.10008.5.1.4.1.1.7"]
                },
                "00080018": {
                    "vr": "UI",
                    "Value": ["1.2.826.0.1.3680043.8.498.49043964482360854182530167603505525116"]
                },
                "00080020": {
                    "vr": "DA",
                    "Value": ["20170101"]
                },
                "00080023": { "vr": "DA" },
                "0008002A": { "vr": "DT" },
                "00080030": {
                    "vr": "TM",
                    "Value": ["120000"],
                },
                "00080033": { "vr": "TM" },
                "00080050": { "vr": "SH" },
                "00080060": {
                    "vr": "CS",
                    "Value": ["OT"]
                },
                "00080064": {
                    "vr": "CS",
                    "Value": ["SYN"]
                },
                "00080090": {
                    "vr": "PN",
                    "Value": [
                        {
                            "Alphabetic": "Moriarty^James"
                        }
                    ]
                }
            })
        );
    }
}