Skip to content

Commit 6c6aa2b

Browse files
authored
fix: [SDK-4523] Unity custom event nested property encoding (#871)
1 parent 928341d commit 6c6aa2b

5 files changed

Lines changed: 78 additions & 26 deletions

File tree

com.onesignal.unity.android/Runtime/Utilities/AndroidJavaObjectExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ public static AndroidJavaObject ToMap(this Dictionary<string, object> source)
173173
case bool boolValue:
174174
value = new AndroidJavaObject("java.lang.Boolean", boolValue);
175175
break;
176-
case Dictionary<string, object> dictValue:
176+
// Check IDictionary before IList: some dict-like types
177+
// (e.g. Newtonsoft's JObject) also implement IList.
178+
case System.Collections.IDictionary dictValue:
177179
value = new AndroidJavaObject(
178180
"org.json.JSONObject",
179181
Json.Serialize(dictValue)

com.onesignal.unity.core/Runtime/Utilities/MiniJSON.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,16 @@ void SerializeValue(object value)
509509
{
510510
builder.Append((bool)value ? "true" : "false");
511511
}
512-
else if ((asList = value as IList) != null)
513-
{
514-
SerializeArray(asList);
515-
}
512+
// Check IDictionary before IList: some dict-like types (e.g. Newtonsoft's
513+
// JObject) also implement IList, and would otherwise be serialized as arrays.
516514
else if ((asDict = value as IDictionary) != null)
517515
{
518516
SerializeObject(asDict);
519517
}
518+
else if ((asList = value as IList) != null)
519+
{
520+
SerializeArray(asList);
521+
}
520522
else if (value is char)
521523
{
522524
SerializeString(new string((char)value, 1));

examples/demo/Assets/Scripts/UI/Dialogs/TrackEventDialog.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using Newtonsoft.Json;
3+
using OneSignalSDK;
44
using UnityEngine.UIElements;
55

66
namespace OneSignalDemo.UI.Dialogs
@@ -75,14 +75,7 @@ private void ValidateInput()
7575
var propsText = _propertiesField?.value;
7676
if (!string.IsNullOrEmpty(propsText))
7777
{
78-
try
79-
{
80-
JsonConvert.DeserializeObject<Dictionary<string, object>>(propsText);
81-
}
82-
catch
83-
{
84-
_jsonValid = false;
85-
}
78+
_jsonValid = TryParseProperties(propsText, out _);
8679
}
8780

8881
_jsonError.style.display = _jsonValid ? DisplayStyle.None : DisplayStyle.Flex;
@@ -97,20 +90,30 @@ private void OnConfirm()
9790

9891
Dictionary<string, object> props = null;
9992
var propsText = _propertiesField?.value;
100-
if (!string.IsNullOrEmpty(propsText))
93+
if (!string.IsNullOrEmpty(propsText) && !TryParseProperties(propsText, out props))
10194
{
102-
try
103-
{
104-
props = JsonConvert.DeserializeObject<Dictionary<string, object>>(propsText);
105-
}
106-
catch
107-
{
108-
return;
109-
}
95+
return;
11096
}
11197

11298
_onConfirm?.Invoke(name, props);
11399
Dismiss();
114100
}
101+
102+
// Use the SDK's MiniJSON parser so nested objects/arrays come back as plain
103+
// Dictionary<string, object> / List<object>. Newtonsoft would hand back
104+
// JObject/JArray, which the native bridges then mis-serialize.
105+
private static bool TryParseProperties(string propsText, out Dictionary<string, object> props)
106+
{
107+
props = null;
108+
try
109+
{
110+
props = Json.Deserialize(propsText) as Dictionary<string, object>;
111+
}
112+
catch
113+
{
114+
return false;
115+
}
116+
return props != null;
117+
}
115118
}
116119
}

examples/demo/run-android.sh

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,39 @@
66
set -eu
77

88
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
9-
UNITY="${UNITY_PATH:-/Applications/Unity/Hub/Editor/6000.3.6f1/Unity.app/Contents/MacOS/Unity}"
10-
ADB="/Applications/Unity/Hub/Editor/6000.3.6f1/PlaybackEngines/AndroidPlayer/SDK/platform-tools/adb"
9+
10+
# Auto-detect newest Unity 6000.x install under the Hub, unless UNITY_PATH is set.
11+
find_unity() {
12+
if [ -n "${UNITY_PATH:-}" ]; then
13+
echo "$UNITY_PATH"
14+
return
15+
fi
16+
for d in $(ls -1 /Applications/Unity/Hub/Editor 2>/dev/null | sort -rV); do
17+
BIN="/Applications/Unity/Hub/Editor/$d/Unity.app/Contents/MacOS/Unity"
18+
[ -x "$BIN" ] && echo "$BIN" && return
19+
done
20+
}
21+
UNITY="$(find_unity)"
22+
23+
# Resolve adb in this order: $ADB, $ANDROID_HOME/platform-tools, ~/Library/Android/sdk,
24+
# `which adb`, then Unity's bundled Android SDK.
25+
find_adb() {
26+
if [ -n "${ADB:-}" ] && [ -x "$ADB" ]; then echo "$ADB"; return; fi
27+
for CANDIDATE in \
28+
"${ANDROID_HOME:-}/platform-tools/adb" \
29+
"${ANDROID_SDK_ROOT:-}/platform-tools/adb" \
30+
"$HOME/Library/Android/sdk/platform-tools/adb"; do
31+
[ -x "$CANDIDATE" ] && echo "$CANDIDATE" && return
32+
done
33+
if command -v adb >/dev/null 2>&1; then command -v adb; return; fi
34+
if [ -n "$UNITY" ]; then
35+
UNITY_DIR=$(dirname "$(dirname "$(dirname "$(dirname "$UNITY")")")")
36+
CANDIDATE="$UNITY_DIR/PlaybackEngines/AndroidPlayer/SDK/platform-tools/adb"
37+
[ -x "$CANDIDATE" ] && echo "$CANDIDATE" && return
38+
fi
39+
}
40+
ADB="$(find_adb)"
41+
1142
OUTPUT="$SCRIPT_DIR/Build/Android/onesignal-demo.apk"
1243
LOG="$SCRIPT_DIR/Build/build-android.log"
1344
INSTALL=true
@@ -21,6 +52,7 @@ for arg in "$@"; do
2152
done
2253

2354
pick_emulator() {
55+
[ -z "$ADB" ] && echo "adb not found. Set ADB or ANDROID_HOME to your Android SDK." && exit 1
2456
LIST=$("$ADB" devices | awk '/emulator-[0-9]+[[:space:]]+device/{print $1}')
2557
COUNT=$(printf '%s\n' "$LIST" | grep -c . || true)
2658

examples/demo/run-ios.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
set -eu
77

88
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
9-
UNITY="${UNITY_PATH:-/Applications/Unity/Hub/Editor/6000.3.6f1/Unity.app/Contents/MacOS/Unity}"
9+
10+
# Auto-detect newest Unity 6000.x install under the Hub, unless UNITY_PATH is set.
11+
find_unity() {
12+
if [ -n "${UNITY_PATH:-}" ]; then
13+
echo "$UNITY_PATH"
14+
return
15+
fi
16+
for d in $(ls -1 /Applications/Unity/Hub/Editor 2>/dev/null | sort -rV); do
17+
BIN="/Applications/Unity/Hub/Editor/$d/Unity.app/Contents/MacOS/Unity"
18+
[ -x "$BIN" ] && echo "$BIN" && return
19+
done
20+
}
21+
UNITY="$(find_unity)"
22+
1023
XCODE_DIR="$SCRIPT_DIR/Build/iOS"
1124
LOG="$SCRIPT_DIR/Build/build-ios.log"
1225
SCHEME="Unity-iPhone"

0 commit comments

Comments
 (0)