Mercurial > hg > orthanc
annotate Core/SerializationToolbox.cpp @ 2695:c4ee0bedb51b jobs
some reserved global properties for plugins
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Jul 2018 14:59:54 +0200 |
parents | 47d812308d63 |
children | 218e2c864d1d |
rev | line source |
---|---|
2656 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "PrecompiledHeaders.h" | |
35 #include "SerializationToolbox.h" | |
36 | |
37 #include "OrthancException.h" | |
38 | |
39 namespace Orthanc | |
40 { | |
41 namespace SerializationToolbox | |
42 { | |
43 std::string ReadString(const Json::Value& value, | |
44 const std::string& field) | |
45 { | |
46 if (value.type() != Json::objectValue || | |
47 !value.isMember(field.c_str()) || | |
48 value[field.c_str()].type() != Json::stringValue) | |
49 { | |
50 throw OrthancException(ErrorCode_BadFileFormat); | |
51 } | |
52 else | |
53 { | |
54 return value[field.c_str()].asString(); | |
55 } | |
56 } | |
57 | |
58 | |
59 int ReadInteger(const Json::Value& value, | |
60 const std::string& field) | |
61 { | |
62 if (value.type() != Json::objectValue || | |
63 !value.isMember(field.c_str()) || | |
64 (value[field.c_str()].type() != Json::intValue && | |
65 value[field.c_str()].type() != Json::uintValue)) | |
66 { | |
67 throw OrthancException(ErrorCode_BadFileFormat); | |
68 } | |
69 else | |
70 { | |
71 return value[field.c_str()].asInt(); | |
72 } | |
73 } | |
74 | |
75 | |
76 unsigned int ReadUnsignedInteger(const Json::Value& value, | |
77 const std::string& field) | |
78 { | |
79 int tmp = ReadInteger(value, field); | |
80 | |
81 if (tmp < 0) | |
82 { | |
83 throw OrthancException(ErrorCode_BadFileFormat); | |
84 } | |
85 else | |
86 { | |
87 return static_cast<unsigned int>(tmp); | |
88 } | |
89 } | |
90 | |
91 | |
92 bool ReadBoolean(const Json::Value& value, | |
93 const std::string& field) | |
94 { | |
95 if (value.type() != Json::objectValue || | |
96 !value.isMember(field.c_str()) || | |
97 value[field.c_str()].type() != Json::booleanValue) | |
98 { | |
99 throw OrthancException(ErrorCode_BadFileFormat); | |
100 } | |
101 else | |
102 { | |
103 return value[field.c_str()].asBool(); | |
104 } | |
105 } | |
106 | |
107 | |
108 void ReadArrayOfStrings(std::vector<std::string>& target, | |
109 const Json::Value& value, | |
110 const std::string& field) | |
111 { | |
112 if (value.type() != Json::objectValue || | |
113 !value.isMember(field.c_str()) || | |
114 value[field.c_str()].type() != Json::arrayValue) | |
115 { | |
116 throw OrthancException(ErrorCode_BadFileFormat); | |
117 } | |
118 | |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
119 const Json::Value& arr = value[field.c_str()]; |
2656 | 120 |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
121 target.resize(arr.size()); |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
122 |
2656 | 123 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) |
124 { | |
125 if (arr[i].type() != Json::stringValue) | |
126 { | |
127 throw OrthancException(ErrorCode_BadFileFormat); | |
128 } | |
129 else | |
130 { | |
131 target[i] = arr[i].asString(); | |
132 } | |
133 } | |
134 } | |
135 | |
136 | |
137 void ReadListOfStrings(std::list<std::string>& target, | |
138 const Json::Value& value, | |
139 const std::string& field) | |
140 { | |
141 std::vector<std::string> tmp; | |
142 ReadArrayOfStrings(tmp, value, field); | |
143 | |
144 target.clear(); | |
145 for (size_t i = 0; i < tmp.size(); i++) | |
146 { | |
147 target.push_back(tmp[i]); | |
148 } | |
149 } | |
150 | |
151 | |
152 void ReadSetOfStrings(std::set<std::string>& target, | |
153 const Json::Value& value, | |
154 const std::string& field) | |
155 { | |
156 std::vector<std::string> tmp; | |
157 ReadArrayOfStrings(tmp, value, field); | |
158 | |
159 target.clear(); | |
160 for (size_t i = 0; i < tmp.size(); i++) | |
161 { | |
162 target.insert(tmp[i]); | |
163 } | |
164 } | |
165 | |
166 | |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
167 void ReadSetOfTags(std::set<DicomTag>& target, |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
168 const Json::Value& value, |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
169 const std::string& field) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
170 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
171 if (value.type() != Json::objectValue || |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
172 !value.isMember(field.c_str()) || |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
173 value[field.c_str()].type() != Json::arrayValue) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
174 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
175 throw OrthancException(ErrorCode_BadFileFormat); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
176 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
177 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
178 const Json::Value& arr = value[field.c_str()]; |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
179 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
180 target.clear(); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
181 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
182 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
183 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
184 DicomTag tag(0, 0); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
185 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
186 if (arr[i].type() != Json::stringValue || |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
187 !DicomTag::ParseHexadecimal(tag, arr[i].asCString())) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
188 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
189 throw OrthancException(ErrorCode_BadFileFormat); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
190 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
191 else |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
192 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
193 target.insert(tag); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
194 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
195 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
196 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
197 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
198 |
2656 | 199 void WriteArrayOfStrings(Json::Value& target, |
200 const std::vector<std::string>& values, | |
201 const std::string& field) | |
202 { | |
203 if (target.type() != Json::objectValue || | |
204 target.isMember(field.c_str())) | |
205 { | |
206 throw OrthancException(ErrorCode_BadFileFormat); | |
207 } | |
208 | |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
209 Json::Value& value = target[field]; |
2656 | 210 |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
211 value = Json::arrayValue; |
2656 | 212 for (size_t i = 0; i < values.size(); i++) |
213 { | |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
214 value.append(values[i]); |
2656 | 215 } |
216 } | |
217 | |
218 | |
219 void WriteSetOfStrings(Json::Value& target, | |
220 const std::set<std::string>& values, | |
221 const std::string& field) | |
222 { | |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
223 if (target.type() != Json::objectValue || |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
224 target.isMember(field.c_str())) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
225 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
226 throw OrthancException(ErrorCode_BadFileFormat); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
227 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
228 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
229 Json::Value& value = target[field]; |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
230 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
231 value = Json::arrayValue; |
2656 | 232 |
233 for (std::set<std::string>::const_iterator it = values.begin(); | |
234 it != values.end(); ++it) | |
235 { | |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
236 value.append(*it); |
2656 | 237 } |
2662
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
238 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
239 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
240 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
241 void WriteSetOfTags(Json::Value& target, |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
242 const std::set<DicomTag>& tags, |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
243 const std::string& field) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
244 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
245 if (target.type() != Json::objectValue || |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
246 target.isMember(field.c_str())) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
247 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
248 throw OrthancException(ErrorCode_BadFileFormat); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
249 } |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
250 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
251 Json::Value& value = target[field]; |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
252 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
253 value = Json::arrayValue; |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
254 |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
255 for (std::set<DicomTag>::const_iterator it = tags.begin(); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
256 it != tags.end(); ++it) |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
257 { |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
258 value.append(it->Format()); |
47d812308d63
serialization of DicomModification
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
259 } |
2656 | 260 } |
261 } | |
262 } |