Refine timeout control; refine rest book examples.
parent
2e2b45dd43
commit
e9096d4e53
@ -0,0 +1,52 @@
|
||||
#include "example/common/book_json.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "example/common/book.h"
|
||||
|
||||
std::string JsonToString(const Json::Value& json) {
|
||||
Json::StreamWriterBuilder builder;
|
||||
return Json::writeString(builder, json);
|
||||
}
|
||||
|
||||
Json::Value StringToJson(const std::string& str) {
|
||||
Json::Value json;
|
||||
|
||||
Json::CharReaderBuilder builder;
|
||||
std::stringstream stream(str);
|
||||
std::string errs;
|
||||
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
|
||||
std::cerr << errs << std::endl;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
Json::Value BookToJson(const Book& book) {
|
||||
Json::Value root;
|
||||
root["id"] = book.id;
|
||||
root["title"] = book.title;
|
||||
root["price"] = book.price;
|
||||
return root;
|
||||
}
|
||||
|
||||
std::string BookToJsonString(const Book& book) {
|
||||
return JsonToString(BookToJson(book));
|
||||
}
|
||||
|
||||
bool JsonStringToBook(const std::string& json_str, Book* book) {
|
||||
Json::Value json = StringToJson(json_str);
|
||||
|
||||
if (!json) {
|
||||
return false;
|
||||
}
|
||||
|
||||
book->id = json["id"].asString();
|
||||
book->title = json["title"].asString();
|
||||
book->price = json["price"].asDouble();
|
||||
|
||||
return true;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#ifndef EXAMPLE_COMMON_BOOK_JSON_H_
|
||||
#define EXAMPLE_COMMON_BOOK_JSON_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "json/json-forwards.h"
|
||||
|
||||
struct Book;
|
||||
|
||||
std::string JsonToString(const Json::Value& json);
|
||||
|
||||
Json::Value StringToJson(const std::string& str);
|
||||
|
||||
Json::Value BookToJson(const Book& book);
|
||||
|
||||
std::string BookToJsonString(const Book& book);
|
||||
bool JsonStringToBook(const std::string& json_str, Book* book);
|
||||
|
||||
#endif // EXAMPLE_COMMON_BOOK_JSON_H_
|
@ -1,4 +1,13 @@
|
||||
add_executable(rest_book_async_client main.cc)
|
||||
set(TARGET_NAME rest_book_async_client)
|
||||
|
||||
target_link_libraries(rest_book_async_client webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_book_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
set(SRCS
|
||||
../common/book.cc
|
||||
../common/book.h
|
||||
../common/book_json.cc
|
||||
../common/book_json.h
|
||||
main.cc)
|
||||
|
||||
add_executable(${TARGET_NAME} ${SRCS})
|
||||
|
||||
target_link_libraries(${TARGET_NAME} webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(${TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
|
Loading…
Reference in New Issue