Skip to content

Instantly share code, notes, and snippets.

@joobei
Last active August 12, 2017 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joobei/581f7e3c2ea6dc2a2603bb2dd625b021 to your computer and use it in GitHub Desktop.
Save joobei/581f7e3c2ea6dc2a2603bb2dd625b021 to your computer and use it in GitHub Desktop.
JsonUtility
string jsondata = File.ReadAllText(Application.dataPath + "/../" + fileName + ".json");
Debug.Log (jsondata);
Connection[] temparray = new Connection[JsonHelper.FromJson<Connection>(jsondata).Length];
^---- This is where it breaks.
Debug.Log (temparray.Length);
foreach (Connection connection in JsonHelper.FromJson<Connection>(jsondata))
{
connections.Add (connection);
}
==== ERROR =====
NullReferenceException: Object reference not set to an instance of an object
NodeEditor.OnGUI () (at Assets/Editor/NodeEditor.cs:112)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:272)
UnityEditor.HostView.Invoke (System.String methodName) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:265)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:232)
====== JSONHELPER ======
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[System.Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment