json.net serialization

I’m trying to port an old xna project to monogame. This project uses json.net serialization of data. I found some interesting differences between xna and monogame. It’s causing problems with my porting to monogame. Wondering if anyone knows why this happens.

Point object when serialized using json.net. See differences below.
XNA: “0, 0”
MonoGame: { “X”:0, “Y”:0}

Color object when serialized using json.net. See differences below.
XNA: “0, 0, 0, 0”
MonoGame: { “B”: 0, “G”: 0, “R”: 0, “A”: 255}

Vector2 doesn’t have any differences with using json.net serialization.

Isn’t this just some sliding arbitrary decision they made? “Format drift” if you will…

json uses key/value pairs like in the MonoGame examples above: { “Key”: Value }

The XNA examples above are not json.

Does XNA serialize alpha to 0 while MonoGame serializes it to 255, or was that a typo?

I don’t know why the XNA serializes to a string or if there’s a straightforward way to adjust that, but one option for compatibility is to override the MonoGame serialization style using property methods:

public class MyDataClass{
    public string MyColorString {
        get {
            return MyColor.B + ", " + MyColor.G + ", " + MyColor.R + ", " + MyColor.A;
        set {
            string[] split = value.split(", ");
            MyColor.B = int.Parse(split[0]);
            MyColor.G = int.Parse(split[1]);
            MyColor.R = int.Parse(split[2]);
            MyColor.A = int.Parse(split[3]);

    public Color MyColor { get; set; }

*probably not the best example and may contain errors

@jonathanmcc, you are correct the XNA example are not JSON but if you do a JSON.net serialization on a XNA Point object that’s what it looks like. That’s why I’m confused.

1 Like

@Karob, that was a mistake in my part. It should have been 255. I ended up having to write custom json.net jsonconverters to handle the differences. It was something I was trying to avoid.