5#include <QtCore/qjsonarray.h>
6#include <QtCore/qjsonvalue.h>
7#include <QtCore/qstring.h>
17constexpr int napiToJsonMappingNestingLimit = 1024;
19QJsonValue mapNapiValueToJsonValue(
const QNapi::Value &napiValue,
int remainingNestingLevels);
20QJsonObject mapNapiObjectToJsonObjectImpl(
const QNapi::Object &napiObject,
int remainingNestingLevels);
21QNapi::Value mapJsonValueToNapiValue(napi_env env,
const QJsonValue &jsonValue);
23void checkNapiToJsonMappingRemainingNestingLevels(napi_env env,
int remainingNestingLevels)
25 if (remainingNestingLevels == 0)
26 throw QNapi::makeLoggedException(env,
"JS value nested too deeply to be mapped to JSON");
29QJsonArray mapNapiArrayToJsonArray(
const QNapi::Array &napiArray,
int remainingNestingLevels)
31 checkNapiToJsonMappingRemainingNestingLevels(napiArray.Env(), remainingNestingLevels);
33 const std::uint32_t arrayLength = napiArray.Length();
34 for (
std::uint32_t i = 0; i < arrayLength; ++i) {
35 Napi::HandleScope elementScope(napiArray.Env());
36 auto jsonValue = mapNapiValueToJsonValue(napiArray.Get(i), remainingNestingLevels);
37 jsonArray.append(jsonValue.isUndefined() ? QJsonValue(QJsonValue::Null) : jsonValue);
42QNapi::Array mapJsonArrayToNapiArray(napi_env env,
const QJsonArray &jsonArray)
44 return QNapi::runEscapingHandleScope<QNapi::Array>(
47 const auto arrayLength =
static_cast<std::uint32_t>(jsonArray.size());
48 auto napiArray = QNapi::Array::New(env, arrayLength);
49 for (std::uint32_t i = 0; i < arrayLength; ++i)
50 napiArray.Set(i, mapJsonValueToNapiValue(env, jsonArray.at(i)));
55QJsonValue mapNapiValueToJsonValue(
const QNapi::Value &napiValue,
int remainingNestingLevels)
57 if (napiValue.IsNull())
58 return QJsonValue(QJsonValue::Null);
59 if (napiValue.IsBoolean())
60 return QJsonValue(QNapi::checkedCast<QNapi::Boolean>(napiValue).Value());
61 if (napiValue.IsNumber())
62 return QJsonValue(QNapi::checkedCast<QNapi::Number>(napiValue).DoubleValue());
63 if (napiValue.IsString())
64 return QJsonValue(QString::fromStdString(QNapi::checkedCast<QNapi::String>(napiValue)));
65 if (napiValue.IsArray())
66 return mapNapiArrayToJsonArray(QNapi::checkedCast<QNapi::Array>(napiValue), remainingNestingLevels - 1);
67 if (napiValue.IsObject() && !napiValue.IsFunction())
68 return mapNapiObjectToJsonObjectImpl(QNapi::checkedCast<QNapi::Object>(napiValue), remainingNestingLevels - 1);
69 return QJsonValue(QJsonValue::Undefined);
72QNapi::Value mapJsonValueToNapiValue(napi_env env,
const QJsonValue &jsonValue)
74 switch (jsonValue.type()) {
75 case QJsonValue::Null:
76 case QJsonValue::Undefined:
77 return Napi::Env(env).Null();
78 case QJsonValue::Bool:
79 return QNapi::Boolean::New(env, jsonValue.toBool());
80 case QJsonValue::Double:
81 return QNapi::Number::New(env, jsonValue.toDouble());
82 case QJsonValue::String:
83 return QNapi::String::New(env, jsonValue.toString().toStdString());
84 case QJsonValue::Array:
85 return mapJsonArrayToNapiArray(env, jsonValue.toArray());
86 case QJsonValue::Object:
87 return mapJsonObjectToNapiObject(env, jsonValue.toObject());
89 Q_UNREACHABLE_RETURN(QNapi::Value());
92QJsonObject mapNapiObjectToJsonObjectImpl(
const QNapi::Object &napiObject,
int remainingNestingLevels)
94 checkNapiToJsonMappingRemainingNestingLevels(napiObject.Env(), remainingNestingLevels);
95 QJsonObject jsonObject;
96 for (
const auto &property : napiObject) {
97 if (!property.first.IsString())
99 auto jsonValue = mapNapiValueToJsonValue(property.second, remainingNestingLevels);
100 if (!jsonValue.isUndefined())
101 jsonObject.insert(QString::fromStdString(QNapi::checkedCast<QNapi::String>(property.first)), jsonValue);
110 return mapNapiObjectToJsonObjectImpl(napiObject, napiToJsonMappingNestingLimit);
115 return QNapi::runEscapingHandleScope<QNapi::Object>(
118 auto napiObject = QNapi::Object::New(env);
119 for (
auto propertyIter = jsonObject.begin(); propertyIter != jsonObject.end(); ++propertyIter)
120 napiObject.Set(propertyIter.key().toStdString(), mapJsonValueToNapiValue(env, propertyIter.value()));
QJsonObject mapNapiObjectToJsonObject(const QNapi::Object &napiObject)
QNapi::Object mapJsonObjectToNapiObject(napi_env env, const QJsonObject &jsonObject)