4
\$\begingroup\$

I am trying to use Unity's built in json serialization. It produces empty string. I do not understand why. (I was trying to use NewtonSoft Json.Net but that wasn't working either even though I was following instructions).

Here's the serialization code:

public class Player : MonoBehaviour
{
    void Start ()
    {

        Mission a = new Mission()
        {
            Id = 1,
            Name = "Speak to the Farmer",
            State = MissionState.OnGoing,
            Parent = -1,
            Type = MissionType.Main
        };

        string json = JsonUtility.ToJson(a);
        System.IO.File.WriteAllText("missions2.json", json);
    }
}

Here's the Mission class:

[Serializable]
public enum MissionState
{
    NotStarted,
    OnGoing,
    Finished
}

[Serializable]
public enum MissionType
{
    Main,
    Sideline
}

[Serializable]
public class Mission
{
    public Mission()
    {
        Parent = -1;
    }

    public int Parent { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public MissionState State { get; set; }
    public MissionType Type { get; set; }
}

here's the output: {}

There are no exceptions thrown. The file missions2.json is created. But the data is as shown above. What is wrong that the serialization produces nothing?

Thnx Matt

\$\endgroup\$
5
  • \$\begingroup\$ Did you try JsonConvert.SerializeObject? Or use this method: stackoverflow.com/a/24432626/644496 \$\endgroup\$
    – Skyblade
    Apr 5, 2019 at 16:19
  • \$\begingroup\$ @Skyblade Yes. I tried using newtonsoft. I wasn't able to get unity to compile with it. I followed the directions in the link near the top of my post but was not successful. But I may revisit it if the unity json support doesn't work. \$\endgroup\$
    – tatmanblue
    Apr 5, 2019 at 17:17
  • \$\begingroup\$ Did you fing a solution? We would like to know \$\endgroup\$ Oct 1, 2019 at 16:22
  • \$\begingroup\$ @ruddyillanes I did not. I eventually wrote my own serialization \$\endgroup\$
    – tatmanblue
    Nov 1, 2019 at 3:13
  • \$\begingroup\$ There's a version of Json.Net built specifically for unity. It avoids some of the more reflection/compilation-heavy techniques that Unity doesn't support. It's on the Asset store and it's free, so just add that to your project. It uses the same namespaces and classes as Newtonsoft's Json.Net used throughout .Net : assetstore.unity.com/packages/tools/input-management/… \$\endgroup\$
    – Basic
    Oct 28, 2020 at 13:35

6 Answers 6

2
\$\begingroup\$

Unity's JsonUtility only serializes public fields.

//  you don't even need the Serializable attribute when using JsonUtility
public class Mission {
    public int ID; // just a public field, no SerializeField attribute needed either
}

You should use Newtonsoft's Json library if you need more flexibility.

\$\endgroup\$
1
  • \$\begingroup\$ At the time I wrote this newtonsoft was not available for unity (at the least version I was using). Newtonsoft is the best solution and I am now able to use it. \$\endgroup\$
    – tatmanblue
    Apr 19, 2021 at 11:26
7
\$\begingroup\$

Unity do not serialize properties. You need to have simple fields in order to serialize class. It is one of the limitation of jsonUtility.

In Short,

Use

 public int Parent;

instead of

 public int Parent { get; set; } 
     

in your class .

Otherwise use Newtonsoft.json plugin.

\$\endgroup\$
1
  • \$\begingroup\$ This must be the accepted answer, it is great. \$\endgroup\$ May 31, 2023 at 4:45
5
\$\begingroup\$

if I recall correctly, when you serialize a C# class to JSON, it serializes members, not properties (even if by {get; set;} the compiler creates the members). You can try by marking the members as serializable with [SerializeField] or public access (you can still use C#'s properties to encapsulate your class/structure):

[Serializable]
public class Mission
{
    public Mission()
    {
        Parent = -1;
    }

    [SerializeField] private int Parent;
    [SerializeField] private int Id;
    [SerializeField] private string Name;
    [SerializeField] private string Description;
    [SerializeField] private MissionState State;
    [SerializeField] private MissionType Type;
}

Hope it helps.

\$\endgroup\$
3
  • \$\begingroup\$ That produced the same output for me. In other words, {}. So unfortunately this is not a solution. Thank you for the answer though. \$\endgroup\$
    – tatmanblue
    Apr 5, 2019 at 17:20
  • 1
    \$\begingroup\$ @tatmanblue If the answer does not solve your issue, why do you mark it as accepted? \$\endgroup\$
    – Vaillancourt
    Jul 30, 2019 at 15:02
  • \$\begingroup\$ For me this answer worked using Unity3D's classic JsonUtility - but the key was adding the [Serializable] to the class itself - otherwise I got the empty object '{}' as reported in this question \$\endgroup\$ Jul 12, 2022 at 2:06
0
\$\begingroup\$

I think you should add public to each property.

example:

[Serializable]
public class Mission
{
    public Mission()
    {
        Parent = -1;
    }

    [SerializeField] public int Parent;
    [SerializeField] public int Id;
    [SerializeField] public string Name;
    [SerializeField] public string Description;
    [SerializeField] public MissionState State;
    [SerializeField] public MissionType Type;
}
\$\endgroup\$
1
  • \$\begingroup\$ That changes the purpose a bit. \$\endgroup\$
    – Vaillancourt
    Jul 30, 2019 at 15:03
0
\$\begingroup\$

I think the problem is that you're calling the JSON operation from MonoBehaviour... I had a similar problem, so I created a static class to handle this, calling a function like SaveMission() and it worked.

\$\endgroup\$
-1
\$\begingroup\$

change from

public int Parent { get; set; } public int Id { get; set; }

to

public int Parent; public int Id;

\$\endgroup\$
1
  • \$\begingroup\$ This seems to be the same advice that machineman shared in an answer a year ago. Do you have anything to add that's not covered in this existing answer? \$\endgroup\$
    – DMGregory
    Oct 14, 2021 at 4:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .