dicom-rs icon indicating copy to clipboard operation
dicom-rs copied to clipboard

DICOM JSON Format

Open vsaase opened this issue 3 years ago • 4 comments

Hi, many thanks for your work!

Do you maybe have a working prototype for converting from DICOM binary format to JSON and back? Or any ideas on how to achieve this elegantly? Would be useful form DICOMWeb interaction.

I was just experimenting a bit with Serde:

use serde::Deserialize;
use serde_json::Value;
use enum_as_inner::EnumAsInner;

#[derive(Debug, Deserialize, EnumAsInner)]
#[serde(untagged)]
pub enum DICOMJsonTagValue {
    Str(String),
    Int(i32),
    DICOMJson(DICOMJson),
    Value(Value),
}

#[derive(Debug, Deserialize)]
pub struct DICOMJsonTag {
    pub vr: String,
    pub Value: Vec<DICOMJsonTagValue>,
}

pub type DICOMJson = Vec<HashMap<String, DICOMJsonTag>>;

Then this is the user facing version:

let parsed: DICOMJson = serde_json::from_str(jsonstring)?;
let study_instance_uid = parsed[0]["0020000D"].Value[0].as_str().unwrap();

I believe Serde has much more flexibility for a better API than that. Just wanted to ask your opinion before diving in deeper..

Victor

vsaase avatar May 13 '21 12:05 vsaase

Thank you for reaching out, @vsaase. It is true that this project is currently not covering conversions to/from JSON, but it makes sense to have that eventually, potentially as one more crate in the DICOM-rs umbrella. It only does not exist yet because the closest topics in the roadmap have been to incorporate more functionalities specific to DICOM representations and protocols, but a DICOM Web initiative should be bound to happen eventually.

With that said, I believe that the shortest path towards converting a JSON value into an in-memory DICOM object would be to convert each element into an InMemElement through a bit of matching and conversion to primitive values (a bit of a manual procedure I fear). With a sequence of these made, they can be trivially collected into an InMemoryDicomObject, via the associated function from_element_source_with_dict. Hopefully this may at least help you have an idea on how to achieve this. I'm afraid that I do not have an implementation handy.

Please feel free to ask follow-up questions on this. And if there is interest from you to contribute to the project with a DICOM JSON proof of concept, I would also be more than grateful.

Enet4 avatar May 13 '21 13:05 Enet4

Thank you for reaching out, @vsaase. It is true that this project is currently not covering conversions to/from JSON, but it makes sense to have that eventually, potentially as one more crate in the DICOM-rs umbrella. It only does not exist yet because the closest topics in the roadmap have been to incorporate more functionalities specific to DICOM representations and protocols, but a DICOM Web initiative should be bound to happen eventually.

With that said, I believe that the shortest path towards converting a JSON value into an in-memory DICOM object would be to convert each element into an InMemElement through a bit of matching and conversion to primitive values (a bit of a manual procedure I fear). With a sequence of these made, they can be trivially collected into an InMemoryDicomObject, via the associated function from_element_source_with_dict. Hopefully this may at least help you have an idea on how to achieve this. I'm afraid that I do not have an implementation handy.

Well, actually, I have something coming like this... I will try to submit another PR soon. Regards

Please feel free to ask follow-up questions on this. And if there is interest from you to contribute to the project with a DICOM JSON proof of concept, I would also be more than grateful.

charbeljc avatar Sep 01 '21 16:09 charbeljc

@Enet4 @vsaase please have a look on the last commit of #174

charbeljc avatar Sep 09 '21 12:09 charbeljc

Great! I will have a look later. I have been working on dicomweb in my spare time in the last few days

vsaase avatar Sep 09 '21 12:09 vsaase