12 #include <Poco/JSON/JSONException.h>
13 #include <Poco/JSON/Stringifier.h>
40 using namespace HCE::exception;
44 std::vector<Poco::Dynamic::Var> varPath;
45 Poco::Dynamic::Var ret;
48 varPath.push_back(Poco::Dynamic::Var(paramName));
49 ret = jsonObjExtractor.getAsVar(parametersPtr, varPath);
53 errStr +=
" PARAM NOT FOUND";
62 Poco::Dynamic::Var localVar;
64 localVar = extractParam(PARAM_BRGIN_MARKER_NAME, parametersPtr);
65 inDataParams.
beginMarker = localVar.extract<std::string>();
67 localVar = extractParam(PARAM_END_MARKER_NAME, parametersPtr);
68 inDataParams.
endMarker = localVar.extract<std::string>();
70 localVar = extractParam(PARAM_MAX_NUMBER_NAME, parametersPtr);
71 inDataParams.
maxNumber = localVar.extract<
int>();
73 localVar = extractParam(PARAM_DELIMITERS_NAME, parametersPtr);
74 inDataParams.
delimiters = localVar.extract<std::string>();
76 localVar = extractParam(PARAM_ACRONYMS_NAME, parametersPtr);
77 inDataParams.
acronyms = localVar.extract<std::string>();
79 localVar = extractParam(PARAM_NUMBERS_AS_WORDS_NAME, parametersPtr);
82 localVar = extractParam(PARAM_USE_SINGLE_CHARS_NAME, parametersPtr);
85 localVar = extractParam(PARAM_TIMEOUT, parametersPtr);
86 inDataParams.
timeout = localVar.extract<
int>();
88 inDataStructPtr->addParams(inDataParams);
93 Poco::JSON::Parser parser;
94 Poco::SharedPtr<InDataStruct> ret =
nullptr;
95 Poco::JSON::Object::Ptr obj =
nullptr;
96 std::string localQueryStr;
97 Poco::Dynamic::Var result;
100 result = parser.parse(jsonString);
102 catch(Poco::JSON::JSONException &excp)
106 catch(Poco::SyntaxException &excp)
111 if(result.type() ==
typeid(Poco::JSON::Object::Ptr))
115 obj = result.extract<Poco::JSON::Object::Ptr>();
117 catch(Poco::BadCastException &excp)
124 std::vector<Poco::Dynamic::Var> varPath;
125 varPath.push_back(Poco::Dynamic::Var(QUERY_NAME));
126 Poco::Dynamic::Var queryStr = jsonObjExtractor.getAsVar(obj, varPath);
127 if(queryStr.isEmpty())
129 errStr =
"EMPTY OR NOT FOUND [";
130 errStr += QUERY_NAME;
136 varPath.push_back(Poco::Dynamic::Var(CONTENTS_NAME));
137 Poco::JSON::Array::Ptr contentArrayPtr = jsonObjExtractor.getAsArray(obj, varPath);
138 if(contentArrayPtr.isNull())
140 errStr =
"EMPTY OR NOT FOUND, OR TYPE NOT ARRAY [";
141 errStr += CONTENTS_NAME;
147 varPath.push_back(Poco::Dynamic::Var(PARAMETERS_NAME));
148 Poco::JSON::Object::Ptr parametersPtr = jsonObjExtractor.getAsObject(obj, varPath);
149 if(parametersPtr.isNull())
151 errStr =
"EMPTY OR NOT FOUND, OR TYPE NOT OBJECT [";
152 errStr += PARAMETERS_NAME;
158 varPath.push_back(Poco::Dynamic::Var(RETURN_HASH_NAME));
159 Poco::Dynamic::Var returnHashVar = jsonObjExtractor.getAsVar(obj, varPath);
160 if(returnHashVar.isEmpty())
162 errStr =
"EMPTY OR NOT FOUND[";
163 errStr += RETURN_HASH_NAME;
168 ret = Poco::SharedPtr<InDataStruct>(
new InDataStruct());
169 ret->setSendCacheBack(returnHashVar.extract<
bool>());
172 parseParams(ret, parametersPtr);
174 catch(Poco::BadCastException &excp)
176 errStr =
"CAN'T CAST SOME PARAM'S TYPE, CHECK THEIR TYPES";
180 Poco::JSON::Array::Ptr arrayVal;
181 Poco::Dynamic::Var arraySubVal;
182 for(
unsigned int i = 0; i < contentArrayPtr->size(); i++)
184 arrayVal = contentArrayPtr->getArray(i);
185 if(!arrayVal.isNull())
189 arraySubVal = arrayVal->get(0);
190 const std::string &constContentStr = arraySubVal.extract<std::string>();
191 ret->addContent(const_cast<std::string &>(constContentStr));
192 arraySubVal = arrayVal->get(1);
193 const std::string &constWcacheStr = arraySubVal.extract<std::string>();
194 ret->addWcache(const_cast<std::string &>(constWcacheStr));
196 catch(Poco::BadCastException &excp)
198 errStr =
"CAN'T CAST CONTENT SUB_ARRAY ELEM [";
199 errStr += std::to_string(i);
200 errStr +=
"] TO STRING";
206 errStr =
"CAN'T CAST CONTENT ARRAY ELEM [";
207 errStr += std::to_string(i);
208 errStr +=
"] TO STRING";
214 localQueryStr = queryStr.extract<std::string>();
216 catch(Poco::BadCastException &excp)
221 if(searchStringsVector.size() == 0)
225 ret->setSearchStrings(searchStringsVector);
232 Poco::Dynamic::Var var;
233 Poco::Dynamic::Var val;
234 Poco::JSON::Array::Ptr contentArray = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
235 Poco::JSON::Array::Ptr contentSubArray =
nullptr;
236 Poco::JSON::Object::Ptr obj = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
237 val = outDataStructPtr->getErrCode();
238 obj->set(PARAM_ERROR_CODE_NAME, val);
239 val = outDataStructPtr->getErrorMsg();
240 obj->set(PARAM_ERROR_MSG_NAME, val);
241 val = outDataStructPtr->getHighlightedCounter();
242 obj->set(PARAM_HIGHLIGHTTED_NAME, val);
243 val = outDataStructPtr->getFoundCounter();
244 obj->set(PARAM_FOUND_NAME, val);
245 obj->set(PARAM_TOTAL_TIME_NAME, static_cast<unsigned int>(tDiff));
247 for(
unsigned int i = 0; i < outDataStructPtr->getContentsCount(); i++)
249 contentSubArray = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
250 val = outDataStructPtr->getContent(i);
251 contentSubArray->add(val);
252 if(i < outDataStructPtr->getWcachesCount())
254 val = outDataStructPtr->getWcache(i);
258 val = std::string(
"");
260 contentSubArray->add(val);
261 contentArray->add(contentSubArray);
262 contentSubArray =
nullptr;
265 obj->set(PARAM_CONTENTS_NAME, contentArray);
267 std::ostringstream outJSON;
268 Poco::JSON::Stringifier::stringify(var, outJSON, 1);
269 outJSONString = outJSON.str();