diff --git a/include/engine/plugins/tile.hpp b/include/engine/plugins/tile.hpp index 7eb920d81..647a4cc15 100644 --- a/include/engine/plugins/tile.hpp +++ b/include/engine/plugins/tile.hpp @@ -333,7 +333,7 @@ template class TilePlugin final : public BasePlugin } } - json_result.values["pbf"] = buffer; + json_result.values["pbf"] = osrm::util::json::Buffer(buffer); return Status::Ok; } diff --git a/include/util/json_container.hpp b/include/util/json_container.hpp index b5b14a143..812581499 100644 --- a/include/util/json_container.hpp +++ b/include/util/json_container.hpp @@ -50,6 +50,15 @@ namespace json struct Object; struct Array; +// For encoding raw binary data in a JSON response +struct Buffer +{ + Buffer() {} + Buffer(const char *value) : value(value) {} + Buffer(std::string value) : value(std::move(value)) {} + std::string value; +}; + struct String { String() = default; @@ -78,6 +87,7 @@ struct Null }; using Value = mapbox::util::variant, mapbox::util::recursive_wrapper, diff --git a/include/util/json_renderer.hpp b/include/util/json_renderer.hpp index a1ac7a04a..c02132f4e 100644 --- a/include/util/json_renderer.hpp +++ b/include/util/json_renderer.hpp @@ -25,6 +25,13 @@ struct Renderer { explicit Renderer(std::ostream &_out) : out(_out) {} + void operator()(const Buffer &buffer) const + { + out << "\""; + out << escape_JSON(buffer.value); + out << "\""; + } + void operator()(const String &string) const { out << "\""; @@ -81,6 +88,14 @@ struct ArrayRenderer { explicit ArrayRenderer(std::vector &_out) : out(_out) {} + void operator()(const Buffer &buffer) const + { + out.push_back('\"'); + const auto string_to_insert = escape_JSON(buffer.value); + out.insert(std::end(out), std::begin(string_to_insert), std::end(string_to_insert)); + out.push_back('\"'); + } + void operator()(const String &string) const { out.push_back('\"');