question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonSerializationException: Unable to deserialize instance of 'BgDataMessage' for iOS on device..

See original GitHub issue

When 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
VkDevPhcommented, Sep 29, 2022

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

0reactions
deniszykovcommented, Jan 21, 2023

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.

if (typeof(string).Name == "") // any non true expression which is NOT compile time evaluable
{
    new BgDataMessage(...);
}

and this is bug in Unity’s runtime which should be reported:

NullReferenceException: Object reference not set to an instance of an object.
  at System.Linq.Expressions.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found