diff options
author | Tor Arne Vestbø <[email protected]> | 2024-08-13 11:58:48 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <[email protected]> | 2024-08-19 08:57:24 +0000 |
commit | 3c3f5813166a04f347f7083fe6a78251db85caac (patch) | |
tree | a809846cb11948024cc733e203be8bed947e57ce | |
parent | e2b1614c5aa84314be9a63f428218b00ba7d6693 (diff) |
Add support for NSArray and NSDictionary in Darwin WebView's fromJSValue
To not have to write NSArray and NSDictionary conversion from scratch
we round-trip via JSON. One quirk of this approach is that NSDate is
supported as a standalone result object, but not nested in an array
or dictionary, as NSJSONSerialization doesn't support that. But at
least this brings us some of the way for array and dictionary support.
Fixes: QTBUG-70166
Pick-to: 6.7 6.5
Change-Id: Ied8a261e964c5adbadb503efb0bb7812ef911b83
Reviewed-by: Christian Strømme <[email protected]>
(cherry picked from commit b1e326ddffc45dd12dd6e454254649686bfdbfa4)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
-rw-r--r-- | src/plugins/darwin/qdarwinwebview.mm | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/plugins/darwin/qdarwinwebview.mm b/src/plugins/darwin/qdarwinwebview.mm index 4ec9fc1..80b9916 100644 --- a/src/plugins/darwin/qdarwinwebview.mm +++ b/src/plugins/darwin/qdarwinwebview.mm @@ -17,6 +17,8 @@ #include <CoreFoundation/CoreFoundation.h> #include <WebKit/WebKit.h> +#include <QtCore/qjsondocument.h> + #ifdef Q_OS_IOS #import <UIKit/UIKit.h> #import <UIKit/UIGestureRecognizerSubclass.h> @@ -528,7 +530,29 @@ QVariant fromJSValue(id result) if ([result isKindOfClass:[NSDate class]]) return QDateTime::fromNSDate(static_cast<NSDate *>(result)); - // JSValue also supports arrays and dictionaries, but we don't handle that yet + if ([result isKindOfClass:[NSArray class]] + || [result isKindOfClass:[NSDictionary class]]) { + @try { + // Round-trip via JSON, so we don't have to implement conversion + // from NSArray and NSDictionary manually. + + // FIXME: NSJSONSerialization requires that any nested object + // is NSString, NSNumber, NSArray, NSDictionary, or NSNull, so + // nested NSDates are not supported -- meaning we support plain + // NSDate (above), but not in an array or dict. To handle this + // use-case we'd need a manual conversion. + auto jsonData = QByteArray::fromNSData( + [NSJSONSerialization dataWithJSONObject:result options:0 error:nil]); + + QJsonParseError parseError; + auto jsonDocument = QJsonDocument::fromJson(jsonData, &parseError); + if (parseError.error == QJsonParseError::NoError) + return jsonDocument.toVariant(); + } @catch (NSException *) { + return QVariant(); + } + } + return QVariant(); } |