diff --git a/modules/juce_core/native/juce_ObjCHelpers_mac.h b/modules/juce_core/native/juce_ObjCHelpers_mac.h index cb5d8e24b8..c7c6175922 100644 --- a/modules/juce_core/native/juce_ObjCHelpers_mac.h +++ b/modules/juce_core/native/juce_ObjCHelpers_mac.h @@ -96,20 +96,17 @@ inline var jsonDataToVar (NSData* jsonData) return JSON::parse (nsStringToJuce ([jsonString autorelease])); } -inline NSDictionary* varObjectToNSDictionary (const var& varToParse) +// If for any reason the given var cannot be converted into a valid dictionary +// an empty dictionary will be returned instead +inline NSDictionary* varToNSDictionary (const var& varToParse) { - jassert (varToParse.isObject()); - - if (! varToParse.isObject()) - return nullptr; - NSError* error { nullptr }; NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData: varToJsonData (varToParse) options: NSJSONReadingMutableContainers error: &error]; - jassert (error == nullptr); - jassert (dictionary != nullptr); + if (dictionary == nullptr || error != nullptr) + return @{}; return dictionary; } @@ -127,7 +124,7 @@ inline NSData* jsonObjectToData (const NSObject* jsonObject) return jsonData; } -inline var nsDictionaryToVar (NSDictionary* dictionary) +inline var nsDictionaryToVar (const NSDictionary* dictionary) { return jsonDataToVar (jsonObjectToData (dictionary)); } diff --git a/modules/juce_core/native/juce_ObjCHelpers_mac_test.mm b/modules/juce_core/native/juce_ObjCHelpers_mac_test.mm index f7c3f185ac..0ad1792d1a 100644 --- a/modules/juce_core/native/juce_ObjCHelpers_mac_test.mm +++ b/modules/juce_core/native/juce_ObjCHelpers_mac_test.mm @@ -49,8 +49,11 @@ public: Array array { 45, 67.8, true, "Hello array!" }; data->setProperty ("array", array); - auto *nsDictionary = varObjectToNSDictionary (data.get()); - auto clone = nsDictionaryToVar (nsDictionary); + const auto* nsDictionary = varToNSDictionary (data.get()); + expect (nsDictionary != nullptr); + + const auto clone = nsDictionaryToVar (nsDictionary); + expect (clone.isObject()); expect (clone.getProperty ("integer", {}).isInt()); expect (clone.getProperty ("double", {}).isDouble()); @@ -62,7 +65,19 @@ public: expect (clone.getProperty ("double", {}) == var { 2.3 }); expect (clone.getProperty ("boolean", {}) == var { true }); expect (clone.getProperty ("string", {}) == var { "Hello world!" }); - expect (clone.getProperty ("array", {}) == array); + expect (clone.getProperty ("array", {}) == var { array }); + } + + beginTest ("varToNSDictionary converts a void variant to an empty dictionary"); + { + var voidVariant; + + const auto* nsDictionary = varToNSDictionary (voidVariant); + expect (nsDictionary != nullptr); + + const auto result = nsDictionaryToVar (nsDictionary); + expect (result.isObject()); + expect (result.getDynamicObject()->getProperties().isEmpty()); } } }; diff --git a/modules/juce_gui_extra/native/juce_PushNotifications_ios.cpp b/modules/juce_gui_extra/native/juce_PushNotifications_ios.cpp index 7827fa063f..105eb86fcf 100644 --- a/modules/juce_gui_extra/native/juce_PushNotifications_ios.cpp +++ b/modules/juce_gui_extra/native/juce_PushNotifications_ios.cpp @@ -57,7 +57,7 @@ struct PushNotificationsDelegateDetails action.title = juceStringToNS (a.title); action.behavior = a.style == Action::text ? UIUserNotificationActionBehaviorTextInput : UIUserNotificationActionBehaviorDefault; - action.parameters = varObjectToNSDictionary (a.parameters); + action.parameters = varToNSDictionary (a.parameters); action.activationMode = a.triggerInBackground ? UIUserNotificationActivationModeBackground : UIUserNotificationActivationModeForeground; action.destructive = (bool) a.destructive; @@ -120,7 +120,7 @@ struct PushNotificationsDelegateDetails auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec); notification.fireDate = [NSDate dateWithTimeIntervalSince1970: (double) triggerTime.toMilliseconds() / 1000.0]; - notification.userInfo = varObjectToNSDictionary (n.properties); + notification.userInfo = varToNSDictionary (n.properties); auto soundToPlayString = n.soundToPlay.toString (true); @@ -152,7 +152,7 @@ struct PushNotificationsDelegateDetails else if (soundToPlayString.isNotEmpty()) content.sound = [UNNotificationSound soundNamed: juceStringToNS (soundToPlayString)]; - auto* propsDict = (NSMutableDictionary*) varObjectToNSDictionary (n.properties); + auto* propsDict = (NSMutableDictionary*) varToNSDictionary (n.properties); [propsDict setObject: juceStringToNS (soundToPlayString) forKey: nsStringLiteral ("com.juce.soundName")]; content.userInfo = propsDict; diff --git a/modules/juce_gui_extra/native/juce_PushNotifications_mac.cpp b/modules/juce_gui_extra/native/juce_PushNotifications_mac.cpp index 85ac5bc2a2..d9e4d177a2 100644 --- a/modules/juce_gui_extra/native/juce_PushNotifications_mac.cpp +++ b/modules/juce_gui_extra/native/juce_PushNotifications_mac.cpp @@ -40,7 +40,7 @@ namespace PushNotificationsDelegateDetailsOsx notification.title = juceStringToNS (n.title); notification.subtitle = juceStringToNS (n.subtitle); notification.informativeText = juceStringToNS (n.body); - notification.userInfo = varObjectToNSDictionary (n.properties); + notification.userInfo = varToNSDictionary (n.properties); auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec); notification.deliveryDate = [NSDate dateWithTimeIntervalSince1970: (double) triggerTime.toMilliseconds() / 1000.0];