James McGhee avatar image
James McGhee suggested

Serialize enums as numeric value not string value such as when using EntityObject.DataObject.

for conversation lets assume you have an enum such as

public enum MyCoolEnum{    Standing,    Walking,    Running,}

We have noticed that if we serialize an enum value that the resulting JSON is for example

{ "state": "Running" } 

when really it should be

{ "state": 2 } 

So if you use say


when it reads the enum field and sees that it has a string value it will skip it and default the value meaining if you recorded state = MyCoolEnum.Running. Then PlayFab will store it as
{ "state": "Running" } 

And JsonUtility will parse it as

state = MyCoolEnum.Standing

This is becuase the default value of an enum is 0 which is usually the first entry if your using default enum values. This can be worked around by either setting the enum field in your data model as an int or similar and casting that to the deisred enum or by using Enum.Parse. The Parse method though has some issues ...

An enum is not a string, an enum values name can change without effectign the enum ... for example lets assume I initally defined.

public enum MyCoolEnum{    Standing,    Walking,    Runniing,}

Notice the 2 'i' characters in Running ... its a typo I do a lot, I can catch this some time later and fix it and it shouldn't be an issue at all since anything that serializes an enum should serialize it as its numeric value which didn't change by just fixing the typo.

Granted if I where to do

public enum MyCoolEnum{    Standing,    Crawling,    Walking,    Running,}

That would break it ... of course the standard way to fix that is

public enum MyCoolEnum
Standing = 0,
Crawling = 3,
Walking = 1,
Running = 2

But if you serailize as a string there is no fixing that other than manually parsing the JSON string and handling the variations.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

No Comments


Write a Comment

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Your Opinion Counts

Share your great idea, or help out by voting for other people's ideas.