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): public static T loadJsonObject<T>(string jsonFileName) { //Json Top Level Array Type string jsonString = JsonHelper.LoadJsonResource (jsonFileName); T jsonObject = JsonUtility.FromJson<T> (jsonString); return jsonObject; } 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): public static T[] getJsonArray<T>(string json) { string newJson = "{ \"array\": " + json + "}"; Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson); return wrapper.array; } [Serializable] private class Wrapper<T> { public T[] array; } The wrapper class for wishlist: Code (CSharp): using System; using UnityEngine; namespace Richy.Data { [Serializable] public class Wishlist { public string name; public float price; public string desc; public Wishlist (string name, float price, string desc) { this.name = name; this.price = price; this.desc = desc; } public static Wishlist CreateFromJSON(string jsonString) { return JsonUtility.FromJson<Wishlist>(jsonString); } override public string ToString() { return "name: " + this.name + " price: " + this.price + " desc: " + this.desc; } } } 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.
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.
Thank you I will post a bug report and I have added you the json helper file, where I import the json files.
This right here is an issue: Code (csharp): public static T[] getJsonArray<T>(string json) { string newJson = "{ \"array\": " + json + "}"; Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson); return wrapper.array; } [Serializable] private class Wrapper<T> { public T[] array; } 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): [Serializable] private class WrapperInt : Wrapper<T> { } And serialize that instead.
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?!
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.
yes. Actually I did it the above mentioned way, because of this thread (referring to ffleureys answer). https://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/
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.
Yeah. .NET scripting backend is only used on Windows Store platform, and no other platform in Unity today.
Hi, so basically there is no chance to convert .json Files with top level array in it, to Objects on the HoloLens? thanks.
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.