Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qharmonyjsonconversions.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/private/qnapi_p.h>
6#include <QtCore/private/qohosjsonconversions_p.h>
7#include <QtCore/private/qohoslogger_p.h>
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 Returns a new JS object in \a env with the properties of \a jsonObject,
13 for use on the JS thread. Nested objects and arrays become JS objects and
14 arrays; JSON null becomes JS \c null. Returns \nullptr, logging the error
15 and leaving no exception pending, if a JS exception is raised during the
16 mapping.
17
18 This is how a request object, for example a \c Want, is built for a
19 platform API call from Qt data.
20*/
21napi_value QtHarmony::mapQJsonObjectToJsObject(napi_env env, const QJsonObject &jsonObject)
22{
23 try {
24 return QtOhos::mapJsonObjectToNapiObject(env, jsonObject);
25 } catch (const Napi::Error &error) {
26 qOhosPrintfWarning("%s: mapping failed: %s", Q_FUNC_INFO, error.what());
27 return nullptr;
28 }
29}
30
31/*!
32 Returns the enumerable properties of the JS object \a jsObject in \a env
33 as a QJsonObject. Booleans, numbers and strings become JSON values of the
34 same kind, nested objects and arrays become JSON objects and arrays, and
35 JS \c null becomes JSON null. Property values with no JSON counterpart
36 (\c undefined, functions, symbols, \c BigInt) are skipped in objects and
37 become null in arrays. Returns an empty QJsonObject if \a jsObject is not
38 a JS object (\nullptr, \c null, \c undefined, a primitive) or is an array
39 or a function. It also returns an empty QJsonObject, logging the error and
40 leaving no exception pending, if \a jsObject is nested too deeply, for
41 example because it contains a reference cycle, or if a JS exception is
42 raised while reading it, for example by a property getter.
43
44 This is how a platform object, for example a received \c Want, is taken
45 to the Qt thread.
46*/
47QJsonObject QtHarmony::mapJsObjectToQJsonObject(napi_env env, napi_value jsObject)
48{
49 Napi::Value value(env, jsObject);
50 if (value.IsEmpty() || !value.IsObject() || value.IsArray() || value.IsFunction())
51 return {};
52
53 try {
54 return QtOhos::mapNapiObjectToJsonObject(QNapi::checkedCast<QNapi::Object>(value));
55 } catch (const Napi::Error &error) {
56 qOhosPrintfWarning("%s: mapping failed: %s", Q_FUNC_INFO, error.what());
57 return {};
58 }
59}
60
61QT_END_NAMESPACE