dicom-rs
dicom-rs copied to clipboard
DICOM JSON Format
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
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.
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 anInMemoryDicomObject
, via the associated functionfrom_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.
@Enet4 @vsaase please have a look on the last commit of #174
Great! I will have a look later. I have been working on dicomweb in my spare time in the last few days