[Possible Bug] Unity3d XmlSerializer ShouldSerialize not working?

Summary: ShouldSerialize not working with unity but works in normal c# app

Basically i tried serializing my class into xml for save file. i tried using ShouldSerialize*() so that it only serialize when it’s true or something. But the xml file result ignore the ShouldSerialize and write it anyway. I tried making simple test using both Unity3d and Visual Studio c# console app. Unity failed.

  • Unity3d 5.6.0f3
  • Visual Studio 2017
  • Unity Project totally new with no other class beside the one attached
  • I Literally Copy-Paste the code and both succesfully made the file (except unity one still serialize the variable)

Made using Visual Studio 2017 console app (ShouldSerializeLever works)

    using System;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ExampleA test = new ExampleA();
                test.Save(Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"WithoutUnity.xml"));
            }
        }
        public class ExampleA
        {
            public bool Lever = false;
            public bool ShouldSerializeLever()
            {
                return false;
            }
            public void Save(string path)
            {
                var serializer = new XmlSerializer(typeof(ExampleA));
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    serializer.Serialize(stream, this);
                }
            }
        }
    }

Made using Unity3d (ShouldSerializeLever doesn’t works)

    using UnityEngine;
    using System.IO;
    using System.Xml.Serialization;
    
    [System.Serializable]
    public class TestScript : MonoBehaviour
    {
        void Start()
        {
            ExampleA test = new ExampleA();
            test.Save(Path.Combine(Application.dataPath, "WithUnity.xml"));
        }
        public class ExampleA
        {
            public bool Lever = false;
            public bool ShouldSerializeLever()
            {
                return false;
            }
            public void Save(string path)
            {
                var serializer = new XmlSerializer(typeof(ExampleA));
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    serializer.Serialize(stream, this);
                }
            }
        }
    }

Edit: Similar unsolved case to this

Visual Studio compiles the code with the .NET compiler. Unity uses a modified version of the Mono compiler, so they are not 1-to-1 with functionality.

Also, I’ve never seen that ShouldSerialize*() syntax before… Especially without some kind of generics or interface… but I’m not very well versed with serialization.

Anyway, look here for how Unity determines what gets serialized and not.

After upgrading my Unity to 2017.1.0f3 and changing the Scripting Runtime version to experimental (.NET 4.6 equivalent) the ShouldSerialize works.

After few digging apparently Mono’s System.Xml.Serialization add the ShouldSerialize*() option at Sep 5 2014 (around mono 3.x) while Unity use Mono2x (Mono 2.6)

Source: (github history and the commit)

I guessing since Unity still use older version of mono the ShouldSerialize hasn’t been implemented yet