-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JSON query encoding/decoding and modular changes (#65)
* Added prototype for converting between URL components and JSON * Added implementation and tests for converting a query string to a JSON data structure and back * Added optional value to query_iterator * Added error object for JSON query objects * Moved some more deckchairs around
- Loading branch information
Showing
43 changed files
with
469 additions
and
6,997 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) | |
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
find_package(tl-expected CONFIG REQUIRED) | ||
find_package(nlohmann_json CONFIG REQUIRED) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
glynos
Author
Owner
|
||
|
||
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU OR | ||
${CMAKE_CXX_COMPILER_ID} MATCHES Clang) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2020 Glyn Matthews. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef SKYR_URL_JSON_HPP | ||
#define SKYR_URL_JSON_HPP | ||
|
||
#include <string> | ||
#include <system_error> | ||
#include <vector> | ||
#include <tl/expected.hpp> | ||
#include <nlohmann/json.hpp> | ||
#include <skyr/query/query_iterator.hpp> | ||
#include <skyr/percent_encoding/percent_encode.hpp> | ||
#include <skyr/percent_encoding/percent_decode.hpp> | ||
|
||
namespace skyr { | ||
inline namespace v1 { | ||
namespace json { | ||
/// | ||
enum class json_errc { | ||
/// | ||
invalid_query = 1, | ||
}; | ||
|
||
namespace details { | ||
class json_error_category : public std::error_category { | ||
public: | ||
[[nodiscard]] auto name() const noexcept -> const char * override { | ||
return "url json query"; | ||
} | ||
|
||
[[nodiscard]] auto message(int error) const noexcept -> std::string override { | ||
switch (static_cast<json_errc>(error)) { | ||
case json_errc::invalid_query: return "Invalid query object"; | ||
default: return "(Unknown error)"; | ||
} | ||
} | ||
}; | ||
} // namespace details | ||
|
||
/// Creates a `std::error_code` given a `skyr::json::json_errc` value | ||
/// \param error A JSON query error | ||
/// \returns A `std::error_code` object | ||
inline auto make_error_code(json_errc error) noexcept -> std::error_code { | ||
static const details::json_error_category category{}; | ||
return std::error_code(static_cast<int>(error), category); | ||
} | ||
|
||
|
||
inline auto encode_query(const nlohmann::json &json, char separator='&', char equal='=') | ||
-> tl::expected<std::string, std::error_code> { | ||
using namespace std::string_literals; | ||
|
||
auto result = ""s; | ||
|
||
if (!json.is_object()) { | ||
return tl::make_unexpected(make_error_code(json_errc::invalid_query)); | ||
} | ||
|
||
for (auto &[key, value] : json.items()) { | ||
|
||
if (value.is_string()) { | ||
result += percent_encode<std::string>(key); | ||
result += equal; | ||
result += percent_encode<std::string>(value.get<std::string>()); | ||
result += separator; | ||
} | ||
else if (value.is_array()) { | ||
for (auto &element : value.items()) { | ||
result += percent_encode<std::string>(key); | ||
result += equal; | ||
result += percent_encode<std::string>(element.value().get<std::string>()); | ||
result += separator; | ||
} | ||
} | ||
else { | ||
result += percent_encode<std::string>(key); | ||
result += equal; | ||
result += separator; | ||
} | ||
} | ||
|
||
return result.substr(0, result.size() - 1); | ||
} | ||
|
||
inline auto decode_query(std::string_view query, char separator='&', char equal='=') -> nlohmann::json { | ||
if (query[0] == '?') { | ||
query.remove_prefix(1); | ||
} | ||
|
||
nlohmann::json object; | ||
for (auto [key, value] : query_parameter_range(query)) { | ||
const auto key_ = skyr::percent_decode<std::string>(key).value(); | ||
const auto value_ = value? skyr::percent_decode<std::string>(value.value()).value() : std::string(); | ||
|
||
if (object.contains(key_)) { | ||
auto current_value = object[key_]; | ||
if (current_value.is_string()) { | ||
auto prev_value = current_value.get<std::string>(); | ||
object[key_] = std::vector<std::string>{prev_value, value_}; | ||
} | ||
else if (current_value.is_array()) { | ||
auto values = current_value.get<std::vector<std::string>>(); | ||
values.emplace_back(value_); | ||
object[key_] = values; | ||
} | ||
} | ||
else { | ||
object[key_] = value_; | ||
} | ||
} | ||
return object; | ||
} | ||
} // namespace json | ||
} // namespace v1 | ||
} // namespace skyr | ||
|
||
#endif //SKYR_URL_QUERYSTRING_HPP |
Oops, something went wrong.
Is it really required when we are not building tests and examples?