8 #include <Poco/JSON/Object.h>
9 #include <Poco/JSON/Array.h>
10 #include <Poco/JSON/JSON.h>
11 #include <Poco/JSON/Stringifier.h>
12 #include <Poco/JSON/Parser.h>
13 #include <Poco/JSON/JSONException.h>
14 #include <Poco/Dynamic/Var.h>
32 std::ostringstream ostr;
34 if (minNumberFieldsPacking<=fieldsCount)
36 const size_t count = data.length()/fieldWidth;
41 for (
size_t i=0;i<fieldsCount;++i)
43 std::string hexString;
45 hexString = data.substr(i*fieldWidth, fieldWidth);
46 size_t pos = hexString.find_first_not_of(
'0');
48 if (pos!=std::string::npos)
49 ostr << hexString.substr(pos);
60 std::ostringstream ostr;
62 for (
size_t i=0;i<data.size();++i)
63 if (data[i]==delimiter)
68 std::istringstream istr(data);
71 while(getline(istr, line, delimiter))
75 ostr << std::setfill(
'0') << std::setw(fieldWidth) << line;
82 ostr << std::setfill(
'0') << std::setw(fieldWidth) <<
"";
103 :
inherited(), resultData(resultData_), jsonType(jsonType_)
118 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
121 Poco::JSON::Array::Ptr pMatches =
new Poco::JSON::Array();
125 for (
size_t i=0;i<matchInfoCount;++i)
127 Poco::JSON::Object::Ptr pMatchElem =
new Poco::JSON::Object();
133 Poco::JSON::Array::Ptr pAttributes =
new Poco::JSON::Array();
139 Poco::JSON::Object::Ptr pAttrElem =
new Poco::JSON::Object();
141 pAttributes->add(pAttrElem);
145 pMatches->add(pMatchElem);
151 Poco::JSON::Array::Ptr pRequests =
new Poco::JSON::Array();
156 for (
size_t i=0;i<requestInfoCount;++i)
158 Poco::JSON::Object::Ptr pRequestElem =
new Poco::JSON::Object();
169 Poco::JSON::Array::Ptr pWords =
new Poco::JSON::Array();
173 for (
size_t j=0;j<wordsCount;++j)
175 Poco::JSON::Object::Ptr pWordElem =
new Poco::JSON::Object();
180 pWords->add(pWordElem);
184 pRequests->add(pRequestElem);
189 std::stringstream ostr;
190 Poco::JSON::Stringifier::stringify(obj, ostr);
194 catch(std::exception& e)
205 return serialize(jsonType, json, fieldCount, minNumberFieldsPacking);
221 Poco::JSON::Parser parser;
222 Poco::Dynamic::Var res = parser.parse(json);
223 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
224 Poco::Dynamic::Var tmp;
228 if (!pMatches.isNull())
230 const size_t matchesCount = pMatches->size();
231 for (
size_t i=0;i<matchesCount;++i)
233 Poco::JSON::Object::Ptr pMatchElem = pMatches->getObject(i);
234 if (!pMatchElem.isNull())
239 matchInfo.
setDocId(std::stoull(tmp.convert<std::string>()));
246 if (!pAttributes.isNull())
251 Poco::JSON::Object::Ptr pAttrElem = pAttributes->getObject(j);
252 if (!pAttrElem.isNull())
254 std::vector<std::string> attrNames;
255 pAttrElem->getNames(attrNames);
256 const size_t attrNamesCount = attrNames.size();
257 for (
size_t k=0;k<attrNamesCount;++k)
259 tmp = pAttrElem->get(attrNames[k]);
261 matchInfo.
addAttrInfo(attrNames[k], tmp.convert<std::string>());
266 resultData.
addMatchInfo(std::forward<SphinxMatchInfo>(matchInfo));
272 if (!pRequests.isNull())
274 const size_t requestsCount = pRequests->size();
275 for (
size_t i=0;i<requestsCount;++i)
278 Poco::JSON::Object::Ptr pRequestElem = pRequests->getObject(i);
282 requestInfo.
setNodeName(tmp.convert<std::string>());
286 requestInfo.
setQuery(tmp.convert<std::string>());
289 requestInfo.
setQueryId(convertVarToNumeric<unsigned int>(tmp, 0));
295 requestInfo.
setOrderBy(convertVarToNumeric<unsigned int>(tmp, 0));
298 requestInfo.
setTotal(convertVarToNumeric<unsigned int>(tmp, 0));
301 requestInfo.
setTotalFound(convertVarToNumeric<unsigned int>(tmp, 0));
304 requestInfo.
setTimeMsec(convertVarToNumeric<unsigned int>(tmp, 0));
307 if (!pWords.isNull())
309 const size_t wordsCount = pWords->size();
310 for (
size_t j=0;j<wordsCount;++j)
312 Poco::JSON::Object::Ptr pWordElem = pWords->getObject(j);
313 if (!pWordElem.isNull())
318 tmp.convert(wordInfo.
word);
321 wordInfo.
hits = convertVarToNumeric<unsigned int>(tmp, 0);
324 wordInfo.
docs = convertVarToNumeric<unsigned int>(tmp, 0);
326 requestInfo.
addWordInfo(std::forward<WordInfo>(wordInfo));
330 resultData.
addRequestInfo(std::forward<SphinxRequestInfo>(requestInfo));
335 catch(Poco::JSON::JSONException& e)
341 catch(std::exception& e)
354 makeJSON(matchesCount, attributesCount, maxResultNumber);
357 bool SphinxDefaultJSON::fillResultData(
SphinxResultData& resultData,
unsigned int matchesCount,
unsigned int attributesCount,
unsigned int maxResultNumber)
362 gettimeofday(&now, 0);
364 const size_t maxMatchesCount = now.tv_usec + matchesCount;
366 for (
size_t i=now.tv_usec;i<maxMatchesCount;++i)
373 matchInfo.
addAttrInfo(
"attr_"+std::to_string(j),
"value_"+std::to_string(j));
375 resultData.
addMatchInfo(std::forward<SphinxMatchInfo>(matchInfo));
378 requestInfo.setNodeName(
"node");
379 requestInfo.setQuery(
"query");
380 requestInfo.setQueryId(matchesCount);
381 requestInfo.setMaxResultsNumber(maxResultNumber);
382 requestInfo.setOrderBy(attributesCount);
383 requestInfo.setTotal(matchesCount);
384 requestInfo.setTotalFound(matchesCount);
385 requestInfo.addWordInfo(
"word1", 1 , 2);
386 requestInfo.addWordInfo(
"word2", 2 , 3);
387 requestInfo.addWordInfo(
"word3", 3 , 4);
388 resultData.
addRequestInfo(std::forward<SphinxRequestInfo>(requestInfo));
392 catch(std::exception& e)
403 if (!maxResultNumber)
418 if (fillResultData(resultData, matchesCount, attributesCount, maxResultNumber))
424 _isError = resultSerializator.
isError();
433 return os << defaultJson.
getJSON();