Search Unity

Compiling for IOS - weird Newtonsoft.Json.Serialization errors

Discussion in 'Scripting' started by Flindt, Aug 8, 2019.

  1. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    As the title say we are trying to do compile program that uses dll's and is set to uses .NET 4.x

    It was running the services fine on windows - and after some few tweaks it also just got it to work on Mac - but now we try to get it running on IOS - but it throws the errors below from Xcode

    But program runs - but it does not work = it does not load the data that we ask.

    We have an idea that it is the il2cpp that compiles in "other" way than the Mono - which we have used until now. But aparently it can not run on IOS.

    We also see that some asks similar question - and are told to use Json.Net.Unity3D instead of Json.NET - but we would like to avoid that + we dont know what that Json.Net.Unity version supports?

    But anyone have any ideas or help - it would be really apreciated?

    "NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor
    at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, System.Boolean skipVisibility) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructor[T] (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateArrayContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0
    at System.Func`2[T,TResult].Invoke (T arg) [0x00000] in <00000000000000000000000000000000>:0
    at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Utilities.ThreadSafeStore`2[TKey,TValue].Get (TKey key) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0
    at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream (System.Type type, System.IO.Stream readStream, System.Text.Encoding effectiveEncoding, System.Net.Http.Formatting.IFormatterLogger formatterLogger) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Formatting.JsonMediaTypeFormatter.ReadFromStream (System.Type type, System.IO.Stream readStream, System.Text.Encoding effectiveEncoding, System.Net.Http.Formatting.IFormatterLogger formatterLogger) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream (System.Type type, System.IO.Stream readStream, System.Net.Http.HttpContent content, System.Net.Http.Formatting.IFormatterLogger formatterLogger) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStreamAsync (System.Type type, System.IO.Stream readStream, System.Net.Http.HttpContent content, System.Net.Http.Formatting.IFormatterLogger formatterLogger) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Formatting.MediaTypeFormatter.ReadFromStreamAsync (System.Type type, System.IO.Stream readStream, System.Net.Http.HttpContent content, System.Net.Http.Formatting.IFormatterLogger formatterLogger, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Headers.TryParseListDelegate`1[T].EndInvoke (System.Collections.Generic.List`1[T]& result, System.IAsyncResult __result) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.Headers.HttpHeaders.SetValue[T] (System.String name, T value, System.Func`2[T,TResult] toStringConverter) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.HttpContentExtensions.ReadAsAsyncCore[T] (System.Net.Http.HttpContent content, System.Type type, System.Net.Http.Formatting.IFormatterLogger formatterLogger, System.Net.Http.Formatting.MediaTypeFormatter formatter, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.HttpContentExtensions.ReadAsAsync[T] (System.Net.Http.HttpContent content, System.Type type, System.Collections.Generic.IEnumerable`1[T] formatters, System.Net.Http.Formatting.IFormatterLogger formatterLogger, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.HttpContentExtensions.ReadAsAsync[T] (System.Net.Http.HttpContent content, System.Type type, System.Collections.Generic.IEnumerable`1[T] formatters, System.Net.Http.Formatting.IFormatterLogger formatterLogger) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.HttpContentExtensions.ReadAsAsync[T] (System.Net.Http.HttpContent content, System.Collections.Generic.IEnumerable`1[T] formatters) [0x00000] in <00000000000000000000000000000000>:0
    at System.Net.Http.HttpContentExtensions.ReadAsAsync[T] (System.Net.Http.HttpContent content) [0x00000] in <00000000000000000000000000000000>:0
    at Interactive.Pilot.Data.InstallationManager+<>c__DisplayClass8_0.<Compare>b__1 (Interactive.Pilot.Data.Installation x) [0x00000] in <00000000000000000000000000000000>:0 "
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It seems you're trying to deserialize script. iOS securoty policy bans loading executable code. You can't publish an iPhone app wich loads and executes arbitrary code. Runtime code generation (this is what deserializer trying to do when loading method) is not supported on iOS too.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You can view the issue in the Json.NET git repo here and here.

    Json.NET For Unity on the Asset Store was specifically created to get around this issue- the problem is that it hasn't really been updated in quite awhile (late 2017)- same as most other versions that have popped up over the years.

    The answer is to use this repo here instead for Unity targeting iOS. This version is using up-to-date Json.NET and will likely be available on the UAS soon, but you can already get it via the package manager if you like, or include it in your project directly, or compile it separately to target Standard 2.0 and just drag in the libraries.
     
    XXIO likes this.
  4. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Thank you both for your replies I will check it out - and hopefully we can get it working with the other repo.
     
  5. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Thanks a lot but I don’t think it helped us. We tried it but without any success.
    We have 2 Dll and we try to do some math in those two dll. But we also use them in other versions = that is why we use the dll.

    I tried with the portable versio of the git - but that still produce the same or similar issues.

    Does anyone have other solutions?
     
  6. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Are you still experiencing this issue with the linked repository https://github.com/jilleJr/Newtonsoft.Json-for-Unity ?
    Am very keen in getting to the bottom of why it may not work for you.

    There can have developed some fixes for any disturbances you experienced. The project is moving quite a lot. Do you receive the identical "NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor" error or does some other error show?
     
  7. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    Hi there @jilleJr - i did not get info about your reply.

    We fixed it all - and It does not have the same error - but for some reason the calculations and data we get through this dl's are wrong/with small differences when running on IOS/MAC unity.

    It look like when it delivers jus pure data - like numbers - then it is correct - but for some reason when it is doing any calculation inside the dll's - then it gives small differences....

    It is based on a date time - that have different values saved...

    Any help on that is apreciated - but i can see that you also updated the version on the git - so maybe i should try loading latest version?

    Thanks
     
  8. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    @jilleJr i just updated to your latest version but unfortunately that does not solve it.
    This means that on mac or iOS - i get different values than on pc...

    Do you have any ideas?
     
  9. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    @Flindt Hi, do you have example of these calculation errors? Are we talking +/- a millisecond, seconds, or minutes?


    One reason could be due to floating point arithmetic errors. According to @joncham in this post:

    ..which would give you oh so slight arithmetic differences between when using Mono (when targeting PC) vs IL2CPP (when targeting iOS). But only when using the float data type. This does not apply to int, long, double, or any other numeric data type in .NET, and neither to the DateTime struct since it's based on the ulong numeric type (at least in Mono https://github.com/mono/mono/blob/f...rivate.CoreLib/shared/System/DateTime.cs#L131).