Default values on deserializing

Jun 15, 2013 at 9:31 AM
Hello,

Let's assume I have defined some data classes like this:
    [YAXSerializableType(
        Options = YAXSerializationOptions.DontSerializeNullObjects,
        FieldsToSerialize = YAXSerializationFields.AttributedFieldsOnly)]
    [YAXSerializeAs("data")]
    public class Data
    {
        public Data ()
        {
        }

        public Data(Item item)
        {
            Item = item;
        }

        [YAXSerializableField]
        [YAXSerializeAs("item")]
        public Item Item {
            get;
            set;
        }

        public override string ToString ()
        {
            return string.Format ("[Data: Item={0}]", Item);
        }
    }

    [YAXSerializableType(
        Options = YAXSerializationOptions.DontSerializeNullObjects,
        FieldsToSerialize = YAXSerializationFields.AttributedFieldsOnly)]
    [YAXSerializeAs("item")]
    public class Item
    {
        public Item():
            this("foo")
        {
        }

        public Item(string value)
        {
            Value = value;
        }

        [YAXSerializableField]
        [YAXSerializeAs("val")]
        [YAXAttributeForClass]
        [YAXErrorIfMissed(YAXExceptionTypes.Warning, DefaultValue = "foo")]
        public string Value {
            get;
            set;
        }

        public override string ToString ()
        {
            return string.Format ("[Item: Value={0}]", Value);
        }
    }
When reading the following input
<data><item /></data>
I would like to get this structure:
"[Data: Item=[Item: Value=foo]]"
(the default instance for Item class),
but I get
"[Data: Item=]"
(with Item == null) which could mean that the "<item />" tag is missing.

I am deserializing using this piece of code:
        private static Data Deserialize(string input)
        {
            YAXSerializer serializer = new YAXSerializer(
                typeof(Data),
                YAXExceptionHandlingPolicies.ThrowErrorsOnly,
                YAXExceptionTypes.Warning);
            return (Data)serializer.Deserialize(input);
        }
Am I doing something wrong? If yes, what could it be?
Jun 15, 2013 at 10:43 AM
Have you tried removing the YAXSerializationOptions.DontSerializeNullObjects from the Item class?
Just a thought.
ztp
Jun 15, 2013 at 12:22 PM
I just tried removing the DontSerializeNullObjects option, but problem still exists.