bool RESTHandler::build_message(Poco::Net::HTTPServerRequest &request, Poco::Net::HTMLForm &form, Poco::URI &url, zmqpp::message &msg) { Json::Value root; bool ok = false; /// Find 'args' param in query or as POST body if (form.has("args")) { ok = reader.parse(form.get("args"), root); } else if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST) { ok = reader.parse(request.stream(), root); } if (!ok || !root.isArray()) { return false; } if (verbose) { std::clog << "0\t" << url.getPath().substr(1) << std::endl; } /// Get service name as path without leading slash msg << url.getPath().substr(1); for (size_t i = 0; i < root.size(); ++i) { auto val = root.get(i, ""); if (!verbose) msg << (val.isString() ? root.get(i, "").asString() : val.toStyledString()); else { std::string s = (val.isString() ? root.get(i, "").asString() : val.toStyledString()); msg << s; std::clog << (i + 1) << '\t' << s << std::endl; } } return true; }
void Manifest::awaken(Origin origin) const { if (!m_remote.at(origin)) return; const std::size_t chunk(origin / m_chunkSize * m_chunkSize); const auto m(m_endpoint.getSubEndpoint("m")); const auto bytes(io::ensureGet(m, std::to_string(chunk))); const auto json(parse(bytes->data())); std::lock_guard<std::mutex> lock(m_mutex); if (!json.isArray()) { throw std::runtime_error( "Invalid file-info chunk - expected array: " + json.toStyledString()); } else if (json.size() != std::min(m_chunkSize, size() - chunk)) { throw std::runtime_error("Invalid file-info chunk - unexpected size"); } std::size_t i(chunk); for (const auto& f : json) { m_fileInfo.at(i) = FileInfo(f); m_remote.at(i) = false; ++i; } }
int main () { const auto h = hello{ '?', "hello world", 42, {21, 12.12, {1, 2, 3, 4, 5}, {{42, 84}}}, 84.42, false, {{1, 2, 3}, {3,2,1}}, "hello world", {{42.2, 51.7, 0.987, 12.97}}, {{"pauline", 25}, {"jeremy", 25}, {"alexie", 22}}, {{"pauline", 25}, {"jeremy", 25}, {"alexie", 22}}, {'a', 'b', 'c', 'd'}, }; const auto i = inner{21, 12.12, {1, 2, 3, 4, 5}, {{42, 84}}}; const auto _s = small{42}; const auto _s_equal = small{42}; const auto _s_not_equal = small{43}; std::cout << "s == s_equal " << std::boolalpha << sad::equals(_s, _s_equal) << std::endl; std::cout << "s != s_not_equal " << std::boolalpha << sad::equals(_s, _s_not_equal) << std::endl; // fun(h); auto s = sad::schema<hello>()(h); std::cout << sad::schema<hello>().type_name() << std::endl; s.get<int>("i") = 250; // std::cout << "hello.i:" << s.get<int>("i") << std::endl; auto mn = std::move(sad::maybe_null<int>{}); sad::maybe_null<int> mi = 42; std::cout << mi << std::endl; sad::serialize(sad::backend::cout_serializer, h); std::cout << std::endl; auto json_value = sad::serialize(sad::backend::jsoncpp_serializer{}, i); std::cout << json_value.toStyledString() << std::endl; auto d_result = sad::deserialize<numbers>(sad::backend::jsoncpp_deserializer{}, "{\"i\": 42, \"f\": 84.48, \"b\": true, \"veci\":[23,89,24]}"); if (not d_result) { std::cout << d_result.error_msg << std::endl; } std::cout << "deserialized numbers: " << sad::schema<numbers>()(*d_result) << std::endl; // ty<decltype(s)> t; }
zmqpp::message reader::Json::read() { zmqpp::message msg; msg << target; ::Json::Value value; ::Json::Reader reader; if (!reader.parse(Plain::read_input_str(), value)) { throw std::runtime_error(reader.getFormatedErrorMessages()); } if (!value.isArray()) { throw std::runtime_error("Must be array"); } for (size_t i = 0; i < value.size(); ++i) { auto val = value.get(i, ""); std::string param = (val.isString() ? val.asString() : val.toStyledString()); if (!val.isString()) { if (verbose) { std::cerr << "Item #" << i << " is not string!" << std::endl; } param = trim_newline(param); } msg << param; } return msg; }