hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxInputJsonMessageManage.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 
8 #include "SphinxError.hpp"
11 
12 namespace HCE
13 {
14 namespace sphinx
15 {
16 //----------------------------------------------------------------------------
19 {
20  if (!json.empty())
21  unserialize(json);
22 }
23 //-----------------------------------------------------------------------------
25 {
26  _isError = false;
27  errorMsg.clear();
28  try
29  {
30  Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
31  obj->set(input_json_message_const::commandString, commandString);
32  obj->set(input_json_message_const::commandOptionsString, commandOptionsString);
33 
34  std::stringstream ostr;
35  Poco::JSON::Stringifier::stringify(obj, ostr);
36  json = ostr.str();
37 
38  }
39  catch(std::exception& e)
40  {
41  errorMsg = e.what();
43  _isError = true;
44  }
45  return !_isError;
46 }
47 //----------------------------------------------------------------------------
48 bool SphinxInputJsonMessageManage::unserialize(const std::string& json)
49 {
50  commandString.clear();
51  commandOptionsString.clear();
52  errorMsg.clear();
53  _isError = false;
54  try
55  {
56  Poco::JSON::Parser parser;
57  Poco::Dynamic::Var res = parser.parse(json);
58  Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
59 
60  Poco::Dynamic::Var tmp = obj->get(input_json_message_const::commandString);
61  if (tmp.isString())
62  commandString = tmp.convert<std::string>();
63 
65  if (tmp.isString())
66  commandOptionsString = tmp.convert<std::string>();
67 
68  }
69  catch(Poco::JSON::JSONException& e)
70  {
71  errorMsg = e.displayText();
73  _isError = true;
74  }
75  return !_isError;
76 }
77 //----------------------------------------------------------------------------
78 std::istream& operator>>(std::istream& is, SphinxInputJsonMessageManage& messageManage)
79 {
80  std::string json;
81  std::getline(is, json);
82  messageManage.unserialize(json);
83  return is;
84 }
85 //-----------------------------------------------------------------------------
86 std::ostream& operator<<(std::ostream& os, const SphinxInputJsonMessageManage& messageManage)
87 {
88  std::string json;
89  const_cast<SphinxInputJsonMessageManage&>(messageManage).serialize(json);
90  return os << json;
91 }
92 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
94 } // end namespace sphinx
95 } // end namespace HCE