osrm-backend/include/nodejs/json_v8_renderer.hpp

69 lines
1.8 KiB
C++
Raw Normal View History

#ifndef OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
#define OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
#include "osrm/json_container.hpp"
2022-11-16 09:44:36 -05:00
#include <napi.h>
#include <functional>
namespace node_osrm
{
struct V8Renderer
{
2022-11-16 09:44:36 -05:00
explicit V8Renderer(const Napi::Env &env, Napi::Value &out) : env(env), out(out) {}
void operator()(const osrm::json::String &string) const
{
2022-11-16 09:44:36 -05:00
out = Napi::String::New(env, string.value);
}
2022-11-16 09:44:36 -05:00
void operator()(const osrm::json::Number &number) const
{
out = Napi::Number::New(env, number.value);
}
void operator()(const osrm::json::Object &object) const
{
2022-11-16 09:44:36 -05:00
Napi::Object obj = Napi::Object::New(env);
for (const auto &keyValue : object.values)
{
2022-11-16 09:44:36 -05:00
Napi::Value child;
mapbox::util::apply_visitor(V8Renderer(env, child), keyValue.second);
obj.Set(keyValue.first, child);
}
out = obj;
}
void operator()(const osrm::json::Array &array) const
{
2022-11-16 09:44:36 -05:00
Napi::Array a = Napi::Array::New(env, array.values.size());
for (auto i = 0u; i < array.values.size(); ++i)
{
2022-11-16 09:44:36 -05:00
Napi::Value child;
mapbox::util::apply_visitor(V8Renderer(env, child), array.values[i]);
a.Set(i, child);
}
out = a;
}
2022-11-16 09:44:36 -05:00
void operator()(const osrm::json::True &) const { out = Napi::Boolean::New(env, true); }
2022-11-16 09:44:36 -05:00
void operator()(const osrm::json::False &) const { out = Napi::Boolean::New(env, false); }
2022-11-16 09:44:36 -05:00
void operator()(const osrm::json::Null &) const { out = env.Null(); }
private:
2022-11-16 09:44:36 -05:00
const Napi::Env &env;
Napi::Value &out;
};
2022-11-16 09:44:36 -05:00
inline void renderToV8(const Napi::Env &env, Napi::Value &out, const osrm::json::Object &object)
{
2022-11-16 09:44:36 -05:00
V8Renderer renderer(env, out);
renderer(object);
}
} // namespace node_osrm
#endif // JSON_V8_RENDERER_HPP