Add a distinct Buffer type for encoding binary data in JSON responses. Treated like a string, but allows other consumers (a-la node-osrm) to recognize and not break string encodings.

This commit is contained in:
Daniel Patterson 2016-02-19 14:41:29 -08:00 committed by Patrick Niklaus
parent 5dc7b79bb6
commit 26453af1b9
3 changed files with 26 additions and 1 deletions

View File

@ -333,7 +333,7 @@ template <class DataFacadeT> class TilePlugin final : public BasePlugin
}
}
json_result.values["pbf"] = buffer;
json_result.values["pbf"] = osrm::util::json::Buffer(buffer);
return Status::Ok;
}

View File

@ -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<String,
Buffer,
Number,
mapbox::util::recursive_wrapper<Object>,
mapbox::util::recursive_wrapper<Array>,

View File

@ -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<char> &_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('\"');