Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Windows 10 UWP and JsonUtility

Discussion in 'Windows' started by Astarorr, Jan 29, 2016.

  1. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Hello all!
    I have strange issue when using JsonUtility in Win10 UWP .Net build: when i'm trying to deserialize object i'm always getting crash, but this problem doesn't happens when using IL2CPP build.

    Maybe anybody had same issue and resolved it?

    So here is the fun part: in IL2CPP build i'm unable to use IAP, and in .Net build i'm unable to get my app working :)
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    Looks like a bug to me - could we get a bug report? .NET and il2cpp scripting backends use totally different serialization paths, so it kinda makes sense that only one of them crashes.
     
  3. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Sure, i'll make repro
     
  4. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Case 766349
     
  5. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Same problem with 8.1 Universal
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    Thanks, we'll take a look.
     
  7. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Hi! Any news?
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    A fix is inbound, but I'm not sure which release it will hit yet.
     
  9. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Anyway - great news. Thank you!
     
  10. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    So, almost 3 weeks passed, new release 5.3.3 - and bug still not fixed. This is annoying little bit.
    Any estimates when fix (if any) will be released?
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
  12. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    Bug in repro - yes, it's not reproducing anymore, thank you. But after few hours of digging i found another problem with JsonUtility on windows phone :)
    This problem behaves similar to previous, just trows exception and dies.
    On windows phone JsonUtility can't correctly parse null value :)
    If any Reference Type (not Value Type of course, but i think Nullable Value Type will behave in same way) object in json is null - it crashes!
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    Does it work on any other platforms? Unity serialization system doesn't really support nulls.
     
  14. Astarorr

    Astarorr

    Joined:
    Jan 17, 2013
    Posts:
    41
    It works on Windows Phone using IL2CPP and on normal Windows platform. Didn't tested on other platforms.
     
  15. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    It crashes again in 5.4b17 using .NET. Tried putting a try/catch block around that, but it can't be caught. FromJson/FromJsonOverwrite works, but ToJson crashes.
     
    Last edited: May 22, 2016
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    Can we get a bug report on this?
     
  17. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    66
    Hi, I don't know if this is a similar/same issue, but topic was similar enough to make me avoid opening a new thread.

    I'm using JsonUtility with UWP.
    Unity 5.3.4f1
    Windows 10.

    Here's the error
    Code (CSharp):
    1. System.AccessViolationException was unhandled
    2.   HResult=-2147467261
    3.   Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    4.   Source=WinRTBridge
    5.   StackTrace:
    6.        at WinRTBridge.NamedSerializationReader.BeginSequenceGroup(IntPtr name)
    7.        at TextAdvRenderer.Log.Unity_NamedDeserialize(Int32 depth)
    8.        at UnityEngine.Internal.Types.$UnityType1314.$Invoke1(Int64 instance, Int64* args)
    9.        at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    10.        at UnityEngineProxy.InternalCalls.PInvokeCalls.JsonUtility_CUSTOM_FromJson(IntPtr param_0, Int32 param_1)
    11.        at UnityEngineProxy.InternalCalls.JsonUtility_CUSTOM_FromJson(String json, Type type)
    12.        at UnityEngine.JsonUtility.FromJson[T](String json)
    13.        at Win10Serializer.getObject[T](Byte[] toDeserialize)
    14.        at ExtendPermanet.load[T](String fileName)
    15.        at ExtendPermanet.load[T](Permanent fileName)
    16.        at TextAdvRenderer.Awake()
    17.        at TextAdvRenderer.$Invoke1(Int64 instance, Int64* args)
    18.        at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    19.   InnerException:
    20.  
    Here's the relevant code:

    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// usare solo su windows 10
    4. /// </summary>
    5. public class Win10Serializer
    6. {
    7.     public static byte[] getSerialized<T>(T toSerialize) where T : class
    8.     {
    9.         return System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(toSerialize));
    10.  
    11.     }
    12.  
    13.     public static T getObject<T>(byte[] toDeserialize) where T : class
    14.     {
    15.         T result = default(T);
    16.         result =JsonUtility.FromJson<T>(System.Text.Encoding.UTF8.GetString(toDeserialize));
    17.         return result;
    18.     }
    19. }
    20.  
    21.  
    22.  
    23.     /// <summary>
    24.     /// Load the specified fileName.
    25.     /// </summary>
    26.     /// <param name="fileName">File name.</param>
    27.     /// <typeparam name="T">The 1st type parameter.</typeparam>
    28.     public static T load<T>(string fileName) where T : class
    29.     {
    30.         T result = default(T);
    31. #if UNITY_WSA_10_0
    32.  
    33.         Debug.Log("loading " + getPersistentFilePath(fileName) + " " + UnityEngine.Windows.File.Exists(getPersistentFilePath(fileName)));
    34.         if (UnityEngine.Windows.File.Exists(getPersistentFilePath(fileName)))
    35.         {
    36.             result = Win10Serializer.getObject<T>(UnityEngine.Windows.File.ReadAllBytes(getPersistentFilePath(fileName)));
    37.             Debug.Log("loaded " + System.Text.Encoding.UTF8.GetString(UnityEngine.Windows.File.ReadAllBytes(getPersistentFilePath(fileName))));
    38.         }
    39. #else
    40.         //se il file esiste lo apre e carica, se no restituisce il valore di default per quel tipo... merda.
    41.         if (File.Exists(getPersistentFilePath(fileName)))
    42.         {
    43.             BinaryFormatter bf = new BinaryFormatter();
    44.             FileStream file = File.Open(getPersistentFilePath(fileName), FileMode.Open);
    45.             result = (T)bf.Deserialize(file);
    46.             file.Close();
    47.         }
    48. #endif
    49.         return result;
    50.     }
    51.  
    52.  
    53.     [System.Serializable]
    54.     public class Log
    55.     {
    56.         [SerializeField]
    57.         public List<string> all=new List<string>();
    58.     }
    59.  
    60.  
    61.  
    62.     public void Awake()
    63.     {
    64.  
    65.         logIt = Permanent.PlayTestLog.load<Log>();
    66.         if (logIt == null)
    67.             logIt = new Log();
    68.     }
    69.  
    Basically I'm using JsonUtility as a substitute for BinaryFormatter in an utility class that loads and writes files to hard disk via an enum extension method.
    Is that a bug or is there something dumb I'm doing?
     
  18. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    Done. But i am just saving a very simple object which contains 2 lists. The call stack in visual studio shows a problem with GetTypeInfo if I'm interpreting this correctly. If you need more info, I can upload the whole solution.
     
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    @paskal007r yes, that does look like a bug on our side. Could you report a bug as well so it doesn't get lost?

    @daspiela thanks, could you tell me the case number?
     
    paskal007r likes this.
  20. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    799560
     
  21. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    @Tautvydas Zilys: I uploaded my whole project for a different bug report. If it is of help, please take a look at case 799776.
    1. Start game on UWP device
    2. Choose name and submit
    3. Press "Start new game"
    4. Press "Freunde Herausfordern" (not translated yet)
    5. Search for empty string (shows all existing users)
    6. Press "+" on some user
    7. Crash
     
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    You should add that info to the case 799560 bug report, as it will get lost on the forum.
     
  23. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    Ok
     
  24. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    66
  25. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
  26. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    I've figured out the root cause and a fix is under way. There's a workaround, though: add a public int variable as the first field of the object that you want to serialize to Json. For example:

    Code (csharp):
    1. [Serializable]
    2. class CrashingClass
    3. {
    4.     public List<string> myThings;
    5. }
    6.  
    7. [Serializable]
    8. class ThisNoLongerCrashes
    9. {
    10.     public int dummy;
    11.     public List<string> myThings;
    12. }
     
  27. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    @paskal007r I just looked into your bug report. It seems like you're getting hit by two issues. The first one was fixed in 5.3.4p4 (or, if you prefer non-patch releases, you can get 5.3.5f1). The second one is the same issue @daspiela is hitting, and the same workaround should work for you too.
     
    paskal007r likes this.
  28. daspiela

    daspiela

    Joined:
    Mar 5, 2015
    Posts:
    15
    Great, thanks!
     
  29. Mark_Darkrift

    Mark_Darkrift

    Joined:
    Nov 5, 2013
    Posts:
    7
    Hi,

    I believe I am encountering the exact same issue.

    I have an array inside a json file and when trying to parse it, it works fine in the editor and every other platform, but when parsing it in an UWP build with .NET, the variable holding the array is null.

    This is with Unity2017.2.0.f1
     
  30. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    109
  31. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,680
    Can you show how your data structure is defined and how your JSON looks like?