zanders3/json

Issue serializing classes/structs with static variables of the same type

tstavrianos opened this issue · 2 comments

Trying to serialize this struct:

public struct Color {
  public byte R, G, B;
  public Color(byte r, byte g, byte b)
  {
    R = r;
    G = g;
    B = b;
  }
  public static Color Zero = new Color(0, 0, 0);
}

causes a stack overflow.

Initial issue was the byte types. After adding the byte types (along with all the missing numeric types for other classes that need it) it gets stuck at the static (goes into the static and then in the static it finds the static again and so on).
I've bypassed it for now by adding && !fieldInfos[i].IsStatic to the check if a field is public and seems to work.

It will be an issue if someone has a class with public static variables that he wants serialized.

Serializing static variables shouldn't be supported I think - I will add your suggestion. Thanks!

Fixed and added unit tests for it