public class JSONRepresentationOfDicomObjectFactory
extends java.lang.Object
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:
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.
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
reservedKeywordForPersonNameAlphabeticPropertyInJsonRepresentation |
protected static java.lang.String |
reservedKeywordForPersonNameIdeographicPropertyInJsonRepresentation |
protected static java.lang.String |
reservedKeywordForPersonNamePhoneticPropertyInJsonRepresentation |
Constructor and Description |
---|
JSONRepresentationOfDicomObjectFactory()
Construct a factory object, which can be used to get JSON documents from DICOM objects.
|
Modifier and Type | Method and Description |
---|---|
static void |
addPersonNameAsComponentsToJsonObject(javax.json.JsonObjectBuilder jsonPersonNameValue,
java.lang.String value,
java.lang.String alphabeticProperty,
java.lang.String ideographicProperty,
java.lang.String phoneticProperty) |
static void |
createDocumentAndWriteIt(AttributeList list,
java.io.File outputFile)
Serialize a JSON document created from a DICOM attribute list.
|
static void |
createDocumentAndWriteIt(AttributeList list,
java.io.OutputStream out)
Serialize a JSON document created from a DICOM attribute list.
|
static void |
createDocumentAndWriteIt(AttributeList list,
java.lang.String outputPath)
Serialize a JSON document created from a DICOM attribute list.
|
AttributeList |
getAttributeList(java.io.File file)
Given a DICOM instance encoded as a JSON document in a file
convert it to a list of attributes.
|
AttributeList |
getAttributeList(java.io.InputStream stream)
Given a DICOM instance encoded as a JSON document in a stream
convert it to a list of attributes.
|
AttributeList |
getAttributeList(javax.json.JsonArray document)
Given a DICOM instance encoded as a JSON document
convert it to a list of attributes.
|
AttributeList |
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.
|
AttributeList |
getAttributeList(javax.json.JsonObject document)
Given a DICOM instance encoded as a JSON document
convert it to a list of attributes.
|
AttributeList |
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.
|
AttributeList |
getAttributeList(java.lang.String name)
Given a DICOM instance encoded as a JSON document in a named file
convert it to a list of attributes.
|
protected static AttributeTag |
getAttributeTagFromHexadecimalGroupElementValues(java.lang.String s)
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(java.io.File file)
Given a DICOM instance encoded as a list of attributes, get a JSON document.
|
javax.json.JsonArray |
getDocument(java.io.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(java.lang.String filename)
Given a DICOM object encoded as a list of attributes, get a JSON document.
|
javax.json.JsonArray |
getDocument(java.lang.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 java.lang.String |
getJsonPersonNameFromPropertiesInJsonObject(javax.json.JsonObject jsonObjectValue,
java.lang.String alphabeticProperty,
java.lang.String ideographicProperty,
java.lang.String phoneticProperty) |
static void |
main(java.lang.String[] arg)
Read a DICOM dataset and write a JSON representation of it to the standard output or specified path, or vice versa.
|
protected static java.lang.String |
substituteKeywordForUIDIfPossible(java.lang.String value) |
protected static java.lang.String |
substituteKeywordForUIDIfPossibleAndAppropriateForVRAndRequested(java.lang.String value,
byte[] vr,
boolean substitute) |
protected static java.lang.String |
substituteKeywordForUIDIfPossibleAndRequested(java.lang.String value,
boolean substitute) |
protected static java.lang.String |
substituteUIDForKeywordIfPossible(java.lang.String value) |
protected static java.lang.String |
substituteUIDForKeywordIfPossibleAndAppropriateForVR(java.lang.String value,
byte[] vr) |
static void |
write(java.io.File outputFile,
javax.json.JsonArray document)
Serialize a JSON document.
|
static void |
write(java.io.OutputStream out,
javax.json.JsonArray document)
Serialize a JSON document.
|
static void |
write(java.lang.String outputPath,
javax.json.JsonArray document)
Serialize a JSON document.
|
protected static java.lang.String reservedKeywordForPersonNameAlphabeticPropertyInJsonRepresentation
protected static java.lang.String reservedKeywordForPersonNameIdeographicPropertyInJsonRepresentation
protected static java.lang.String reservedKeywordForPersonNamePhoneticPropertyInJsonRepresentation
public JSONRepresentationOfDicomObjectFactory()
Construct a factory object, which can be used to get JSON documents from DICOM objects.
public static void addPersonNameAsComponentsToJsonObject(javax.json.JsonObjectBuilder jsonPersonNameValue, java.lang.String value, java.lang.String alphabeticProperty, java.lang.String ideographicProperty, java.lang.String phoneticProperty)
jsonPersonNameValue
- value
- alphabeticProperty
- ideographicProperty
- phoneticProperty
- public static void createDocumentAndWriteIt(AttributeList list, java.io.File outputFile) throws java.io.IOException, DicomException
Serialize a JSON document created from a DICOM attribute list.
list
- the list of DICOM attributesoutputFile
- the output file to write tojava.io.IOException
DicomException
public static void createDocumentAndWriteIt(AttributeList list, java.io.OutputStream out) throws DicomException
Serialize a JSON document created from a DICOM attribute list.
list
- the list of DICOM attributesout
- the output stream to write toDicomException
public static void createDocumentAndWriteIt(AttributeList list, java.lang.String outputPath) throws java.io.IOException, DicomException
Serialize a JSON document created from a DICOM attribute list.
list
- the list of DICOM attributesoutputPath
- the output path to write tojava.io.IOException
DicomException
public AttributeList getAttributeList(java.io.File file) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a JSON document in a file convert it to a list of attributes.
file
- the input file containing the JSON documentjava.io.IOException
DicomException
public AttributeList getAttributeList(java.io.InputStream stream) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a JSON document in a stream convert it to a list of attributes.
stream
- the input stream containing the JSON documentjava.io.IOException
DicomException
public AttributeList getAttributeList(javax.json.JsonArray document) throws DicomException
Given a DICOM instance encoded as a JSON document convert it to a list of attributes.
document
- the JSON documentDicomException
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.
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)DicomException
public AttributeList getAttributeList(javax.json.JsonObject document) throws DicomException
Given a DICOM instance encoded as a JSON document convert it to a list of attributes.
document
- the JSON documentDicomException
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.
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)DicomException
public AttributeList getAttributeList(java.lang.String name) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a JSON document in a named file convert it to a list of attributes.
name
- the input file containing the JSON documentjava.io.IOException
DicomException
protected static final AttributeTag getAttributeTagFromHexadecimalGroupElementValues(java.lang.String s)
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).
public javax.json.JsonArray getDocument(AttributeList list) throws DicomException
Given a DICOM instance encoded as a list of attributes, get a JSON document.
list
- the list of DICOM attributesDicomException
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.
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 attributesDicomException
public javax.json.JsonArray getDocument(java.io.File file) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a list of attributes, get a JSON document.
file
- the DICOM filejava.io.IOException
DicomException
public javax.json.JsonArray getDocument(java.io.File file, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a list of attributes, get a JSON document.
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 attributesjava.io.IOException
DicomException
public javax.json.JsonArray getDocument(java.lang.String filename) throws java.io.IOException, DicomException
Given a DICOM object encoded as a list of attributes, get a JSON document.
filename
- the DICOM file namejava.io.IOException
DicomException
public javax.json.JsonArray getDocument(java.lang.String filename, boolean useKeywordInsteadOfTag, boolean addTag, boolean addKeyword, boolean addVR, boolean collapseValueArrays, boolean collapseEmptyToNull, boolean ignoreSR, boolean substituteUIDKeywords, boolean useNumberForIntegerOrDecimalString) throws java.io.IOException, DicomException
Given a DICOM instance encoded as a list of attributes, get a JSON document.
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 attributesjava.io.IOException
DicomException
public static java.lang.String getJsonPersonNameFromPropertiesInJsonObject(javax.json.JsonObject jsonObjectValue, java.lang.String alphabeticProperty, java.lang.String ideographicProperty, java.lang.String phoneticProperty)
public static void main(java.lang.String[] arg)
Read a DICOM dataset and write a JSON representation of it to the standard output or specified path, or vice versa.
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 pathprotected static java.lang.String substituteKeywordForUIDIfPossible(java.lang.String value)
protected static java.lang.String substituteKeywordForUIDIfPossibleAndAppropriateForVRAndRequested(java.lang.String value, byte[] vr, boolean substitute)
protected static java.lang.String substituteKeywordForUIDIfPossibleAndRequested(java.lang.String value, boolean substitute)
protected static java.lang.String substituteUIDForKeywordIfPossible(java.lang.String value)
protected static java.lang.String substituteUIDForKeywordIfPossibleAndAppropriateForVR(java.lang.String value, byte[] vr)
public static void write(java.io.File outputFile, javax.json.JsonArray document) throws java.io.IOException
Serialize a JSON document.
outputFile
- the output file to write todocument
- the JSON documentjava.io.IOException
public static void write(java.io.OutputStream out, javax.json.JsonArray document) throws java.io.IOException
Serialize a JSON document.
out
- the output stream to write todocument
- the JSON documentjava.io.IOException
public static void write(java.lang.String outputPath, javax.json.JsonArray document) throws java.io.IOException
Serialize a JSON document.
outputPath
- the output path to write todocument
- the JSON documentjava.io.IOException