changeset 1690:ae09132e4237

FromJson
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 07 Oct 2015 17:42:42 +0200
parents 26083d84d237
children e447f3cb8b30
files OrthancServer/FromDcmtkBridge.cpp OrthancServer/FromDcmtkBridge.h UnitTestsSources/FromDcmtkTests.cpp
diffstat 3 files changed, 128 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/FromDcmtkBridge.cpp	Wed Oct 07 16:54:05 2015 +0200
+++ b/OrthancServer/FromDcmtkBridge.cpp	Wed Oct 07 17:42:42 2015 +0200
@@ -695,12 +695,13 @@
                             Encoding encoding);
 
 
-  static void ElementToJson(Json::Value& parent,
-                            DcmElement& element,
-                            DicomToJsonFormat format,
-                            unsigned int maxStringLength,
-                            Encoding encoding)
+  void FromDcmtkBridge::ToJson(Json::Value& parent,
+                               DcmElement& element,
+                               DicomToJsonFormat format,
+                               unsigned int maxStringLength,
+                               Encoding encoding)
   {
+    parent = Json::objectValue;
     Json::Value& target = PrepareNode(parent, element, format);
 
     if (element.isLeaf())
@@ -739,7 +740,7 @@
     for (unsigned long i = 0; i < item.card(); i++)
     {
       DcmElement* element = item.getElement(i);
-      ElementToJson(parent, *element, format, maxStringLength, encoding);
+      FromDcmtkBridge::ToJson(parent, *element, format, maxStringLength, encoding);
     }
   }
 
@@ -754,25 +755,6 @@
   }
 
 
-
-  void FromDcmtkBridge::ToJson(Json::Value& target, 
-                               const std::string& path,
-                               DicomToJsonFormat format,
-                               unsigned int maxStringLength)
-  {
-    DcmFileFormat dicom;
-    if (!dicom.loadFile(path.c_str()).good())
-    {
-      throw OrthancException(ErrorCode_BadFileFormat);
-    }
-    else
-    {
-      FromDcmtkBridge::ToJson(target, *dicom.getDataset(), format, maxStringLength);
-    }
-  }
-
-
-
   std::string FromDcmtkBridge::GetName(const DicomTag& t)
   {
     // Some patches for important tags because of different DICOM
@@ -1333,10 +1315,42 @@
   {
     std::auto_ptr<DcmElement> element;
 
-    if (value.type() == Json::stringValue)
+    switch (value.type())
     {
-      element.reset(CreateElementForTag(tag));
-      FillElementWithString(*element, tag, value.asString(), decodeBinaryTags);
+      case Json::stringValue:
+        element.reset(CreateElementForTag(tag));
+        FillElementWithString(*element, tag, value.asString(), decodeBinaryTags);
+        break;
+
+      case Json::arrayValue:
+      {
+        DcmTag key(tag.GetGroup(), tag.GetElement());
+        if (key.getEVR() != EVR_SQ)
+        {
+          throw OrthancException(ErrorCode_BadParameterType);
+        }
+
+        DcmSequenceOfItems* sequence = new DcmSequenceOfItems(key, value.size());
+        element.reset(sequence);
+        
+        for (Json::Value::ArrayIndex i = 0; i < value.size(); i++)
+        {
+          std::auto_ptr<DcmItem> item(new DcmItem);
+
+          Json::Value::Members members = value[i].getMemberNames();
+          for (Json::Value::ArrayIndex j = 0; j < members.size(); j++)
+          {
+            item->insert(FromJson(value[i][members[j]], ParseTag(members[j]), decodeBinaryTags));
+          }
+
+          sequence->append(item.release());
+        }
+
+        break;
+      }
+
+      default:
+        throw OrthancException(ErrorCode_BadParameterType);
     }
 
     return element.release();
--- a/OrthancServer/FromDcmtkBridge.h	Wed Oct 07 16:54:05 2015 +0200
+++ b/OrthancServer/FromDcmtkBridge.h	Wed Oct 07 17:42:42 2015 +0200
@@ -69,16 +69,17 @@
     static DicomValue* ConvertLeafElement(DcmElement& element,
                                           Encoding encoding);
 
+    static void ToJson(Json::Value& parent,
+                       DcmElement& element,
+                       DicomToJsonFormat format,
+                       unsigned int maxStringLength,
+                       Encoding encoding);
+
     static void ToJson(Json::Value& target, 
                        DcmDataset& dataset,
                        DicomToJsonFormat format,
                        unsigned int maxStringLength);
 
-    static void ToJson(Json::Value& target, 
-                       const std::string& path,
-                       DicomToJsonFormat format,
-                       unsigned int maxStringLength);
-
     static std::string GetName(const DicomTag& tag);
 
     static DicomTag ParseTag(const char* name);
--- a/UnitTestsSources/FromDcmtkTests.cpp	Wed Oct 07 16:54:05 2015 +0200
+++ b/UnitTestsSources/FromDcmtkTests.cpp	Wed Oct 07 17:42:42 2015 +0200
@@ -43,6 +43,8 @@
 #include "../Core/Uuid.h"
 #include "../Resources/EncodingTests.h"
 
+#include <dcmtk/dcmdata/dcelem.h>
+
 using namespace Orthanc;
 
 TEST(DicomFormat, Tag)
@@ -299,3 +301,81 @@
   ASSERT_EQ(ValueRepresentation_Other, 
             FromDcmtkBridge::GetValueRepresentation(DICOM_TAG_PATIENT_ID));
 }
+
+
+TEST(FromDcmtkBridge, FromJson)
+{
+  const DicomTag REFERENCED_STUDY_SEQUENCE(0x0008, 0x1110);
+
+  std::auto_ptr<DcmElement> element;
+
+  {
+    Json::Value a;
+    a = "Hello";
+    element.reset(FromDcmtkBridge::FromJson(a, DICOM_TAG_PATIENT_NAME, false));
+
+    Json::Value b;
+    FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Short, 0, Encoding_Ascii);
+    ASSERT_EQ("Hello", b["0010,0010"].asString());
+  }
+
+  {
+    Json::Value a;
+    a = "Hello";
+    // Cannot assign a string to a sequence
+    ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(a, REFERENCED_STUDY_SEQUENCE, false)), OrthancException);
+  }
+
+  {
+    Json::Value a = Json::arrayValue;
+    a.append("Hello");
+    // Cannot assign an array to a string
+    ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(a, DICOM_TAG_PATIENT_NAME, false)), OrthancException);
+  }
+
+  {
+    Json::Value a;
+    a = "data:application/octet-stream;base64,SGVsbG8=";  // echo -n "Hello" | base64
+    element.reset(FromDcmtkBridge::FromJson(a, DICOM_TAG_PATIENT_NAME, true));
+
+    Json::Value b;
+    FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Short, 0, Encoding_Ascii);
+    ASSERT_EQ("Hello", b["0010,0010"].asString());
+  }
+
+  printf("ici\n");
+
+  {
+    Json::Value a = Json::arrayValue;
+
+    {
+      Json::Value b = Json::objectValue;
+      b["PatientName"] = "Hello";
+      b["PatientID"] = "World";
+      a.append(b);
+    }
+
+    {
+      Json::Value b = Json::objectValue;
+      b["PatientName"] = "Hello2";
+      b["PatientID"] = "World2";
+      a.append(b);
+    }
+
+    element.reset(FromDcmtkBridge::FromJson(a, REFERENCED_STUDY_SEQUENCE, false));
+    element->writeXML(std::cout);
+
+    {
+      Json::Value b;
+      FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Full, 0, Encoding_Ascii);
+      /*ASSERT_EQ(Json::arrayValue, b["0008,1110"].type());
+        ASSERT_EQ(2, b["0008,1110"].size());*/
+
+      std::cout << b;
+    }
+  }
+
+
+
+  // TODO: Test Simplify
+}