Hi,
Not sure who maintains MG at the moment but I wanted to discuss improving structs serialization here before doing a PR that might just get rejected, since it might be considered a breaking change.
So here’s the deal - MG base structs are not serialization friendly. And that’s a huge bummer.
For example, if we choose the built-in C# JSON serializer with the following class:
class MyClass
{
public Vector2 SomeVector { get; set; } = new Vector2(42, 11);
public Rectangle SomeRect { get; set; } = new Rectangle(12, 44, 111, 334);
public Color SomeColor { get; set; } = Color.Red;
}
The result will be this:
{
"SomeVector": {},
"SomeRect": {
"Left": 12,
"Right": 123,
"Top": 44,
"Bottom": 378,
"IsEmpty": false,
"Location": {},
"Size": {},
"Center": {}
},
"SomeColor": {
"B": 0,
"G": 0,
"R": 255,
"A": 255,
"PackedValue": 4278190335
}
}
As you can see this have several issues:
- Vectors don’t serialize at all.
- Rectangles serialize lots of useless junk.
- Colors - could argue it shouldn’t serialize PackedValue.
IMO the vectors case is quite simple - they don’t serialize at all, which is a shame.
The redundant fields in Rectangle and Color are more an open question - on one hand they are useful for deserialization, ie its good that I can either deserialize R, G, B keys, or a single PackedValue key instead. However I’m not sure its smart to also Serialize them all by default.
Anyway what’s your opinion on the subject? This all can be fixed with some attributes but if I do a PR, will it be accepted?
Let’s discuss this topic, and also I’d love to hear alternative solutions if any of you also use JSON files.
PS I know there’s also the option of XML content files that serialize alright, but I prefer to avoid those as they add noise and are less user friendly.
Thanks!