Search Unity

Json Top Level

Discussion in 'VR' started by Sarudil, Mar 16, 2017.

  1. Sarudil

    Sarudil

    Joined:
    Mar 3, 2017
    Posts:
    6
    Hello Folks,

    currently I am trying to import a top level array json file..
    The issue is: it runs in unity editor w/o any issue but when I execute a Windows Store hololens build I got this error:

    InvalidOperationException: Handle is not initialized.
    at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
    at UnityEngine.Internal.$MethodUtility.CreateInstanceAndInvokeDefaultConstructor(IntPtr type, UIntPtr argument0, Int64* argument1)
    at UnityEngineProxy.InternalCalls.PInvokeCalls.JsonUtility_CUSTOM_FromJson(IntPtr param_0, Int32 param_1, Int64& outExceptionHandle)
    at UnityEngineProxy.InternalCalls.JsonUtility_CUSTOM_FromJson(String json, Type type)
    at UnityEngine.JsonUtility.FromJson[T](String json)
    at Json.JsonHelper.getJsonArray[T](String json)
    at Richy.Data.Memory.loadDummyData()
    at Richy.Data.Memory..ctor(Boolean loadDummyData)
    at Richy.RichyBehavior.Start()
    at Richy.RichyBehavior.$Invoke19(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    (Filename: <Unknown> Line: 0)


    The point of error is:
    Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);

    newJson parameter contains, what it should and as said it runs either in UnityEditor or in a normal Windows build, but for Windows Store it won´t run.
    Interestingly when I don´t use a top level array json it works as well.

    In particular


    Code (CSharp):
    1.  
    2.         public static T loadJsonObject<T>(string jsonFileName) {
    3.             //Json Top Level Array Type
    4.             string jsonString = JsonHelper.LoadJsonResource (jsonFileName);
    5.             T jsonObject  = JsonUtility.FromJson<T> (jsonString);
    6.             return jsonObject;
    7.         }
    Additional information:

    File content of wishlist.json (path project/assets/resources/json/wishlist.json)
    [
    { "name": "BMW", "price": 41000, "desc": "New BMW."},
    { "name": "Haus", "price": 750000, "desc": "House"},
    { "name": "Hifi anlage", "price": 1000, "desc": "mega Bass." }
    ]

    I build a json helper to load top-level array json files:
    Code (CSharp):
    1.   public static T[] getJsonArray<T>(string json)
    2.         {
    3.             string newJson = "{ \"array\": " + json + "}";
    4.             Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
    5.             return wrapper.array;
    6.         }
    7.  
    8.         [Serializable]
    9.         private class Wrapper<T>
    10.         {
    11.             public T[] array;
    12.         }

    The wrapper class for wishlist:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace Richy.Data
    5. {
    6.     [Serializable]
    7.     public class Wishlist
    8.     {
    9.         public string name;
    10.         public float price;
    11.         public string desc;
    12.    
    13.         public Wishlist (string name, float price, string desc)
    14.         {
    15.             this.name = name;
    16.             this.price = price;
    17.             this.desc = desc;      
    18.         }  
    19.  
    20.         public static Wishlist CreateFromJSON(string jsonString)
    21.         {
    22.             return JsonUtility.FromJson<Wishlist>(jsonString);
    23.         }
    24.  
    25.         override public string ToString() {
    26.             return "name: " + this.name +  " price: " + this.price + " desc: " + this.desc;
    27.         }
    28.     }
    29. }
    I am not sure what the problem is. My assumption is that it has something to do with different DotNet version used for Windows Store Project.

    Hopefully sb. can help.
     

    Attached Files:

    Last edited: Mar 17, 2017
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    This looks like a bug in Unity. Could we get a bug report with a project that reproduces this issue? I can't really tell how to work around it without looking at the project.
     
  3. Sarudil

    Sarudil

    Joined:
    Mar 3, 2017
    Posts:
    6
    Thank you I will post a bug report and I have added you the json helper file, where I import the json files.
     
    Last edited: Mar 17, 2017
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    This right here is an issue:

    Code (csharp):
    1.        public static T[] getJsonArray<T>(string json)
    2.        {
    3.            string newJson = "{ \"array\": " + json + "}";
    4.            Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
    5.            return wrapper.array;
    6.        }
    7.  
    8.        [Serializable]
    9.        private class Wrapper<T>
    10.        {
    11.            public T[] array;
    12.        }
    13.  
    Unity serialization system does not support non concrete generic types. You cannot serialize/deserialize Wrapper<T>. You'd have to do something like this:

    Code (csharp):
    1.        [Serializable]
    2.        private class WrapperInt : Wrapper<T>
    3.        {
    4.        }
    And serialize that instead.
     
  5. Sarudil

    Sarudil

    Joined:
    Mar 3, 2017
    Posts:
    6
    But does this explain why it runs in editor and NOT in the hololens Project?
    Actually I call the helper like this:
    this.wishlist = JsonHelper.loadJsonArray<Wishlist> ("wishlist.json");

    and during runtime the generic type / datatype is fixed isn´t it?!
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Does it actually deserialize it correctly in the editor? It should not.

    Anyway, the fact that it crashes is a bug (it shouldn't ever crash). I'll need a full unity project that demonstrates the crash to fix it, though.
     
  7. Sarudil

    Sarudil

    Joined:
    Mar 3, 2017
    Posts:
    6
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    If that works in the editor, it means it works by accident - as I mentioned it's not supposed to. This was probably an oversight.

    In Unity, we have 3 different scripting backends: Mono, IL2CPP and .NET. Mono and IL2CPP use one serialization backend, while .NET uses a totally separate one. We always try to keep feature parity between them to minimize friction for our users. Windows Store target by default uses .NET scripting backend (IL2CPP is an option in player settings). And so it happens that generic serialization is not possible on it. It seems that we somehow allowed it on the other serialization backend accidentally.
     
  9. Sarudil

    Sarudil

    Joined:
    Mar 3, 2017
    Posts:
    6
    ok, for me it also works in a "normal" windows build (but not with Microsoft Store)
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Yeah. .NET scripting backend is only used on Windows Store platform, and no other platform in Unity today.
     
  11. Heromanic

    Heromanic

    Joined:
    Jan 25, 2017
    Posts:
    1
    Hi,
    so basically there is no chance to convert .json Files with top level array in it, to Objects on the HoloLens?

    thanks.
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Sure you can, just don't use generics.
     
  13. MauriceGer

    MauriceGer

    Joined:
    Apr 25, 2018
    Posts:
    3
    Hey Heromanic,
    I have exactly the same issue. It works perfectly in the Unity Editor, but it crashes on the Hololens.
    Did you find any solution for your Problem?
    Thank you in advance.