SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

Serializer emits incorect data on second attempt

Open pamidur opened this issue 7 years ago • 8 comments

class Data
    {
        public string Text { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var data = new Data { Text = "text" };

            var s = new Serializer();

            var a1 = s.Serialize(data);
            var a2 = s.Serialize(data);

            Console.WriteLine(a1 == a2);
        }
    }

Result is False Expected is true

pamidur avatar Jan 17 '18 09:01 pamidur

Could you dig the details of what it serializes?

xoofx avatar Jan 17 '18 10:01 xoofx

second one returns

0o/r/n

pamidur avatar Jan 17 '18 10:01 pamidur

Ok. It is because the Serializer maintains a state to support YAML aliasing. You should disable alias using a SerializerSettings.EmitAlias = false

xoofx avatar Jan 17 '18 10:01 xoofx

Ok, I see. Is it possible to drop state? Should I recreate serializer for that?

pamidur avatar Jan 17 '18 11:01 pamidur

As I said above, if you use a proper SerializerSettings with Serializer, by setting SerializerSettings.EmitAlias set to false.

xoofx avatar Jan 17 '18 11:01 xoofx

Just want to make it clear - If I still need aliases but only within single serialization process - I cannot use the same instanse of serializer and I have to create a new one. Correct?

pamidur avatar Jan 17 '18 11:01 pamidur

yes

xoofx avatar Jan 17 '18 11:01 xoofx

The state is kept inside the AnchorSerializer class, it will the top most ChainedSerializer that the Serializer will create if EmitAlias is set to true (it basically means that it is the ObjectSerializer field of the Serializer), then you can safely clear both dictionaries inside the AnchorSerializer.

lassade avatar May 03 '18 05:05 lassade