hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCETaskRequestGetData.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"
12 
13 namespace HCE
14 {
15 namespace drce
16 {
17 //-----------------------------------------------------------------------------
19 : inherited(DRCETaskRequest::RequestType::rtGetTaskData), fetchType(FetchType::ftUninitialized)
20 {
21  if (!json.empty())
22  unserialize(json);
23 }
24 //-----------------------------------------------------------------------------
25 bool DRCETaskRequestGetData::serialize(std::string& json)
26 {
28  errorMsg.clear();
29  try
30  {
31  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
32 
33  obj->set(drce_json_message_const::fetchType, static_cast<unsigned int>(fetchType));
34 
35  std::stringstream ostr;
36  Poco::JSON::Stringifier::stringify(obj, ostr);
37  json = ostr.str();
38 
39  _isError = false;
40  }
41  catch(std::exception& e)
42  {
43  _isError = true;
44  errorMsg = e.what();
46  }
47  return !_isError;}
48 //-----------------------------------------------------------------------------
49 bool DRCETaskRequestGetData::unserialize(const std::string& json)
50 {
51  clear();
53  errorMsg.clear();
54 
55  try
56  {
57  Poco::JSON::Parser parser;
58  Poco::Dynamic::Var res = parser.parse(json);
59  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
60 
61  Poco::Dynamic::Var tmp = obj->get(drce_json_message_const::fetchType);
62  fetchType = static_cast<FetchType>(convertVarToNumeric<unsigned int>(tmp, 0));
63 
64  _isError = false;
65  }
66  catch(Poco::JSON::JSONException& e)
67  {
68  _isError = true;
69  errorMsg = e.message();
71  }
72  catch(Poco::Exception& e)
73  {
74  _isError = true;
75  errorMsg = e.message();
77  }
78  return !_isError;
79 }
80 //-----------------------------------------------------------------------------
82 {
83  fetchType = FetchType::ftUninitialized;
84 }
85 //-----------------------------------------------------------------------------
86 std::istream& operator>>(std::istream& is, DRCETaskRequestGetData& rhs)
87 {
88  std::string json;
89  is.seekg(0, std::ios::end);
90  json.resize(is.tellg());
91  is.seekg(0, std::ios::beg);
92  is.read(const_cast<char*>(json.c_str()), json.size());
93  rhs.unserialize(json);
94  return is;
95 }
96 //-----------------------------------------------------------------------------
97 std::ostream& operator<<(std::ostream& os, const DRCETaskRequestGetData& rhs)
98 {
99  std::string json;
100  const_cast<DRCETaskRequestGetData&>(rhs).serialize(json);
101  return os << json;
102 }
103 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
105 } // end namespace drce
106 } // end namespace HCE