JsonUtility serialize enumerations as NAME not (int)

I know this is slightly unusual, but is there a way to make an enumeration to serialize to it’s name rather than (int) index?

I understand JSON.NET can do this but JsonUtility is what I’m currently using.

Why I need this is beyond the scope of this discussion.

[SOLVED] I moved to JSON.NET and used [JsonConverter(typeof(StringEnumConverter))]

JsonUtility serializes pretty much the same way that the regular unity serializer works.

And said system is very limited unfortunately. You don’t get a lot of control over how things get serialized.

Do you need to use JsonUtility? Is Json.Net not accessible? I wrote my own compact json serializer so as to avoid Json.Net, but to expand on the functionality that you don’t get with JsonUtility.

I generally have a SaveData class that I use with the JsonUtility. That class has a Load and Save method that grabs data from the raw game class is saving or loads its data into the raw game class it has saved.

This has advantages for a couple reasons,

  1. you don’t often want to save every public variable in a class that you want to save.
  2. you sometimes want to save a private variable or property.
  3. If you need to do some special conversion that the JsonUtility can’t do then you can do that in the Save and Load functions of your SaveData class. In this case, savedEnum = enum.ToString(); And then the conversion back from string on Load().

It’s not ideal and probably not the answer you wanted. There are plenty of other Json Utils around that do let you add custom data serialisers and allow you to customise things a bit.

Thanks for your response, but I wanted to avoid using a secondary class to hold yet another copy of my data.
Also there’s tags you can use to ignore certain fields and properties when serialising. [System.NonSerialized]
Since my post I moved over to JSON.NET very quickly and painlessly and have just about everything I need thanks.
[JsonConverter(typeof(StringEnumConverter))] Lets me store enums by name not integer.

1 Like