/* * Handle the plugin description response. * @param response the HTTPResponse that is associated with the request. * @param description the plugin description. * @param error an error string. */ void OladHTTPServer::HandlePluginInfo( HTTPResponse *response, string description, const OlaCallbackClient::PluginState &state, const string &error) { if (!error.empty()) { m_server.ServeError(response, error); return; } string escaped_description = description; Escape(&escaped_description); JsonObject json; json.Add("description", description); json.Add("name", state.name); json.Add("enabled", state.enabled); json.Add("active", state.active); json.Add("preferences_source", state.preferences_source); JsonArray *plugins = json.AddArray("conflicts_with"); vector<OlaPlugin>::const_iterator iter = state.conflicting_plugins.begin(); for (; iter != state.conflicting_plugins.end(); ++iter) { JsonObject *plugin = plugins->AppendObject(); plugin->Add("active", iter->IsActive()); plugin->Add("id", iter->Id()); plugin->Add("name", iter->Name()); } response->SetNoCache(); response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN); response->SendJson(json); delete response; }
/* * Handle the plugin list callback * @param response the HTTPResponse that is associated with the request. * @param plugins a list of plugins * @param error an error string. */ void OladHTTPServer::HandlePluginList(HTTPResponse *response, const vector<OlaPlugin> &plugins, const string &error) { if (!error.empty()) { m_server.ServeError(response, error); return; } JsonObject *json = new JsonObject(); // fire off the universe request now. the main server is running in a // separate thread. bool ok = m_client.FetchUniverseList( NewSingleCallback(this, &OladHTTPServer::HandleUniverseList, response, json)); if (!ok) { m_server.ServeError(response, K_BACKEND_DISCONNECTED_ERROR); delete json; return; } JsonArray *plugins_json = json->AddArray("plugins"); vector<OlaPlugin>::const_iterator iter; for (iter = plugins.begin(); iter != plugins.end(); ++iter) { JsonObject *plugin = plugins_json->AppendObject(); plugin->Add("name", iter->Name()); plugin->Add("id", iter->Id()); } }
/* * Handle the plugin list callback * @param response the HTTPResponse that is associated with the request. * @param plugins a list of plugins * @param error an error string. */ void OladHTTPServer::HandlePluginList(HTTPResponse *response, const client::Result &result, const vector<OlaPlugin> &plugins) { if (!result.Success()) { m_server.ServeError(response, result.Error()); return; } JsonObject *json = new JsonObject(); // fire off the universe request now. the main server is running in a // separate thread. m_client.FetchUniverseList( NewSingleCallback(this, &OladHTTPServer::HandleUniverseList, response, json)); JsonArray *plugins_json = json->AddArray("plugins"); vector<OlaPlugin>::const_iterator iter; for (iter = plugins.begin(); iter != plugins.end(); ++iter) { JsonObject *plugin = plugins_json->AppendObject(); plugin->Add("name", iter->Name()); plugin->Add("id", iter->Id()); } }
/** * @brief Handle the plugin description response. * @param response the HTTPResponse that is associated with the request. * @param description the plugin description * @param result the result of the API call. * @param state the state of the plugin. */ void OladHTTPServer::HandlePluginInfo(HTTPResponse *response, string description, const client::Result &result, const ola::client::PluginState &state) { if (!result.Success()) { m_server.ServeError(response, result.Error()); return; } string escaped_description = description; // Replace \n before passing in so we get \\n out the far end ReplaceAll(&escaped_description, "\n", "\\n"); JsonObject json; json.Add("description", escaped_description); json.Add("name", state.name); json.Add("enabled", state.enabled); json.Add("active", state.active); json.Add("preferences_source", state.preferences_source); JsonArray *plugins = json.AddArray("conflicts_with"); vector<OlaPlugin>::const_iterator iter = state.conflicting_plugins.begin(); for (; iter != state.conflicting_plugins.end(); ++iter) { JsonObject *plugin = plugins->AppendObject(); plugin->Add("active", iter->IsActive()); plugin->Add("id", iter->Id()); plugin->Add("name", iter->Name()); } response->SetNoCache(); response->SetContentType(HTTPServer::CONTENT_TYPE_PLAIN); response->SendJson(json); delete response; }
/* * Return the section as a string. */ string JsonSection::AsString() const { JsonObject json; json.Add("refresh", m_allow_refresh); json.Add("error", m_error); if (!m_save_button_text.empty()) json.Add("save_button", m_save_button_text); JsonArray *items = json.AddArray("items"); vector<const GenericItem*>::const_iterator iter = m_items.begin(); for (; iter != m_items.end(); ++iter) { JsonObject *item = items->AppendObject(); (*iter)->PopulateItem(item); } return JsonWriter::AsString(json); }
/* * Test a complex object. */ void JsonTest::testComplexObject() { JsonObject object; object.Add("age", 10); object.Add("name", "simon"); object.Add("male", true); JsonArray *array = object.AddArray("lucky numbers"); array->Append(2); array->Append(5); string expected = ( "{\n" " \"age\": 10,\n" " \"lucky numbers\": [2, 5],\n" " \"male\": true,\n" " \"name\": \"simon\"\n" "}"); OLA_ASSERT_EQ(expected, JsonWriter::AsString(object)); }