0

Hey I'm trying to use the Unity Json Utility (UnityEngine.JsonUtility) that comes built into the UnityEngine library to serialise a pretty simple object with a single public string field.

My code using the library is pretty straightforward:

var logDTO = new LogDTO();
logDTO.Message = "Unity speaks too!";
var obj = JsonUtility.ToJson(logDTO);

And my object is about as simple as it gets:

[System.Serializable]
public class LogDTO {
    public string Message { get; set; }
}

This is the json result.

Expected: { Message : "Unity speaks too!" }

Actual: {}

You can see the whole thing in the following screenshot:

Simple object not serializing

Other Json libraries like JsonFX work as expected, but for some reason JsonUtility doesn't. Anyone come across this or see what I'm doing wrong with here?

Fehr
  • 476
  • 4
  • 5
  • 1
    IT can't serialize properties. Remove `{ get; set; }`. See the troubleshooting section from the duplicate for more reason you might get an empty json. – Programmer Sep 01 '18 at 07:22
  • Thanks @Programmer. Care to turn that into a post so that I can mark it as a solution? – Fehr Sep 01 '18 at 08:56

0 Answers0