5#include <QtCore/qjsonarray.h>
6#include <QtCore/qjsonvalue.h>
7#include <QtCore/qstring.h>
17QJsonValue mapNapiValueToJsonValue(
const QNapi::Value &napiValue);
18QNapi::Value mapJsonValueToNapiValue(napi_env env,
const QJsonValue &jsonValue);
20QJsonArray mapNapiArrayToJsonArray(
const QNapi::Array &napiArray)
23 const std::uint32_t arrayLength = napiArray.Length();
24 for (
std::uint32_t i = 0; i < arrayLength; ++i) {
25 Napi::HandleScope elementScope(napiArray.Env());
26 auto jsonValue = mapNapiValueToJsonValue(napiArray.Get(i));
27 jsonArray.append(jsonValue.isUndefined() ? QJsonValue(QJsonValue::Null) : jsonValue);
32QNapi::Array mapJsonArrayToNapiArray(napi_env env,
const QJsonArray &jsonArray)
34 return QNapi::runEscapingHandleScope<QNapi::Array>(
37 const auto arrayLength =
static_cast<std::uint32_t>(jsonArray.size());
38 auto napiArray = QNapi::Array::New(env, arrayLength);
39 for (std::uint32_t i = 0; i < arrayLength; ++i)
40 napiArray.Set(i, mapJsonValueToNapiValue(env, jsonArray.at(i)));
45QJsonValue mapNapiValueToJsonValue(
const QNapi::Value &napiValue)
47 if (napiValue.IsNull())
48 return QJsonValue(QJsonValue::Null);
49 if (napiValue.IsBoolean())
50 return QJsonValue(QNapi::checkedCast<QNapi::Boolean>(napiValue).Value());
51 if (napiValue.IsNumber())
52 return QJsonValue(QNapi::checkedCast<QNapi::Number>(napiValue).DoubleValue());
53 if (napiValue.IsString())
54 return QJsonValue(QString::fromStdString(QNapi::checkedCast<QNapi::String>(napiValue)));
55 if (napiValue.IsArray())
56 return mapNapiArrayToJsonArray(QNapi::checkedCast<QNapi::Array>(napiValue));
57 if (napiValue.IsObject() && !napiValue.IsFunction())
58 return mapNapiObjectToJsonObject(QNapi::checkedCast<QNapi::Object>(napiValue));
59 return QJsonValue(QJsonValue::Undefined);
62QNapi::Value mapJsonValueToNapiValue(napi_env env,
const QJsonValue &jsonValue)
64 switch (jsonValue.type()) {
65 case QJsonValue::Null:
66 case QJsonValue::Undefined:
67 return Napi::Env(env).Null();
68 case QJsonValue::Bool:
69 return QNapi::Boolean::New(env, jsonValue.toBool());
70 case QJsonValue::Double:
71 return QNapi::Number::New(env, jsonValue.toDouble());
72 case QJsonValue::String:
73 return QNapi::String::New(env, jsonValue.toString().toStdString());
74 case QJsonValue::Array:
75 return mapJsonArrayToNapiArray(env, jsonValue.toArray());
76 case QJsonValue::Object:
77 return mapJsonObjectToNapiObject(env, jsonValue.toObject());
79 Q_UNREACHABLE_RETURN(QNapi::Value());
86 QJsonObject jsonObject;
87 for (
const auto &property : napiObject) {
88 if (!property.first.IsString())
90 auto jsonValue = mapNapiValueToJsonValue(property.second);
91 if (!jsonValue.isUndefined())
92 jsonObject.insert(QString::fromStdString(QNapi::checkedCast<QNapi::String>(property.first)), jsonValue);
99 return QNapi::runEscapingHandleScope<QNapi::Object>(
102 auto napiObject = QNapi::Object::New(env);
103 for (
auto propertyIter = jsonObject.begin(); propertyIter != jsonObject.end(); ++propertyIter)
104 napiObject.Set(propertyIter.key().toStdString(), mapJsonValueToNapiValue(env, propertyIter.value()));
QJsonObject mapNapiObjectToJsonObject(const QNapi::Object &napiObject)
QNapi::Object mapJsonObjectToNapiObject(napi_env env, const QJsonObject &jsonObject)