void print_json(const json::value & jvalue) { if (!jvalue.is_null()) { cout << jvalue << endl; } }
void RemoteExecutor::ReportTaskCompletion( int jobId, int taskId, int taskRequeueCount, json::value jsonBody, const std::string& callbackUri) { try { if (!jsonBody.is_null()) { Logger::Debug(jobId, taskId, taskRequeueCount, "Callback to {0} with {1}", callbackUri, jsonBody); client::http_client client = HttpHelper::GetHttpClient(callbackUri); http_request request = HttpHelper::GetHttpRequest(methods::POST, jsonBody); http_response response = client.request(request).get(); Logger::Info(jobId, taskId, taskRequeueCount, "Callback to {0} response code {1}", callbackUri, response.status_code()); } } catch (const std::exception& ex) { this->jobTaskTable.RequestResync(); Logger::Error(jobId, taskId, taskRequeueCount, "Exception when sending back task result. {0}", ex.what()); } }
static void __print_scalar (string & result, const json::value & v) { if (v.is_null()) { result.append("null"); } else if (v.is_string()) { string r; result.append(1, '"'); result.append(v.get<string>()); result.append(1, '"'); } else { result.append(v.get<string>()); } }
void GoogleSearchClient::flattenJsonInnerLoop(json::value const & val, FlatJsonObject & json_map) { // input : Body of the google search response as json::value // This recursive function parse the Json raw value to a C++ multimap if (!val.is_null() && val.is_object()) { for (auto const &iter : val.as_object()) { //iter on each element for Json object type. Return a vect<string_t,json::value> const auto &mapKey = iter.first; const auto &content = iter.second; json_map.insert(std::pair<utility::string_t, utility::string_t>(mapKey, content.serialize())); flattenJsonInnerLoop(content, json_map); } } else if (val.is_array()) { for (auto const &content : val.as_array()) { //iter on each element for Json array type. Return a json::value flattenJsonInnerLoop(content, json_map); } } }
http::http_request http_client_impl::_build_request(http::method method, http::uri_builder request_uri, const ::utility::string_t& accept, json::value object) const { http::http_request msg(method); if (!accept.empty()) msg.headers().add(U("Accept"), accept); msg.set_request_uri(request_uri.to_uri()); if (method == http::methods::POST || method == http::methods::PUT || method == http::methods::PATCH) { if(object.is_null() || !object.is_object()) throw std::invalid_argument("POST, PUT, and PATCH requests require a payload"); msg.set_body(object); } return msg; }
void print_search_results(json::value const & value) { if (!value.is_null()) { //auto response = value.at(L"data"); //auto results = response[L"movies"]; // iteration is supported not directly on json::value but on // json::object and json::array. Use json::value::as_object() and json::value::as_array(). std::wcout << value.as_array().at(0) << std::endl; for (auto const & o : value.as_array()) { auto id = o.at(L"id"); auto name = o.at(L"name"); auto username = o.at(L"username"); auto email = o.at(L"email"); auto address = o.at(L"address"); auto street = address.at(L"street"); std::wcout << id << std::endl << name << std::endl << username << std::endl << email << std::endl << street << std::endl << std::endl; } } }
void Leanplum::ParserResponse(json::value response) { if (response.is_null()){ return; } CString key; VARIABLE_TYPE type; typedef std::map<CString, std::pair<VARIABLE_TYPE, CString>>::iterator it_type; for(it_type iterator = m_variablesMap.begin(); iterator != m_variablesMap.end(); iterator++) { key = iterator->first; type = iterator->second.first; if (VARIABLE_TYPE::INT == type) { int v = response.at(key.GetBuffer()).as_integer(); iterator->second.second.Format(_T("%d"), v); } else if (VARIABLE_TYPE::BOOL == type) { bool v = response.at(key.GetBuffer()).as_bool(); iterator->second.second = v? _T("true"):_T("false"); } else if (VARIABLE_TYPE::STRING == type) { iterator->second.second = response.at(key.GetBuffer()).as_string().c_str(); } } }
// ---------------------------------------------------------------------------- #define CATCH_CONFIG_MAIN #include "catch.hpp" // ---------------------------------------------------------------------------- #include <json/json.h> // ---------------------------------------------------------------------------- TEST_CASE( "default-constructed-value-is-null", "[value]" ) { json::value v; REQUIRE( v.is_null() == true ); } // ---------------------------------------------------------------------------- TEST_CASE( "construct-number", "[value]" ) { json::value v1{1.234}; json::value v2 = 10; REQUIRE( v1.as_number() == 1.234 ); REQUIRE( v2.as_number() == 10 ); } // ---------------------------------------------------------------------------- TEST_CASE( "construct-object-by-initializer-list", "[object]" ) { json::object v{ { "x", "y" }, { "t", "u" } }; REQUIRE( v["x"].is_string() == true ); REQUIRE( v["x"].as_string() == "y" );