2017-03-01 12:27:57 -05:00
|
|
|
#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>
|
2017-03-01 12:27:57 -05:00
|
|
|
|
|
|
|
#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) {}
|
2017-03-01 12:27:57 -05:00
|
|
|
|
|
|
|
void operator()(const osrm::json::String &string) const
|
|
|
|
{
|
2022-11-16 09:44:36 -05:00
|
|
|
out = Napi::String::New(env, string.value);
|
2017-03-01 12:27:57 -05:00
|
|
|
}
|
|
|
|
|
2022-11-16 09:44:36 -05:00
|
|
|
void operator()(const osrm::json::Number &number) const
|
|
|
|
{
|
|
|
|
out = Napi::Number::New(env, number.value);
|
|
|
|
}
|
2017-03-01 12:27:57 -05:00
|
|
|
|
|
|
|
void operator()(const osrm::json::Object &object) const
|
|
|
|
{
|
2022-11-16 09:44:36 -05:00
|
|
|
Napi::Object obj = Napi::Object::New(env);
|
2017-03-01 12:27:57 -05:00
|
|
|
for (const auto &keyValue : object.values)
|
|
|
|
{
|
2022-11-16 09:44:36 -05:00
|
|
|
Napi::Value child;
|
2024-05-28 12:52:49 -04:00
|
|
|
std::visit(V8Renderer(env, child), keyValue.second);
|
2024-11-03 12:23:23 -05:00
|
|
|
obj.Set(keyValue.first.data(), child);
|
2017-03-01 12:27:57 -05:00
|
|
|
}
|
|
|
|
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());
|
2017-03-01 12:27:57 -05:00
|
|
|
for (auto i = 0u; i < array.values.size(); ++i)
|
|
|
|
{
|
2022-11-16 09:44:36 -05:00
|
|
|
Napi::Value child;
|
2024-05-28 12:52:49 -04:00
|
|
|
std::visit(V8Renderer(env, child), array.values[i]);
|
2022-11-16 09:44:36 -05:00
|
|
|
a.Set(i, child);
|
2017-03-01 12:27:57 -05:00
|
|
|
}
|
|
|
|
out = a;
|
|
|
|
}
|
|
|
|
|
2022-11-16 09:44:36 -05:00
|
|
|
void operator()(const osrm::json::True &) const { out = Napi::Boolean::New(env, true); }
|
2017-03-01 12:27:57 -05:00
|
|
|
|
2022-11-16 09:44:36 -05:00
|
|
|
void operator()(const osrm::json::False &) const { out = Napi::Boolean::New(env, false); }
|
2017-03-01 12:27:57 -05:00
|
|
|
|
2022-11-16 09:44:36 -05:00
|
|
|
void operator()(const osrm::json::Null &) const { out = env.Null(); }
|
2017-03-01 12:27:57 -05:00
|
|
|
|
|
|
|
private:
|
2022-11-16 09:44:36 -05:00
|
|
|
const Napi::Env &env;
|
|
|
|
Napi::Value &out;
|
2017-03-01 12:27:57 -05:00
|
|
|
};
|
|
|
|
|
2022-11-16 09:44:36 -05:00
|
|
|
inline void renderToV8(const Napi::Env &env, Napi::Value &out, const osrm::json::Object &object)
|
2017-03-01 12:27:57 -05:00
|
|
|
{
|
2022-11-16 09:44:36 -05:00
|
|
|
V8Renderer renderer(env, out);
|
2022-10-03 15:43:51 -04:00
|
|
|
renderer(object);
|
2017-03-01 12:27:57 -05:00
|
|
|
}
|
2020-11-26 10:21:39 -05:00
|
|
|
} // namespace node_osrm
|
2017-03-01 12:27:57 -05:00
|
|
|
|
|
|
|
#endif // JSON_V8_RENDERER_HPP
|