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:
parent
5dc7b79bb6
commit
26453af1b9
@ -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;
|
||||
}
|
||||
|
@ -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>,
|
||||
|
@ -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('\"');
|
||||
|
Loading…
Reference in New Issue
Block a user