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>
20 queryParams(), orderParams(), filtersArray(), externalFields(), orderFields()
28 queryParams.push_back(std::make_pair(paramName, paramValue));
33 orderParams.push_back(std::make_pair(paramName, paramValue));
44 Poco::SharedPtr<SphinxFilter> pFilter(
new SphinxFilter());
45 pFilter->setFilterType(filterType);
46 pFilter->setAttributeName(attributeName);
47 pFilter->setAttributeValues(attributeValues);
48 pFilter->setExcludeValue(exclude);
54 externalFields.push_back(fieldName);
59 orderFields.push_back(fieldName);
62 std::string SphinxInputJsonMessageSearch::convertVarToString(Poco::Dynamic::Var& var)
throw (std::exception)
66 result = var.convert<std::string>();
67 else if(var.isNumeric())
72 result = std::to_string(var.convert<
long>());
74 result = std::to_string(var.convert<
unsigned long>());
78 result = std::to_string(var.convert<
double>());
90 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
96 Poco::JSON::Object::Ptr elem =
new Poco::JSON::Object();
97 queryParameters->add(elem);
98 for (
size_t i=0;i<queryParams.size();++i)
99 elem->set(queryParams[i].first, queryParams[i].second);
101 Poco::JSON::Array::Ptr extArray =
new Poco::JSON::Array();
103 for (
size_t i=0;i<externalFields.size();++i)
104 extArray->add(externalFields[i]);
109 Poco::JSON::Object::Ptr elemOrder =
new Poco::JSON::Object();
110 orderParameters->add(elemOrder);
111 for (
size_t i=0;i<orderParams.size();++i)
112 elemOrder->set(orderParams[i].first, orderParams[i].second);
114 Poco::JSON::Array::Ptr orderArray =
new Poco::JSON::Array();
116 for (
size_t i=0;i<orderFields.size();++i)
117 orderArray->add(orderFields[i]);
119 std::string filterString;
120 if (!filtersArray.
serialize(filterString))
124 std::stringstream ostr;
125 Poco::JSON::Stringifier::stringify(obj, ostr);
128 catch(std::exception& e)
142 filtersArray.
clear();
143 externalFields.clear();
148 Poco::JSON::Parser parser;
149 Poco::Dynamic::Var res = parser.parse(json);
150 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
162 if (queryParameters.isNull())
165 for (
size_t j = 0;j <queryParameters->size();++j)
167 tmp = queryParameters->get(j);
168 if (tmp.type() ==
typeid(Poco::JSON::Object::Ptr))
170 Poco::JSON::Object::Ptr elem = tmp.extract<Poco::JSON::Object::Ptr>();
171 std::vector<std::string> vecNames;
172 elem->getNames(vecNames);
173 for (
size_t i=0;i<vecNames.size();++i)
175 tmp = elem->get(vecNames[i]);
183 if (extArray.isNull())
186 for (
size_t k=0;k<extArray->size();++k)
188 tmp = extArray->get(k);
190 externalFields.push_back(tmp.convert<std::string>());
194 queryParams.push_back(std::make_pair(vecNames[i], convertVarToString(tmp)));
200 if (!orderParameters.isNull())
202 for (
size_t j = 0;j <orderParameters->size();++j)
204 tmp = orderParameters->get(j);
205 if (tmp.type() ==
typeid(Poco::JSON::Object::Ptr))
207 Poco::JSON::Object::Ptr elem = tmp.extract<Poco::JSON::Object::Ptr>();
208 std::vector<std::string> vecNames;
209 elem->getNames(vecNames);
210 for (
size_t i=0;i<vecNames.size();++i)
212 tmp = elem->get(vecNames[i]);
220 if (extArray.isNull())
223 for (
size_t k=0;k<extArray->size();++k)
225 tmp = extArray->get(k);
227 orderFields.push_back(tmp.convert<std::string>());
231 orderParams.push_back(std::make_pair(vecNames[i], convertVarToString(tmp)));
241 std::string filterString =
decodeBase64(tmp.convert<std::string>());
247 catch(Poco::JSON::JSONException& e)
253 catch(std::exception& e)
265 is.seekg(0, std::ios::end);
266 json.resize(is.tellg());
267 is.seekg(0, std::ios::beg);
268 is.read(const_cast<char*>(json.c_str()), json.size());