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()
29 queryParams.push_back(std::make_pair(paramName, paramValue));
35 orderParams.push_back(std::make_pair(paramName, paramValue));
46 Poco::SharedPtr<SphinxFilter> pFilter(
new SphinxFilter());
47 pFilter->setFilterType(filterType);
48 pFilter->setAttributeName(attributeName);
49 pFilter->setAttributeValues(attributeValues);
50 pFilter->setExcludeValue(exclude);
57 externalFields.push_back(fieldName);
63 orderFields.push_back(fieldName);
66 std::string SphinxInputJsonMessageSearch::convertVarToString(Poco::Dynamic::Var& var)
throw (std::exception)
70 result = var.convert<std::string>();
71 else if(var.isNumeric())
76 result = std::to_string(var.convert<
long>());
78 result = std::to_string(var.convert<
unsigned long>());
82 result = std::to_string(var.convert<
double>());
94 Poco::JSON::Object::Ptr obj =
new Poco::JSON::Object();
100 Poco::JSON::Object::Ptr elem =
new Poco::JSON::Object();
101 queryParameters->add(elem);
102 for (
size_t i=0;i<queryParams.size();++i)
103 elem->set(queryParams[i].first, queryParams[i].second);
105 Poco::JSON::Array::Ptr extArray =
new Poco::JSON::Array();
107 for (
size_t i=0;i<externalFields.size();++i)
108 extArray->add(externalFields[i]);
113 Poco::JSON::Object::Ptr elemOrder =
new Poco::JSON::Object();
114 orderParameters->add(elemOrder);
115 for (
size_t i=0;i<orderParams.size();++i)
116 elemOrder->set(orderParams[i].first, orderParams[i].second);
118 Poco::JSON::Array::Ptr orderArray =
new Poco::JSON::Array();
120 for (
size_t i=0;i<orderFields.size();++i)
121 orderArray->add(orderFields[i]);
123 std::string filterString;
124 if (!filtersArray.
serialize(filterString))
128 std::stringstream ostr;
129 Poco::JSON::Stringifier::stringify(obj, ostr);
132 catch(std::exception& e)
146 filtersArray.
clear();
147 externalFields.clear();
152 Poco::JSON::Parser parser;
153 Poco::Dynamic::Var res = parser.parse(json);
154 Poco::JSON::Object::Ptr obj = res.extract<Poco::JSON::Object::Ptr>();
161 if (queryParameters.isNull())
164 for (
size_t j = 0;j <queryParameters->size();++j)
166 tmp = queryParameters->get(j);
167 if (tmp.type() ==
typeid(Poco::JSON::Object::Ptr))
169 Poco::JSON::Object::Ptr elem = tmp.extract<Poco::JSON::Object::Ptr>();
170 std::vector<std::string> vecNames;
171 elem->getNames(vecNames);
172 for (
size_t i=0;i<vecNames.size();++i)
174 tmp = elem->get(vecNames[i]);
182 if (extArray.isNull())
185 for (
size_t k=0;k<extArray->size();++k)
187 tmp = extArray->get(k);
189 externalFields.push_back(tmp.convert<std::string>());
193 queryParams.push_back(std::make_pair(vecNames[i], convertVarToString(tmp)));
199 if (!orderParameters.isNull())
201 for (
size_t j = 0;j <orderParameters->size();++j)
203 tmp = orderParameters->get(j);
204 if (tmp.type() ==
typeid(Poco::JSON::Object::Ptr))
206 Poco::JSON::Object::Ptr elem = tmp.extract<Poco::JSON::Object::Ptr>();
207 std::vector<std::string> vecNames;
208 elem->getNames(vecNames);
209 for (
size_t i=0;i<vecNames.size();++i)
211 tmp = elem->get(vecNames[i]);
219 if (extArray.isNull())
222 for (
size_t k=0;k<extArray->size();++k)
224 tmp = extArray->get(k);
226 orderFields.push_back(tmp.convert<std::string>());
230 orderParams.push_back(std::make_pair(vecNames[i], convertVarToString(tmp)));
240 std::string filterString =
decodeBase64(tmp.convert<std::string>());
246 catch(Poco::JSON::JSONException& e)
252 catch(std::exception& e)
264 is.seekg(0, std::ios::end);
265 json.resize(is.tellg());
266 is.seekg(0, std::ios::beg);
267 is.read(const_cast<char*>(json.c_str()), json.size());