diff Core/DicomFormat/DicomMap.cpp @ 2863:da12ba232119

serialization of DicomMap
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Oct 2018 17:49:05 +0200
parents d88970f1ffbf
children 8265a6b56100
line wrap: on
line diff
--- a/Core/DicomFormat/DicomMap.cpp	Fri Oct 05 16:07:34 2018 +0200
+++ b/Core/DicomFormat/DicomMap.cpp	Fri Oct 05 17:49:05 2018 +0200
@@ -179,12 +179,11 @@
   }
 
 
-
-
   void DicomMap::Clear()
   {
     for (Map::iterator it = map_.begin(); it != map_.end(); ++it)
     {
+      assert(it->second != NULL);
       delete it->second;
     }
 
@@ -981,4 +980,51 @@
       return value->ParseDouble(result);
     }
   }
+
+  
+  void DicomMap::Serialize(Json::Value& target) const
+  {
+    target = Json::objectValue;
+
+    for (Map::const_iterator it = map_.begin(); it != map_.end(); ++it)
+    {
+      assert(it->second != NULL);
+      
+      std::string tag = it->first.Format();
+
+      Json::Value value;
+      it->second->Serialize(value);
+
+      target[tag] = value;
+    }
+  }
+  
+
+  void DicomMap::Unserialize(const Json::Value& source)
+  {
+    Clear();
+
+    if (source.type() != Json::objectValue)
+    {
+      throw OrthancException(ErrorCode_BadFileFormat);
+    }
+
+    Json::Value::Members tags = source.getMemberNames();
+
+    for (size_t i = 0; i < tags.size(); i++)
+    {
+      DicomTag tag(0, 0);
+      
+      if (!DicomTag::ParseHexadecimal(tag, tags[i].c_str()) ||
+          map_.find(tag) != map_.end())
+      {
+        throw OrthancException(ErrorCode_BadFileFormat);
+      }
+
+      std::auto_ptr<DicomValue> value(new DicomValue);
+      value->Unserialize(source[tags[i]]);
+
+      map_[tag] = value.release();
+    }
+  }
 }