Skip to content

Commit

Permalink
Merge pull request meltwater#59 from AdrianDC/json_data
Browse files Browse the repository at this point in the history
examples: create json_data example for JSON responses
  • Loading branch information
Jeffail authored Aug 27, 2019
2 parents 7e3f350 + f6b18b8 commit 2eb36b8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ADD_SUBDIRECTORY (form_data)
ADD_SUBDIRECTORY (handlers)
ADD_SUBDIRECTORY (hello_world)
ADD_SUBDIRECTORY (hello_world_no_blocking)
ADD_SUBDIRECTORY (json_data)
ADD_SUBDIRECTORY (query_params)
ADD_SUBDIRECTORY (list_endpoints)
ADD_SUBDIRECTORY (request_logger_plugin)
Expand Down
36 changes: 36 additions & 0 deletions src/examples/json_data/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2014 MediaSift Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#
# Locate project sources
#
FILE (GLOB_RECURSE json_data_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)

#
# Configure common project settings
#
SET (json_data_LIBS ${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

#
# Executable build rules
#
ADD_EXECUTABLE (json_data ${json_data_SRCS})
TARGET_LINK_LIBRARIES (json_data ${json_data_LIBS})
INSTALL (TARGETS json_data DESTINATION bin)
81 changes: 81 additions & 0 deletions src/examples/json_data/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2014 MediaSift Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <served/served.hpp>

#include <boost/property_tree/json_parser.hpp>

/* json_data example
*
* This is a JSON data example of served in action.
*/
int main(int, char const**)
{
served::multiplexer mux;

mux.handle("/flat")
.get([](served::response & res, const served::request &) {
boost::property_tree::ptree root;
std::string response;
std::stringstream ss;

// Populate JSON response
root.put("bool", true);
root.put("integer", 123);
root.put("string", "String");

// Export JSON response
boost::property_tree::json_parser::write_json(ss, root, true /* human */);
res << ss.str();
});

mux.handle("/nested")
.get([](served::response & res, const served::request &) {
boost::property_tree::ptree root;
boost::property_tree::ptree arrayTree;
boost::property_tree::ptree flatTree;
std::stringstream ss;

// Populate JSON response
flatTree.put("bool", true);
flatTree.put("integer", 123);
flatTree.put("string", "String");
arrayTree.push_back(std::make_pair("", flatTree));
arrayTree.push_back(std::make_pair("", flatTree));
arrayTree.push_back(std::make_pair("", flatTree));
root.add_child("node_array", arrayTree);
root.add_child("node_1", flatTree);

// Export JSON response
boost::property_tree::json_parser::write_json(ss, root, true /* human */);
res << ss.str();
});

std::cout << "Try this example with:" << std::endl;
std::cout << " curl http://localhost:8123/flat" << std::endl;
std::cout << " curl http://localhost:8123/nested" << std::endl;

served::net::server server("127.0.0.1", "8123", mux);
server.run(10); // Run with a pool of 10 threads.

return 0;
}

0 comments on commit 2eb36b8

Please sign in to comment.