2
\$\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\$
  • \$\begingroup\$ Did you try JsonConvert.SerializeObject? Or use this method: stackoverflow.com/a/24432626/644496 \$\endgroup\$ – Skyblade Apr 5 '19 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 '19 at 17:17
  • \$\begingroup\$ Did you fing a solution? We would like to know \$\endgroup\$ – ruddy illanes Oct 1 '19 at 16:22
  • \$\begingroup\$ @ruddyillanes I did not. I eventually wrote my own serialization \$\endgroup\$ – tatmanblue Nov 1 '19 at 3:13
4
\$\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.

|improve this answer|||||
\$\endgroup\$
  • \$\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 '19 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 '19 at 15:02
0
\$\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.

|improve this answer|||||
\$\endgroup\$
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;
}
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ That changes the purpose a bit. \$\endgroup\$ – Vaillancourt Jul 30 '19 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.