简体   繁体   中英

Using Unity's JsonUtility.FromJson to deserialize nested JSON

I have written two tests which attempt to deserialize flat and nested objects that succeed and fail respectively, as follows:

using NUnit.Framework;
using UnityEngine;

namespace Tests
{
  public class FromJson
  {
    [Test]
    public void Flat()
    {
      string json = "{\"data\":\"foo\"}";
      Flat deserialized = JsonUtility.FromJson<Flat>(json);
      Assert.AreEqual(deserialized.data, "foo");
      var reserialized = JsonUtility.ToJson(deserialized);
      Assert.AreEqual(json, reserialized);
    }

    [Test]
    public void Nested() 
    {
      string json = "{\"data\":{\"data\":\"foo\"}}";
      Nested deserialized = JsonUtility.FromJson<Nested>(json);
      Assert.AreEqual(deserialized.data.data, "foo");
      var reserialized = JsonUtility.ToJson(deserialized);
      Assert.AreEqual(json, reserialized);
    }

  }
}

public class Flat
{
  public string data;
}

public class Nested
{
  public Data data;
}

public class Data
{
  public string data;
}

The failing output looks like the following:

Nested (0.009s)
---
System.NullReferenceException : Object reference not set to an instance of an object
---
at Tests.FromJson.Nested () [0x0000e] in /Users/bgates/Unity/Virtual Store/Assets/Tests/Serialization.cs:23 
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <ac823e2bb42b41bda67924a45a0173c3>:0

Any help would be appreciated. Thank you!

It turns out I was missing [System.Serializable] .

The following tests pass:

using NUnit.Framework;
using UnityEngine;

namespace Tests
{
  public class FromJson
  {
    [Test]
    public void Flat()
    {
      string json = "{\"foo\":\"bar\"}";
      Flat deserialized = JsonUtility.FromJson<Flat>(json);
      Assert.AreEqual(deserialized.foo, "bar");
      var reserialized = JsonUtility.ToJson(deserialized);
      Assert.AreEqual(json, reserialized);
    }

    [Test]
    public void Nested()
    {
      string json = "{\"foo\":{\"foo\":\"bar\"}}";
      Nested deserialized = JsonUtility.FromJson<Nested>(json);
      Assert.AreEqual(deserialized.foo.foo, "bar");
      var reserialized = JsonUtility.ToJson(deserialized);
      Assert.AreEqual(json, reserialized);
    }
  }
}

[System.Serializable]
class Foo
{
  public string bar;
}

[System.Serializable]
public class Flat
{
  public string foo;
}

[System.Serializable]
public class Nested
{
  public Foo2 foo;
}

[System.Serializable]
public class Foo2
{
  public string foo;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM