osrm-backend/include/util/json_renderer.hpp

165 lines
3.9 KiB
C++
Raw Normal View History

2015-01-27 11:44:46 -05:00
// based on
// https://svn.apache.org/repos/asf/mesos/tags/release-0.9.0-incubating-RC0/src/common/json.hpp
#ifndef JSON_RENDERER_HPP
#define JSON_RENDERER_HPP
2016-01-02 11:13:44 -05:00
#include "util/cast.hpp"
#include "util/string_util.hpp"
2015-01-27 11:44:46 -05:00
2016-01-02 11:13:44 -05:00
#include "osrm/json_container.hpp"
2015-01-27 11:44:46 -05:00
namespace osrm
{
2016-01-05 10:51:13 -05:00
namespace util
{
namespace json
2015-01-27 11:44:46 -05:00
{
struct Renderer
2015-01-27 11:44:46 -05:00
{
explicit Renderer(std::ostream &_out) : out(_out) {}
void operator()(const String &string) const
{
2015-02-25 15:10:09 -05:00
out << "\"";
out << escape_JSON(string.value);
2015-02-25 15:10:09 -05:00
out << "\"";
}
2015-01-27 11:44:46 -05:00
void operator()(const Number &number) const
{
out.precision(10);
out << number.value;
}
void operator()(const Object &object) const
{
out << "{";
for (auto it = object.values.begin(), end = object.values.end(); it != end;)
2015-01-27 11:44:46 -05:00
{
out << "\"" << it->first << "\":";
mapbox::util::apply_visitor(Renderer(out), it->second);
if (++it != end)
2015-01-27 11:44:46 -05:00
{
out << ",";
}
}
out << "}";
}
void operator()(const Array &array) const
{
out << "[";
for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
2015-01-27 11:44:46 -05:00
{
mapbox::util::apply_visitor(Renderer(out), *it);
if (++it != end)
2015-01-27 11:44:46 -05:00
{
out << ",";
}
}
out << "]";
}
void operator()(const True &) const { out << "true"; }
void operator()(const False &) const { out << "false"; }
void operator()(const Null &) const { out << "null"; }
private:
std::ostream &out;
};
struct ArrayRenderer
2015-01-27 11:44:46 -05:00
{
explicit ArrayRenderer(std::vector<char> &_out) : out(_out) {}
void operator()(const String &string) const
{
out.push_back('\"');
const auto string_to_insert = escape_JSON(string.value);
out.insert(std::end(out), std::begin(string_to_insert), std::end(string_to_insert));
2015-01-27 11:44:46 -05:00
out.push_back('\"');
}
void operator()(const Number &number) const
{
const std::string number_string = cast::to_string_with_precision(number.value);
2015-01-27 11:44:46 -05:00
out.insert(out.end(), number_string.begin(), number_string.end());
}
void operator()(const Object &object) const
{
out.push_back('{');
for (auto it = object.values.begin(), end = object.values.end(); it != end;)
2015-01-27 11:44:46 -05:00
{
out.push_back('\"');
out.insert(out.end(), it->first.begin(), it->first.end());
2015-01-27 11:44:46 -05:00
out.push_back('\"');
out.push_back(':');
mapbox::util::apply_visitor(ArrayRenderer(out), it->second);
if (++it != end)
2015-01-27 11:44:46 -05:00
{
out.push_back(',');
}
}
out.push_back('}');
}
void operator()(const Array &array) const
{
out.push_back('[');
for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
2015-01-27 11:44:46 -05:00
{
mapbox::util::apply_visitor(ArrayRenderer(out), *it);
if (++it != end)
2015-01-27 11:44:46 -05:00
{
out.push_back(',');
}
}
out.push_back(']');
}
void operator()(const True &) const
{
const std::string temp("true");
out.insert(out.end(), temp.begin(), temp.end());
}
2015-01-27 11:44:46 -05:00
void operator()(const False &) const
{
const std::string temp("false");
out.insert(out.end(), temp.begin(), temp.end());
}
2015-01-27 11:44:46 -05:00
void operator()(const Null &) const
{
const std::string temp("null");
out.insert(out.end(), temp.begin(), temp.end());
}
2015-01-27 11:44:46 -05:00
private:
std::vector<char> &out;
};
inline void render(std::ostream &out, const Object &object)
{
Value value = object;
mapbox::util::apply_visitor(Renderer(out), value);
}
inline void render(std::vector<char> &out, const Object &object)
{
Value value = object;
mapbox::util::apply_visitor(ArrayRenderer(out), value);
}
} // namespace json
2016-01-05 10:51:13 -05:00
} // namespace util
} // namespace osrm
2016-01-05 10:51:13 -05:00
2015-01-27 11:44:46 -05:00
#endif // JSON_RENDERER_HPP