Class JSONRepresentationOfDicomObjectFactory
A class to encode a representation of a DICOM object in a JSON form, and to convert it back again.
There are a number of characteristics of this form of output:
Note that a round trip from DICOM to JSON and back again does not always result in full fidelity, since:
- Binary floating point values will lose precision when converted to string representation and back again
- Leading and trailing white space and control characters in strings will be discarded
- Meta information header elements will be changed
- Structural elements such as group lengths will be removed and may or may not be replaced
- Physical offsets such as in the DICOMDIR will be invalidated
- Large attributes with OB and OW value representations will have their values discarded so as not to encode the bulk pixel data (probably should be added as an option)
A typical example of how to invoke this class to convert DICOM to JSON would be:
try { AttributeList list = new AttributeList(); list.read("dicomfile",null,true,true); JsonArray document = new JSONRepresentationOfDicomObjectFactory().getDocument(list); JSONRepresentationOfDicomObjectFactory.write(System.out,document); } catch (Exception e) { slf4jlogger.error("",e); }
or even simpler, if there is no further use for the JSON document:
try { AttributeList list = new AttributeList(); list.read("dicomfile",null,true,true); JSONRepresentationOfDicomObjectFactory.createDocumentAndWriteIt(list,System.out); } catch (Exception e) { slf4jlogger.error("",e); }
A typical example of converting JSON back to DICOM would be:
try { AttributeList list = new JSONRepresentationOfDicomObjectFactory().getAttributeList("jsonfile"); list.insertSuitableSpecificCharacterSetForAllStringValues(); list.write(System.out,TransferSyntax.ExplicitVRLittleEndian,true,true); } catch (Exception e) { slf4jlogger.error("",e); }
or if you need to handle the meta information properly:
try { AttributeList list = new JSONRepresentationOfDicomObjectFactory().getAttributeList("jsonfile"); list.insertSuitableSpecificCharacterSetForAllStringValues(); String sourceApplicationEntityTitle = Attribute.getSingleStringValueOrEmptyString(list,TagFromName.SourceApplicationEntityTitle); list.removeMetaInformationHeaderAttributes(); FileMetaInformation.addFileMetaInformation(list,TransferSyntax.ExplicitVRLittleEndian,sourceApplicationEntityTitle); list.write(System.out,TransferSyntax.ExplicitVRLittleEndian,true,true); } catch (Exception e) { slf4jlogger.error("",e); }
When the JSON is being converted to DICOM, the group, element and VR attributes are not needed if the element name is a keyword that can be found in the dictionary; if they are present, then their values are checked against the dictionary values.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a factory object, which can be used to get JSON documents from DICOM objects. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addPersonNameAsComponentsToJsonObject
(javax.json.JsonObjectBuilder jsonPersonNameValue, String value, String alphabeticProperty, String ideographicProperty, String phoneticProperty) static void
createDocumentAndWriteIt
(AttributeList list, File outputFile) Serialize a JSON document created from a DICOM attribute list.static void
createDocumentAndWriteIt
(AttributeList list, OutputStream out) Serialize a JSON document created from a DICOM attribute list.static void
createDocumentAndWriteIt
(AttributeList list, String outputPath) Serialize a JSON document created from a DICOM attribute list.getAttributeList
(File file) Given a DICOM instance encoded as a JSON document in a file convert it to a list of attributes.getAttributeList
(InputStream stream) Given a DICOM instance encoded as a JSON document in a stream convert it to a list of attributes.getAttributeList
(String name) Given a DICOM instance encoded as a JSON document in a named file convert it to a list of attributes.getAttributeList
(javax.json.JsonArray document) Given a DICOM instance encoded as a JSON document convert it to a list of attributes.getAttributeList
(javax.json.JsonArray document, boolean ignoreUnrecognizedTags, boolean ignoreSR) Given a DICOM instance encoded as a JsonObject in JsonArray in a JSON document convert it to a list of attributes.getAttributeList
(javax.json.JsonObject document) Given a DICOM instance encoded as a JSON document convert it to a list of attributes.getAttributeList
(javax.json.JsonObject topLevelObject, boolean ignoreUnrecognizedTags, boolean ignoreSR) Given a DICOM object encoded in a JSON document convert it to a list of attributes.protected static final AttributeTag
Parse an AttributeTag represented as an eight character hexadecimal representation as defined for the standard JSON or XML representation.javax.json.JsonArray
getDocument
(AttributeList list) Given a DICOM instance encoded as a list of attributes, get a JSON document.javax.json.JsonArray
getDocument
(AttributeList list, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) Given a DICOM instance encoded as a list of attributes, get a JSON document.javax.json.JsonArray
getDocument
(File file) Given a DICOM instance encoded as a list of attributes, get a JSON document.javax.json.JsonArray
getDocument
(File file, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) Given a DICOM instance encoded as a list of attributes, get a JSON document.javax.json.JsonArray
getDocument
(String filename) Given a DICOM object encoded as a list of attributes, get a JSON document.javax.json.JsonArray
getDocument
(String filename, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) Given a DICOM instance encoded as a list of attributes, get a JSON document.static String
getJsonPersonNameFromPropertiesInJsonObject
(javax.json.JsonObject jsonObjectValue, String alphabeticProperty, String ideographicProperty, String phoneticProperty) static void
Read a DICOM dataset and write a JSON representation of it to the standard output or specified path, or vice versa.protected static String
protected static String
substituteKeywordForUIDIfPossibleAndAppropriateForVRAndRequested
(String value, byte[] vr, boolean substitute) protected static String
substituteKeywordForUIDIfPossibleAndRequested
(String value, boolean substitute) protected static String
protected static String
substituteUIDForKeywordIfPossibleAndAppropriateForVR
(String value, byte[] vr) static void
Serialize a JSON document.static void
write
(OutputStream out, javax.json.JsonArray document) Serialize a JSON document.static void
Serialize a JSON document.
-
Field Details
-
reservedKeywordForPersonNameAlphabeticPropertyInJsonRepresentation
-
reservedKeywordForPersonNameIdeographicPropertyInJsonRepresentation
-
reservedKeywordForPersonNamePhoneticPropertyInJsonRepresentation
-
-
Constructor Details
-
JSONRepresentationOfDicomObjectFactory
public JSONRepresentationOfDicomObjectFactory()Construct a factory object, which can be used to get JSON documents from DICOM objects.
-
-
Method Details
-
substituteKeywordForUIDIfPossible
-
substituteKeywordForUIDIfPossibleAndRequested
-
substituteKeywordForUIDIfPossibleAndAppropriateForVRAndRequested
-
substituteUIDForKeywordIfPossible
-
substituteUIDForKeywordIfPossibleAndAppropriateForVR
-
getAttributeTagFromHexadecimalGroupElementValues
Parse an AttributeTag represented as an eight character hexadecimal representation as defined for the standard JSON or XML representation.
Hex digits may be upper or lower case (though tthe standard requires upper only).
- Returns:
- AttributeTag or null if not in expected format
-
getJsonPersonNameFromPropertiesInJsonObject
-
addPersonNameAsComponentsToJsonObject
public static void addPersonNameAsComponentsToJsonObject(javax.json.JsonObjectBuilder jsonPersonNameValue, String value, String alphabeticProperty, String ideographicProperty, String phoneticProperty) - Parameters:
jsonPersonNameValue
-value
-alphabeticProperty
-ideographicProperty
-phoneticProperty
-
-
getDocument
public javax.json.JsonArray getDocument(AttributeList list, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) throws DicomException Given a DICOM instance encoded as a list of attributes, get a JSON document.
- Parameters:
list
- the list of DICOM attributesuseKeywordInsteadOfTag
- use the keyword from the DicomDictionary rather than the hexadecimal tag group and elementaddTag
- add the hexadecimal group and element as an additional attributeaddKeyword
- add the DicomDictionary keyword as an additional attributeaddVR
- add the value representation as an additional attributecollapseValueArrays
- whether or not to elide value object and array when a single value and no other objectscollapseEmptyToNull
- whether or not to elide empty object as value and send null instead when zero length attributeignoreSR
- whether or not to ignore SR Content Items (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)substituteUIDKeywords
- whether or not to substitute keywords for recognized standard SOP ClassesuseNumberForIntegerOrDecimalString
- whether or not to use JSON Number instead of JSON String for IS and DS attributes- Returns:
- the JSON document
- Throws:
DicomException
-
getDocument
Given a DICOM instance encoded as a list of attributes, get a JSON document.
- Parameters:
list
- the list of DICOM attributes- Returns:
- the JSON document
- Throws:
DicomException
-
getDocument
public javax.json.JsonArray getDocument(File file, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) throws IOException, DicomException Given a DICOM instance encoded as a list of attributes, get a JSON document.
- Parameters:
file
- the DICOM fileuseKeywordInsteadOfTag
- use the keyword from the DicomDictionary rather than the hexadecimal tag group and elementaddTag
- add the hexadecimal group and element as an additional attributeaddKeyword
- add the DicomDictionary keyword as an additional attributeaddVR
- add the value representation as an additional attributecollapseValueArrays
- whether or not to elide value object and array when a single value and no other objectscollapseEmptyToNull
- whether or not to elide empty object as value and send null instead when zero length attributeignoreSR
- whether or not to ignore SR Content Items (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)substituteUIDKeywords
- whether or not to substitute keywords for recognized standard SOP ClassesuseNumberForIntegerOrDecimalString
- whether or not to use JSON Number instead of JSON String for IS and DS attributes- Returns:
- the JSON document
- Throws:
IOException
DicomException
-
getDocument
Given a DICOM instance encoded as a list of attributes, get a JSON document.
- Parameters:
file
- the DICOM file- Returns:
- the JSON document
- Throws:
IOException
DicomException
-
getDocument
public javax.json.JsonArray getDocument(String filename, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) throws IOException, DicomException Given a DICOM instance encoded as a list of attributes, get a JSON document.
- Parameters:
filename
- the DICOM file nameuseKeywordInsteadOfTag
- use the keyword from the DicomDictionary rather than the hexadecimal tag group and elementaddTag
- add the hexadecimal group and element as an additional attributeaddKeyword
- add the DicomDictionary keyword as an additional attributeaddVR
- add the value representation as an additional attributecollapseValueArrays
- whether or not to elide value object and array when a single value and no other objectscollapseEmptyToNull
- whether or not to elide empty object as value and send null instead when zero length attributeignoreSR
- whether or not to ignore SR Content Items (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)substituteUIDKeywords
- whether or not to substitute keywords for recognized standard SOP ClassesuseNumberForIntegerOrDecimalString
- whether or not to use JSON Number instead of JSON String for IS and DS attributes- Returns:
- the JSON document
- Throws:
IOException
DicomException
-
getDocument
Given a DICOM object encoded as a list of attributes, get a JSON document.
- Parameters:
filename
- the DICOM file name- Returns:
- the JSON document
- Throws:
IOException
DicomException
-
getAttributeList
public AttributeList getAttributeList(javax.json.JsonObject topLevelObject, boolean ignoreUnrecognizedTags, boolean ignoreSR) throws DicomException Given a DICOM object encoded in a JSON document convert it to a list of attributes.
- Parameters:
topLevelObject
- the first object of the array that is the JSON documentignoreUnrecognizedTags
- whether or not to ignore unrecognized tags (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)ignoreSR
- whether or not to ignore SR Content Items (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)- Returns:
- the list of DICOM attributes
- Throws:
DicomException
-
getAttributeList
public AttributeList getAttributeList(javax.json.JsonArray document, boolean ignoreUnrecognizedTags, boolean ignoreSR) throws DicomException Given a DICOM instance encoded as a JsonObject in JsonArray in a JSON document convert it to a list of attributes.
- Parameters:
document
- the JSON documentignoreUnrecognizedTags
- whether or not to ignore unrecognized tags (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)ignoreSR
- whether or not to ignore SR Content Items (e.g., when used with JSONRepresentationOfStructuredReportObjectFactory)- Returns:
- the list of DICOM attributes
- Throws:
DicomException
-
getAttributeList
Given a DICOM instance encoded as a JSON document convert it to a list of attributes.
- Parameters:
document
- the JSON document- Returns:
- the list of DICOM attributes
- Throws:
DicomException
-
getAttributeList
Given a DICOM instance encoded as a JSON document convert it to a list of attributes.
- Parameters:
document
- the JSON document- Returns:
- the list of DICOM attributes
- Throws:
DicomException
-
getAttributeList
Given a DICOM instance encoded as a JSON document in a stream convert it to a list of attributes.
- Parameters:
stream
- the input stream containing the JSON document- Returns:
- the list of DICOM attributes
- Throws:
IOException
DicomException
-
getAttributeList
Given a DICOM instance encoded as a JSON document in a file convert it to a list of attributes.
- Parameters:
file
- the input file containing the JSON document- Returns:
- the list of DICOM attributes
- Throws:
IOException
DicomException
-
getAttributeList
Given a DICOM instance encoded as a JSON document in a named file convert it to a list of attributes.
- Parameters:
name
- the input file containing the JSON document- Returns:
- the list of DICOM attributes
- Throws:
IOException
DicomException
-
write
Serialize a JSON document.
- Parameters:
out
- the output stream to write todocument
- the JSON document- Throws:
IOException
-
write
Serialize a JSON document.
- Parameters:
outputFile
- the output file to write todocument
- the JSON document- Throws:
IOException
-
write
Serialize a JSON document.
- Parameters:
outputPath
- the output path to write todocument
- the JSON document- Throws:
IOException
-
createDocumentAndWriteIt
public static void createDocumentAndWriteIt(AttributeList list, OutputStream out) throws DicomException Serialize a JSON document created from a DICOM attribute list.
- Parameters:
list
- the list of DICOM attributesout
- the output stream to write to- Throws:
DicomException
-
createDocumentAndWriteIt
public static void createDocumentAndWriteIt(AttributeList list, File outputFile) throws IOException, DicomException Serialize a JSON document created from a DICOM attribute list.
- Parameters:
list
- the list of DICOM attributesoutputFile
- the output file to write to- Throws:
IOException
DicomException
-
createDocumentAndWriteIt
public static void createDocumentAndWriteIt(AttributeList list, String outputPath) throws IOException, DicomException Serialize a JSON document created from a DICOM attribute list.
- Parameters:
list
- the list of DICOM attributesoutputPath
- the output path to write to- Throws:
IOException
DicomException
-
main
Read a DICOM dataset and write a JSON representation of it to the standard output or specified path, or vice versa.
- Parameters:
arg
- either one input path of the file containing the DICOM/JSON dataset, or a direction argument (toDICOM or toJSON, case insensitive, defaults to toJSON) and an input path, and optionally an output path
-