Newtonsoft Json crashes the editor window

Hi all,

My Unity editor consistently crashes while using the JsonConvert.SerializeObject method of “com.unity.nuget.newtonsoft-json” package.
The issue present itself in both @2.0.2 (recommended version) and @3.0.2

I managed to narrow down the problem and created a simple snippet for testing.
The method crashed if encounter a “dot” in the Color property, and works well without the “dot”
Can you please let me know if it behaves this way also for you? Do you know of a way to solve it?

// Color type reference itself in "linear" property
JsonSerializerSettings settings = new()
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};

var workingColor = new Color(0f, 0f, 0f, 0f);
var crashColor = new Color(0.1f, 0f, 0f, 0f); // Contains float value with "dot"

var testColor = workingColor; // Try replace this with crashColor

string json = JsonConvert.SerializeObject(testColor, Formatting.Indented, settings);

Thank you very much in advance.

Generally speaking you cannot serialize any Unity classes, including structs like Color / Color32

You can write your own custom JSON converter to do it for you however, then supply the converter class instance when you serialize / deserialize.

Thank you Kurt, I ended up creating some serializable classes like “ColorVariable” to avoid the issue.