hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCETaskRequestCheckState.cpp
Go to the documentation of this file.
1 #include <Poco/JSON/Object.h>
2 #include <Poco/JSON/Array.h>
3 #include <Poco/JSON/JSON.h>
4 #include <Poco/JSON/Stringifier.h>
5 #include <Poco/JSON/Parser.h>
6 #include <Poco/JSON/JSONException.h>
7 #include <Poco/Dynamic/Var.h>
8 #include <utility>
9 
10 #include "DRCEError.hpp"
11 #include "DRCEMessageConst.hpp"
12 #include "DRCEJsonMessageConst.hpp"
14 
15 namespace HCE
16 {
17 namespace drce
18 {
19 //-----------------------------------------------------------------------------
21 : inherited(DRCETaskRequest::RequestType::rtCheckTaskState), checkType(CheckType::ctUninitialized)
22 {
23  if (!json.empty())
24  unserialize(json);
25 }
26 //-----------------------------------------------------------------------------
27 bool DRCETaskRequestCheckState::serialize(std::string& json)
28 {
30  errorMsg.clear();
31  try
32  {
33  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
34 
35  obj->set(drce_json_message_const::checkType, static_cast<unsigned int>(checkType));
36 
37  std::stringstream ostr;
38  Poco::JSON::Stringifier::stringify(obj, ostr);
39  json = ostr.str();
40 
41  _isError = false;
42  }
43  catch(std::exception& e)
44  {
45  _isError = true;
46  errorMsg = e.what();
48  }
49  return !_isError;}
50 //-----------------------------------------------------------------------------
51 bool DRCETaskRequestCheckState::unserialize(const std::string& json)
52 {
53  clear();
55  errorMsg.clear();
56 
57  try
58  {
59  Poco::JSON::Parser parser;
60  Poco::Dynamic::Var res = parser.parse(json);
61  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
62 
63  Poco::Dynamic::Var tmp = obj->get(drce_json_message_const::checkType);
64  checkType = static_cast<CheckType>(convertVarToNumeric(tmp, 0));
65 
66  _isError = false;
67  }
68  catch(Poco::JSON::JSONException& e)
69  {
70  _isError = true;
71  errorMsg = e.message();
73  }
74  catch(Poco::Exception& e)
75  {
76  _isError = true;
77  errorMsg = e.message();
79  }
80  return !_isError;
81 }
82 //-----------------------------------------------------------------------------
84 {
85  checkType = CheckType::ctUninitialized;
86 }
87 //-----------------------------------------------------------------------------
88 std::istream& operator>>(std::istream& is, DRCETaskRequestCheckState& rhs)
89 {
90  std::string json;
91  is.seekg(0, std::ios::end);
92  json.resize(is.tellg());
93  is.seekg(0, std::ios::beg);
94  is.read(const_cast<char*>(json.c_str()), json.size());
95  rhs.unserialize(json);
96  return is;
97 }
98 //-----------------------------------------------------------------------------
99 std::ostream& operator<<(std::ostream& os, const DRCETaskRequestCheckState& rhs)
100 {
101  std::string json;
102  const_cast<DRCETaskRequestCheckState&>(rhs).serialize(json);
103  return os << json;
104 }
105 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
107 } // end namespace drce
108 } // end namespace HCE