Mercurial > hg > orthanc-webviewer
comparison Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 292:aadbffcee805
remove calls to deprecated classes of JsonCpp
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Dec 2020 09:03:24 +0100 |
parents | b004474eeb66 |
children | e376158e2dbb |
comparison
equal
deleted
inserted
replaced
291:0cd081608fd3 | 292:aadbffcee805 |
---|---|
22 #include "OrthancPluginCppWrapper.h" | 22 #include "OrthancPluginCppWrapper.h" |
23 | 23 |
24 #include <boost/algorithm/string/predicate.hpp> | 24 #include <boost/algorithm/string/predicate.hpp> |
25 #include <boost/move/unique_ptr.hpp> | 25 #include <boost/move/unique_ptr.hpp> |
26 #include <boost/thread.hpp> | 26 #include <boost/thread.hpp> |
27 | |
28 | |
27 #include <json/reader.h> | 29 #include <json/reader.h> |
30 #include <json/version.h> | |
28 #include <json/writer.h> | 31 #include <json/writer.h> |
32 | |
33 #if !defined(JSONCPP_VERSION_MAJOR) || !defined(JSONCPP_VERSION_MINOR) | |
34 # error Cannot access the version of JsonCpp | |
35 #endif | |
36 | |
37 | |
38 /** | |
39 * We use deprecated "Json::Reader", "Json::StyledWriter" and | |
40 * "Json::FastWriter" if JsonCpp < 1.7.0. This choice is rather | |
41 * arbitrary, but if Json >= 1.9.0, gcc generates explicit deprecation | |
42 * warnings (clang was warning in earlier versions). For reference, | |
43 * these classes seem to have been deprecated since JsonCpp 1.4.0 (on | |
44 * February 2015) by the following changeset: | |
45 * https://github.com/open-source-parsers/jsoncpp/commit/8df98f6112890d6272734975dd6d70cf8999bb22 | |
46 **/ | |
47 #if (JSONCPP_VERSION_MAJOR >= 2 || \ | |
48 (JSONCPP_VERSION_MAJOR == 1 && JSONCPP_VERSION_MINOR >= 8)) | |
49 # define JSONCPP_USE_DEPRECATED 0 | |
50 #else | |
51 # define JSONCPP_USE_DEPRECATED 1 | |
52 #endif | |
29 | 53 |
30 | 54 |
31 #if !ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) | 55 #if !ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 2, 0) |
32 static const OrthancPluginErrorCode OrthancPluginErrorCode_NullPointer = OrthancPluginErrorCode_Plugin; | 56 static const OrthancPluginErrorCode OrthancPluginErrorCode_NullPointer = OrthancPluginErrorCode_Plugin; |
33 #endif | 57 #endif |
200 buffer_.size == 0) | 224 buffer_.size == 0) |
201 { | 225 { |
202 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 226 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
203 } | 227 } |
204 | 228 |
205 const char* tmp = reinterpret_cast<const char*>(buffer_.data); | 229 if (!ReadJson(target, buffer_.data, buffer_.size)) |
206 | |
207 Json::Reader reader; | |
208 if (!reader.parse(tmp, tmp + buffer_.size, target)) | |
209 { | 230 { |
210 LogError("Cannot convert some memory buffer to JSON"); | 231 LogError("Cannot convert some memory buffer to JSON"); |
211 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | 232 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
212 } | 233 } |
213 } | 234 } |
290 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); | 311 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); |
291 } | 312 } |
292 } | 313 } |
293 | 314 |
294 | 315 |
316 bool ReadJson(Json::Value& target, | |
317 const std::string& source) | |
318 { | |
319 return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size()); | |
320 } | |
321 | |
322 | |
323 bool ReadJson(Json::Value& target, | |
324 const void* buffer, | |
325 size_t size) | |
326 { | |
327 #if JSONCPP_USE_DEPRECATED == 1 | |
328 Json::Reader reader; | |
329 return reader.parse(reinterpret_cast<const char*>(buffer), | |
330 reinterpret_cast<const char*>(buffer) + size, target); | |
331 #else | |
332 Json::CharReaderBuilder builder; | |
333 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | |
334 assert(reader.get() != NULL); | |
335 JSONCPP_STRING err; | |
336 if (reader->parse(reinterpret_cast<const char*>(buffer), | |
337 reinterpret_cast<const char*>(buffer) + size, &target, &err)) | |
338 { | |
339 return true; | |
340 } | |
341 else | |
342 { | |
343 LogError("Cannot parse JSON: " + err); | |
344 return false; | |
345 } | |
346 #endif | |
347 } | |
348 | |
349 | |
350 void WriteFastJson(std::string& target, | |
351 const Json::Value& source) | |
352 { | |
353 #if JSONCPP_USE_DEPRECATED == 1 | |
354 Json::FastWriter writer; | |
355 target = writer.write(source); | |
356 #else | |
357 Json::StreamWriterBuilder builder; | |
358 builder.settings_["indentation"] = ""; | |
359 target = Json::writeString(builder, source); | |
360 #endif | |
361 } | |
362 | |
363 | |
364 void WriteStyledJson(std::string& target, | |
365 const Json::Value& source) | |
366 { | |
367 #if JSONCPP_USE_DEPRECATED == 1 | |
368 Json::StyledWriter writer; | |
369 target = writer.write(source); | |
370 #else | |
371 Json::StreamWriterBuilder builder; | |
372 builder.settings_["indentation"] = " "; | |
373 target = Json::writeString(builder, source); | |
374 #endif | |
375 } | |
376 | |
377 | |
295 bool MemoryBuffer::RestApiPost(const std::string& uri, | 378 bool MemoryBuffer::RestApiPost(const std::string& uri, |
296 const Json::Value& body, | 379 const Json::Value& body, |
297 bool applyPlugins) | 380 bool applyPlugins) |
298 { | 381 { |
299 Json::FastWriter writer; | 382 std::string s; |
300 return RestApiPost(uri, writer.write(body), applyPlugins); | 383 WriteFastJson(s, body); |
384 return RestApiPost(uri, s, applyPlugins); | |
301 } | 385 } |
302 | 386 |
303 | 387 |
304 bool MemoryBuffer::RestApiPut(const std::string& uri, | 388 bool MemoryBuffer::RestApiPut(const std::string& uri, |
305 const Json::Value& body, | 389 const Json::Value& body, |
306 bool applyPlugins) | 390 bool applyPlugins) |
307 { | 391 { |
308 Json::FastWriter writer; | 392 std::string s; |
309 return RestApiPut(uri, writer.write(body), applyPlugins); | 393 WriteFastJson(s, body); |
394 return RestApiPut(uri, s, applyPlugins); | |
310 } | 395 } |
311 | 396 |
312 | 397 |
313 void MemoryBuffer::CreateDicom(const Json::Value& tags, | 398 void MemoryBuffer::CreateDicom(const Json::Value& tags, |
314 OrthancPluginCreateDicomFlags flags) | 399 OrthancPluginCreateDicomFlags flags) |
315 { | 400 { |
316 Clear(); | 401 Clear(); |
317 | 402 |
318 Json::FastWriter writer; | 403 std::string s; |
319 std::string s = writer.write(tags); | 404 WriteFastJson(s, tags); |
320 | 405 |
321 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags)); | 406 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), NULL, flags)); |
322 } | 407 } |
323 | 408 |
324 void MemoryBuffer::CreateDicom(const Json::Value& tags, | 409 void MemoryBuffer::CreateDicom(const Json::Value& tags, |
325 const OrthancImage& pixelData, | 410 const OrthancImage& pixelData, |
326 OrthancPluginCreateDicomFlags flags) | 411 OrthancPluginCreateDicomFlags flags) |
327 { | 412 { |
328 Clear(); | 413 Clear(); |
329 | 414 |
330 Json::FastWriter writer; | 415 std::string s; |
331 std::string s = writer.write(tags); | 416 WriteFastJson(s, tags); |
332 | 417 |
333 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags)); | 418 Check(OrthancPluginCreateDicom(GetGlobalContext(), &buffer_, s.c_str(), pixelData.GetObject(), flags)); |
334 } | 419 } |
335 | 420 |
336 | 421 |
388 { | 473 { |
389 LogError("Cannot convert an empty memory buffer to JSON"); | 474 LogError("Cannot convert an empty memory buffer to JSON"); |
390 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 475 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
391 } | 476 } |
392 | 477 |
393 Json::Reader reader; | 478 if (!ReadJson(target, str_)) |
394 if (!reader.parse(str_, target)) | |
395 { | 479 { |
396 LogError("Cannot convert some memory buffer to JSON"); | 480 LogError("Cannot convert some memory buffer to JSON"); |
397 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | 481 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
398 } | 482 } |
399 } | 483 } |
1188 } | 1272 } |
1189 | 1273 |
1190 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ | 1274 #endif /* HAS_ORTHANC_PLUGIN_FIND_MATCHER == 1 */ |
1191 | 1275 |
1192 void AnswerJson(const Json::Value& value, | 1276 void AnswerJson(const Json::Value& value, |
1193 OrthancPluginRestOutput* output | 1277 OrthancPluginRestOutput* output) |
1194 ) | 1278 { |
1195 { | 1279 std::string bodyString; |
1196 Json::StyledWriter writer; | 1280 WriteStyledJson(bodyString, value); |
1197 std::string bodyString = writer.write(value); | |
1198 | |
1199 OrthancPluginAnswerBuffer(GetGlobalContext(), output, bodyString.c_str(), bodyString.size(), "application/json"); | 1281 OrthancPluginAnswerBuffer(GetGlobalContext(), output, bodyString.c_str(), bodyString.size(), "application/json"); |
1200 } | 1282 } |
1201 | 1283 |
1202 void AnswerString(const std::string& answer, | 1284 void AnswerString(const std::string& answer, |
1203 const char* mimeType, | 1285 const char* mimeType, |
1204 OrthancPluginRestOutput* output | 1286 OrthancPluginRestOutput* output) |
1205 ) | |
1206 { | 1287 { |
1207 OrthancPluginAnswerBuffer(GetGlobalContext(), output, answer.c_str(), answer.size(), mimeType); | 1288 OrthancPluginAnswerBuffer(GetGlobalContext(), output, answer.c_str(), answer.size(), mimeType); |
1208 } | 1289 } |
1209 | 1290 |
1210 void AnswerHttpError(uint16_t httpError, OrthancPluginRestOutput *output) | 1291 void AnswerHttpError(uint16_t httpError, OrthancPluginRestOutput *output) |
1322 bool RestApiPost(Json::Value& result, | 1403 bool RestApiPost(Json::Value& result, |
1323 const std::string& uri, | 1404 const std::string& uri, |
1324 const Json::Value& body, | 1405 const Json::Value& body, |
1325 bool applyPlugins) | 1406 bool applyPlugins) |
1326 { | 1407 { |
1327 Json::FastWriter writer; | 1408 std::string s; |
1328 return RestApiPost(result, uri, writer.write(body), applyPlugins); | 1409 WriteFastJson(s, body); |
1410 return RestApiPost(result, uri, s, applyPlugins); | |
1329 } | 1411 } |
1330 | 1412 |
1331 | 1413 |
1332 bool RestApiPut(Json::Value& result, | 1414 bool RestApiPut(Json::Value& result, |
1333 const std::string& uri, | 1415 const std::string& uri, |
1355 bool RestApiPut(Json::Value& result, | 1437 bool RestApiPut(Json::Value& result, |
1356 const std::string& uri, | 1438 const std::string& uri, |
1357 const Json::Value& body, | 1439 const Json::Value& body, |
1358 bool applyPlugins) | 1440 bool applyPlugins) |
1359 { | 1441 { |
1360 Json::FastWriter writer; | 1442 std::string s; |
1361 return RestApiPut(result, uri, writer.write(body), applyPlugins); | 1443 WriteFastJson(s, body); |
1444 return RestApiPut(result, uri, s, applyPlugins); | |
1362 } | 1445 } |
1363 | 1446 |
1364 | 1447 |
1365 bool RestApiDelete(const std::string& uri, | 1448 bool RestApiDelete(const std::string& uri, |
1366 bool applyPlugins) | 1449 bool applyPlugins) |
2018 { | 2101 { |
2019 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | 2102 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); |
2020 } | 2103 } |
2021 else | 2104 else |
2022 { | 2105 { |
2023 Json::FastWriter writer; | 2106 WriteFastJson(content_, content); |
2024 content_ = writer.write(content); | |
2025 } | 2107 } |
2026 } | 2108 } |
2027 | 2109 |
2028 | 2110 |
2029 void OrthancJob::ClearSerialized() | 2111 void OrthancJob::ClearSerialized() |
2039 { | 2121 { |
2040 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); | 2122 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_BadFileFormat); |
2041 } | 2123 } |
2042 else | 2124 else |
2043 { | 2125 { |
2044 Json::FastWriter writer; | 2126 WriteFastJson(serialized_, serialized); |
2045 serialized_ = writer.write(serialized); | |
2046 hasSerialized_ = true; | 2127 hasSerialized_ = true; |
2047 } | 2128 } |
2048 } | 2129 } |
2049 | 2130 |
2050 | 2131 |
2900 Json::Value& answerBody /* out */) | 2981 Json::Value& answerBody /* out */) |
2901 { | 2982 { |
2902 std::string body; | 2983 std::string body; |
2903 Execute(answerHeaders, body); | 2984 Execute(answerHeaders, body); |
2904 | 2985 |
2905 Json::Reader reader; | 2986 if (!ReadJson(answerBody, body)) |
2906 if (!reader.parse(body, answerBody)) | |
2907 { | 2987 { |
2908 LogError("Cannot convert HTTP answer body to JSON"); | 2988 LogError("Cannot convert HTTP answer body to JSON"); |
2909 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | 2989 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); |
2910 } | 2990 } |
2911 } | 2991 } |