Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity iOS Build - JsonException for Newtonsoft.Json.Converters.StringEnumConverter

Discussion in 'Editor & General Support' started by NikoNowo, Feb 17, 2022.

  1. NikoNowo

    NikoNowo

    Joined:
    Feb 17, 2022
    Posts:
    1
    Hey, I hope this forum is the right place to post this problem!

    I've got a Unity 2D Project that I'm working on and usually I'm building the project for Android and iOS Devices, so far everything worked fine, but after I updated my Unity Version to 2020.3.26f1 every time I click on my games start button I receive the following error:


    JsonException: No parameterless constructor defined for 'Newtonsoft.Json.Converters.StringEnumConverter'


    Followed by a lot of more lines of errors, which I attached as a snipped at the bottom of my post.

    Now the weird part is that if I play my game in Unity itself (on a Mac) everything works fine. No error at all. If I build it for Android, fine as well. Only if I build it for my iPad or for a iPad Simulator I am receiving the error, but only after I pressed that one button which starts the game. I assume it has something to do with the connection that is getting setup once I click the button and the message that is being created (which uses the StringEnumConverter), although I'm rather confused why that only happens on my iOS Devices and not in my Unity Editor on my Mac or my Android Tablet.

    I searched my code and found that I use "StringEnumConverter" only like this in my models:

    Code (CSharp):
    1.  
    2. using System.ComponentModel;
    3. using Newtonsoft.Json;
    4. using Newtonsoft.Json.Converters;
    5.  
    6. namespace Heroes.Models
    7. {
    8.     [JsonConverter(typeof(StringEnumConverter))]
    9.     public enum EventCategory
    10.     {
    11.         [Description("Categories")]
    12.         None,
    13.  
    14.         [Description("Example A")]
    15.         ExampleA,
    16.  
    17.         [Description("Example B")]
    18.         ExampleB,
    19.  
    20.         [Description("Example C")]
    21.         ExampleC
    22.     }
    23. }
    The start Button itself Starts an Initialization function which creates an InitRequest that is creating a Request, that is being filled by a Message which we then usually send to our servers like this:

    Code (CSharp):
    1.  
    2.         private void InitRequest(Action<Message> completeHandler)
    3.         {
    4.             Request(
    5.                 Message.CreateInitRequest(),
    6.                 (initRequestResponseMessage) => InitRequestCompleteHandler(initRequestResponseMessage, completeHandler)
    7.             );
    8.         }
    9.  
    10.     public class Message
    11.     {
    12.         [JsonProperty(PropertyName = "type")]
    13.         public MessageType Type { get; set; } = MessageType.Unset;
    14.  
    15.         [JsonProperty(PropertyName = "sessionId")]
    16.         public string SessionId { get; set; } = "";
    17.  
    18.         [JsonProperty(PropertyName = "content")]
    19.         public JObject Content { get; set; } = null;
    20.  
    21.         public Message() { }
    22.  
    23.         private Message(MessageType type, object content, string sessionId = "")
    24.         {
    25.             Type = type;
    26.             Content = SerializationProvider.Serialize(content);
    27.             SessionId = sessionId;
    28.         }
    29.  
    30.         public Message(Action action, string sessionId = "")
    31.             : this(MessageType.Action, action, sessionId)
    32.         {
    33.         }
    34.  
    35.         public static Message CreateInitRequest()
    36.         {
    37.            return new Message(MessageType.InitRequest);
    38.         }
    39.     }
    I searched around the web but couldn't find a real solution so far, maybe I'm not searching for the right thing, so I hope someone here might know a possible solution for this?

    Also as a side note, when I updated my Unity Version I had some errors with my JsonDotNet (apparently it was duplicated after the update). So I deleted my JsonDotNet Folder in my Assets folder along with the Assemblies and some files that were Newtonsoft.Json.dll, could that be the problem? Problem is that when I get this back, I always receive the duplication warning and have to start my Unity Editor in the Safemode until I delete the folder again. After that everything works fine again and I can build my game, with the only exception being that the Start Button is giving me the error on iPad Devices or iPad Simulators.

    Please tell me if more infos are needed and thanks for anyone that can help!

    Code (CSharp):
    1. JsonException: No parameterless constructor defined for 'Newtonsoft.Json.Converters.StringEnumConverter'.
    2. Newtonsoft.Json.Serialization.JsonTypeReflector+<>c__DisplayClass22_0.<GetCreator>b__0 (System.Object[] parameters) (at <00000000000000000000000000000000>:0)
    3. System.Func`2[T,TResult].Invoke (T arg) (at <00000000000000000000000000000000>:0)
    4. Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter (System.Object attributeProvider) (at <00000000000000000000000000000000>:0)
    5. Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) (at <00000000000000000000000000000000>:0)
    6. Newtonsoft.Json.Serialization.DefaultContractResolver.CreatePrimitiveContract (System.Type objectType) (at <00000000000000000000000000000000>:0)
    7. System.Func`2[T,TResult].Invoke (T arg) (at <00000000000000000000000000000000>:0)
    8. System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <00000000000000000000000000000000>:0)
    9. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract& memberContract, System.Object& memberValue) (at <00000000000000000000000000000000>:0)
    10. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at <00000000000000000000000000000000>:0)
    11. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at <00000000000000000000000000000000>:0)
    12. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at <00000000000000000000000000000000>:0)
    13. Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at <00000000000000000000000000000000>:0)
    14. Newtonsoft.Json.Linq.JToken.FromObjectInternal (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) (at <00000000000000000000000000000000>:0)
    15. Newtonsoft.Json.Linq.JObject.FromObject (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) (at <00000000000000000000000000000000>:0)
    16. Heroes.Web.WebService.GetContent (Heroes.Web.Message message) (at <00000000000000000000000000000000>:0)
    17. Heroes.Web.WebService.Request (Heroes.Web.Message requestMessage, System.Action`1[T] responseHandler) (at <00000000000000000000000000000000>:0)
    18. Heroes.UI.WelcomeScreenTrigger.RebootInit (System.Boolean isMecodia, System.Boolean isGameLoaded) (at <00000000000000000000000000000000>:0)
    19. Heroes.UI.WelcomeScreenTrigger.OnExtendedOfflineStartClick () (at <00000000000000000000000000000000>:0)
    20. UnityEngine.Events.UnityAction.Invoke () (at <00000000000000000000000000000000>:0)
    21. UnityEngine.Events.UnityEvent.Invoke () (at <00000000000000000000000000000000>:0)
    22. UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1].Invoke (T1 handler, UnityEngine.EventSystems.BaseEventData eventData) (at <00000000000000000000000000000000>:0)
    23. UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at <00000000000000000000000000000000>:0)
    24. UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) (at <00000000000000000000000000000000>:0)
    25. UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () (at <00000000000000000000000000000000>:0)
    26. UnityEngine.EventSystems.StandaloneInputModule.Process () (at <00000000000000000000000000000000>:0)
    27. Rethrow as JsonException: Error creating 'Newtonsoft.Json.Converters.StringEnumConverter'.
    28. Newtonsoft.Json.Serialization.JsonTypeReflector+<>c__DisplayClass22_0.<GetCreator>b__0 (System.Object[] parameters) (at <00000000000000000000000000000000>:0)
    29. System.Func`2[T,TResult].Invoke (T arg) (at <00000000000000000000000000000000>:0)
    30. Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter (System.Object attributeProvider) (at <00000000000000000000000000000000>:0)
    31. Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) (at <00000000000000000000000000000000>:0)
    32. Newtonsoft.Json.Serialization.DefaultContractResolver.CreatePrimitiveContract (System.Type objectType) (at <00000000000000000000000000000000>:0)
    33. System.Func`2[T,TResult].Invoke (T arg) (at <00000000000000000000000000000000>:0)
    34. System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <00000000000000000000000000000000>:0)
    35. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract& memberContract, System.Object& memberValue) (at <00000000000000000000000000000000>:0)
    36. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at <00000000000000000000000000000000>:0)
    37. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at <00000000000000000000000000000000>:0)
    38. Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at <00000000000000000000000000000000>:0)
    39. Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at <00000000000000000000000000000000>:0)
    40. Newtonsoft.Json.Linq.JToken.FromObjectInternal (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) (at <00000000000000000000000000000000>:0)
    41. Newtonsoft.Json.Linq.JObject.FromObject (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) (at <00000000000000000000000000000000>:0)
    42. Heroes.Web.WebService.GetContent (Heroes.Web.Message message) (at <00000000000000000000000000000000>:0)
    43. Heroes.Web.WebService.Request (Heroes.Web.Message requestMessage, System.Action`1[T] responseHandler) (at <00000000000000000000000000000000>:0)
    44. Heroes.UI.WelcomeScreenTrigger.RebootInit (System.Boolean isMecodia, System.Boolean isGameLoaded) (at <00000000000000000000000000000000>:0)
    45. Heroes.UI.WelcomeScreenTrigger.OnExtendedOfflineStartClick () (at <00000000000000000000000000000000>:0)
    46. UnityEngine.Events.UnityAction.Invoke () (at <00000000000000000000000000000000>:0)
    47. UnityEngine.Events.UnityEvent.Invoke () (at <00000000000000000000000000000000>:0)
    48. UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1].Invoke (T1 handler, UnityEngine.EventSystems.BaseEventData eventData) (at <00000000000000000000000000000000>:0)
    49. UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at <00000000000000000000000000000000>:0)
    50. UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) (at <00000000000000000000000000000000>:0)
    51. UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () (at <00000000000000000000000000000000>:0)
    52. UnityEngine.EventSystems.StandaloneInputModule.Process () (at <00000000000000000000000000000000>:0)
    53. UnityEngine.EventSystems.StandaloneInputModule:Process()
     
  2. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    55
    For what i've read, Newtonsoft and iOS don't get along (if i recall, it's because the AppStore forbid dynamic code emission, which is what Newtonsoft does). With any other platform, the package works fine.