comparison Core/DicomFormat/DicomValue.cpp @ 2863:da12ba232119

serialization of DicomMap
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Oct 2018 17:49:05 +0200
parents 878b59270859
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2860:8b00e4cb4a6b 2863:da12ba232119
33 33
34 #include "../PrecompiledHeaders.h" 34 #include "../PrecompiledHeaders.h"
35 #include "DicomValue.h" 35 #include "DicomValue.h"
36 36
37 #include "../OrthancException.h" 37 #include "../OrthancException.h"
38 #include "../SerializationToolbox.h"
38 #include "../Toolbox.h" 39 #include "../Toolbox.h"
39 40
40 #include <boost/lexical_cast.hpp> 41 #include <boost/lexical_cast.hpp>
41 42
42 namespace Orthanc 43 namespace Orthanc
191 { 192 {
192 result.assign(content_); 193 result.assign(content_);
193 return true; 194 return true;
194 } 195 }
195 } 196 }
197
198
199 static const char* KEY_TYPE = "Type";
200 static const char* KEY_CONTENT = "Content";
201
202 void DicomValue::Serialize(Json::Value& target) const
203 {
204 target = Json::objectValue;
205
206 switch (type_)
207 {
208 case Type_Null:
209 target[KEY_TYPE] = "Null";
210 break;
211
212 case Type_String:
213 target[KEY_TYPE] = "String";
214 target[KEY_CONTENT] = content_;
215 break;
216
217 case Type_Binary:
218 {
219 target[KEY_TYPE] = "Binary";
220
221 std::string base64;
222 Toolbox::EncodeBase64(base64, content_);
223 target[KEY_CONTENT] = base64;
224 break;
225 }
226
227 default:
228 throw OrthancException(ErrorCode_InternalError);
229 }
230 }
231
232 void DicomValue::Unserialize(const Json::Value& source)
233 {
234 std::string type = SerializationToolbox::ReadString(source, KEY_TYPE);
235
236 if (type == "Null")
237 {
238 type_ = Type_Null;
239 content_.clear();
240 }
241 else if (type == "String")
242 {
243 type_ = Type_String;
244 content_ = SerializationToolbox::ReadString(source, KEY_CONTENT);
245 }
246 else if (type == "Binary")
247 {
248 type_ = Type_Binary;
249
250 const std::string base64 =SerializationToolbox::ReadString(source, KEY_CONTENT);
251 Toolbox::DecodeBase64(content_, base64);
252 }
253 else
254 {
255 throw OrthancException(ErrorCode_BadFileFormat);
256 }
257 }
196 } 258 }