Im using JsonUtility.FromJson
Is it possible and how to deserialize enum from string value instead of int
You could do something like this, it parses a string into an enum and catches any errors that might occur:
try
{
MyClass.m_Var = (eMyEnumType)Enum.Parse( typeof(eMyEnumType), inputString );
}
catch
{
Debug.Log("Warning: eMyEnumType not recognised");
MyClass.m_Var = eMyEnumType.Default;
}
Then it won’t be directly doable. Are you parsing some Json that’s not generated by ToJson?
You could pre-parse the string - find every field that’s an enum value, but has a string-representation, and then switch the string with the correct int. That’d take some reflection work, and knowing the type FromJson will return.
You can also do some post-processing; after getting the FromJson result, find all the enums and set them to the correct value.
1 Like