Mercurial > hg > orthanc
comparison Core/RestApi/RestApiOutput.cpp @ 1046:00f9f36bcd94
on-the-fly conversion of JSON to XML according to HTTP Accept
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 18 Jul 2014 17:15:34 +0200 |
parents | 8d1845feb277 |
children | 648bf1457113 |
comparison
equal
deleted
inserted
replaced
1045:0bfeeb6d340f | 1046:00f9f36bcd94 |
---|---|
38 #include "../OrthancException.h" | 38 #include "../OrthancException.h" |
39 | 39 |
40 namespace Orthanc | 40 namespace Orthanc |
41 { | 41 { |
42 RestApiOutput::RestApiOutput(HttpOutput& output) : | 42 RestApiOutput::RestApiOutput(HttpOutput& output) : |
43 output_(output) | 43 output_(output), |
44 convertJsonToXml_(false) | |
44 { | 45 { |
45 alreadySent_ = false; | 46 alreadySent_ = false; |
46 } | 47 } |
47 | 48 |
48 RestApiOutput::~RestApiOutput() | 49 RestApiOutput::~RestApiOutput() |
69 } | 70 } |
70 | 71 |
71 void RestApiOutput::AnswerJson(const Json::Value& value) | 72 void RestApiOutput::AnswerJson(const Json::Value& value) |
72 { | 73 { |
73 CheckStatus(); | 74 CheckStatus(); |
74 Json::StyledWriter writer; | 75 |
75 std::string s = writer.write(value); | 76 if (convertJsonToXml_) |
76 output_.AnswerBufferWithContentType(s, "application/json"); | 77 { |
78 #if ORTHANC_PUGIXML_ENABLED == 1 | |
79 std::string s; | |
80 Toolbox::JsonToXml(s, value); | |
81 output_.AnswerBufferWithContentType(s, "application/xml"); | |
82 #else | |
83 LOG(ERROR) << "Orthanc was compiled without XML support"; | |
84 throw OrthancException(ErrorCode_InternalError); | |
85 #endif | |
86 } | |
87 else | |
88 { | |
89 Json::StyledWriter writer; | |
90 std::string s = writer.write(value); | |
91 output_.AnswerBufferWithContentType(s, "application/json"); | |
92 } | |
93 | |
77 alreadySent_ = true; | 94 alreadySent_ = true; |
78 } | 95 } |
79 | 96 |
80 void RestApiOutput::AnswerBuffer(const std::string& buffer, | 97 void RestApiOutput::AnswerBuffer(const std::string& buffer, |
81 const std::string& contentType) | 98 const std::string& contentType) |