JsonSerializationException: Unable to deserialize instance of 'BgDataMessage' for iOS on device..
See original GitHub issueWhen exporting my Unity project for iOS, the following exception is thrown during runtime:
JsonSerializationException: Unable to deserialize instance of ‘BgDataMessage’ because there is no parameterless constructor is defined on type.
It’s working fine while playing on Unity Editor. Unity version : 2020.2.2f1 Colyseus SDK version : 0.14.19
Following is the BgDataMessage.cs. class I’m using.
using System.Collections.Generic;
public class BgDataMessage
{
public int max_win_streak, total_games_played, total_games_win;
public float turn_timeout_seconds;
public ulong bet_amount, max_bet_amount;
public bool can_reconnect;
public string _id, user_id, last_game_room_id, last_game_session_id;
public Dictionary<string, List<long>> bet_ranges;
}
Here is how I’m registering for OnMessage:
_roomLobby.OnMessage<BgDataMessage>(BgGameManager.MessageBgData, (bgData) => OnMessageBgData?.Invoke(bgData));
Xcode Stacktrace on Exception:
JsonSerializationException: Unable to deserialize instance of 'BgDataMessage' because there is no parameterless constructor is defined on type.
at GameDevWare.Serialization.Metadata.TypeDescription.CreateInstance () [0x00000] in <00000000000000000000000000000000>:0
at GameDevWare.Serialization.Serializers.ObjectSerializer.PopulateInstance (GameDevWare.Serialization.IndexedDictionary`2[KeyT,ValueT] container, System.Object instance) [0x00000] in <00000000000000000000000000000000>:0
at GameDevWare.Serialization.Serializers.ObjectSerializer.Deserialize (GameDevWare.Serialization.IJsonReader reader) [0x00000] in <00000000000000000000000000000000>:0
at GameDevWare.Serialization.JsonReaderExtentions.ReadValue (GameDevWare.Serialization.IJsonReader reader, System.Type valueType, System.Boolean nextToken) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Enumerable+<OfTypeIterator>d__97`1[TResult].System.Collections.IEnumerable.GetEnumerator () [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncTaskCache.CreateCacheableTask[TResult] (TResult result) [0x00000] in <00000000000000000000000000000000>:0
at Colyseus.ColyseusRoom`1[T].ParseMessage (System.Byte[] bytes) [0x00000] in <00000000000000000000000000000000>:0
at NativeWebSocket.WebSocketMessageEventHandler.Invoke (System.Byte[] data) [0x00000] in <00000000000000000000000000000000>:0
at NativeWebSocket.WebSocket.DispatchMessageQueue () [0x00000] in <00000000000000000000000000000000>:0
at Colyseus.ColyseusClient+ColyseusAddRoomEventHandler.EndInvoke (System.IAsyncResult result) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.SendOrPostCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.SendOrPostCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.UnitySynchronizationContext.Exec () [0x00000] in <00000000000000000000000000000000>:0
UnityEngine.UnitySynchronizationContext:Exec()
UnityEngine.UnitySynchronizationContext:Exec()
Kindly help me to resolve the issue ASAP as my game is ready and I’m not able to play it on device. Totally stuck! 😦
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
"Newtonsoft.Json.JsonSerializationException unable to ...
You need to have an empty constructor in Currency class in order to deserialize it.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi Edel, Do we have any update on it?
NullReferenceException at line no. 213 in Class MetadataReflection.cs.
I’m still stuck here and not able to play the game on an iOS device. waiting for a reply from your end.
Thanks
Hi @VkDevPh! This is case of Unity code stripping. You need to setup link.xml and make sure ILLinker would not delete constructor. As alternative solution you could reference anywhere in you code this constructor and it will retain.
and this is bug in Unity’s runtime which should be reported: