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
|
|
|
|
2016-02-12 18:40:17 -05:00
|
|
|
#include <iterator>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <ostream>
|
2016-02-12 18:40:17 -05:00
|
|
|
#include <string>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <vector>
|
2016-02-12 18:40:17 -05:00
|
|
|
|
2015-02-18 04:46:40 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace util
|
|
|
|
{
|
2015-02-18 04:46:40 -05:00
|
|
|
namespace json
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
|
|
|
|
2016-02-10 15:58:36 -05:00
|
|
|
struct Renderer
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
|
|
|
explicit Renderer(std::ostream &_out) : out(_out) {}
|
|
|
|
|
2015-02-26 04:11:33 -05:00
|
|
|
void operator()(const String &string) const
|
|
|
|
{
|
2015-02-25 15:10:09 -05:00
|
|
|
out << "\"";
|
2015-02-26 04:11:33 -05:00
|
|
|
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 << "{";
|
2015-09-14 19:13:31 -04:00
|
|
|
for (auto it = object.values.begin(), end = object.values.end(); it != end;)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
2015-09-14 19:13:31 -04: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 << "[";
|
2015-10-18 03:31:08 -04:00
|
|
|
for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
2015-09-14 19:13:31 -04: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;
|
|
|
|
};
|
|
|
|
|
2016-02-10 15:58:36 -05:00
|
|
|
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('\"');
|
2015-02-26 04:11:33 -05:00
|
|
|
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
|
|
|
|
{
|
2015-09-08 21:31:03 -04:00
|
|
|
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('{');
|
2015-09-14 19:13:31 -04:00
|
|
|
for (auto it = object.values.begin(), end = object.values.end(); it != end;)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
|
|
|
out.push_back('\"');
|
2015-09-14 19:13:31 -04:00
|
|
|
out.insert(out.end(), it->first.begin(), it->first.end());
|
2015-01-27 11:44:46 -05:00
|
|
|
out.push_back('\"');
|
|
|
|
out.push_back(':');
|
|
|
|
|
2015-09-14 19:13:31 -04:00
|
|
|
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('[');
|
2015-09-14 19:13:31 -04:00
|
|
|
for (auto it = array.values.cbegin(), end = array.values.cend(); it != end;)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
2015-09-14 19:13:31 -04: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(']');
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:23:33 -05:00
|
|
|
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
|
|
|
|
2016-02-12 18:23:33 -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
|
|
|
|
2016-02-12 18:23:33 -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);
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:46:40 -05:00
|
|
|
} // namespace json
|
2016-01-05 10:51:13 -05:00
|
|
|
} // namespace util
|
2015-02-18 04:46:40 -05:00
|
|
|
} // namespace osrm
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
#endif // JSON_RENDERER_HPP
|