DDay.iCal.Serialization.iCalendar.StringSerializer.Deserialize C# (CSharp) Method

Deserialize() public method

public Deserialize ( TextReader tr ) : object
tr TextReader
return object
        public override object Deserialize(TextReader tr)
        {
            if (tr != null)
            {
                string value = tr.ReadToEnd();

                // NOTE: this can deserialize into an IList<string> or simply a string,
                // depending on the input text.  Anything that uses this serializer should
                // be prepared to receive either a string, or an IList<string>.

                bool serializeAsList = false;

                // Determine if we can serialize this property
                // with multiple values per line.
                ICalendarObject co = SerializationContext.Peek() as ICalendarObject;
                if (co is ICalendarProperty)
                    serializeAsList = GetService<IDataTypeMapper>().GetPropertyAllowsMultipleValues(co);

                value = TextUtil.Normalize(value, SerializationContext).ReadToEnd();

                // Try to decode the string
                EncodableDataType dt = null;
                if (co != null)
                {
                    dt = new EncodableDataType();
                    dt.AssociatedObject = co;
                }

                List<string> escapedValues = new List<string>();
                List<string> values = new List<string>();

                int i = 0;
                if (serializeAsList)
                {
                    MatchCollection matches = Regex.Matches(value, @"[^\\](,)");
                    foreach (Match match in matches)
                    {
                        string newValue = dt != null ? Decode(dt, value.Substring(i, match.Index - i + 1)) : value.Substring(i, match.Index - i + 1);
                        escapedValues.Add(newValue);
                        values.Add(Unescape(newValue));
                        i = match.Index + 2;
                    }
                }

                if (i < value.Length)
                {
                    string newValue = dt != null ? Decode(dt, value.Substring(i, value.Length - i)) : value.Substring(i, value.Length - i);
                    escapedValues.Add(newValue);
                    values.Add(Unescape(newValue));
                }

                if (co is ICalendarProperty)
                {
                    // Determine if our we're supposed to store extra information during
                    // the serialization process.  If so, let's store the escaped value.
                    #pragma warning disable 0219
                    ICalendarProperty property = (ICalendarProperty)co;
                    #pragma warning restore 0219
                    ISerializationSettings settings = GetService<ISerializationSettings>();
                    if (settings != null &&
                        settings.StoreExtraSerializationData)
                    {
                        // Store the escaped value
                        co.SetService("EscapedValue", escapedValues.Count == 1 ? 
                            (object)escapedValues[0] :
                            (object)escapedValues);
                    }
                }

                // Return either a single value, or the entire list.
                if (values.Count == 1)
                    return values[0];
                else
                    return values;                
            }
            return null;
        }

Usage Example

        public void String2()
        {
            StringSerializer serializer = new StringSerializer();
            string value = @"test\with\;characters";
            string unescaped = (string)serializer.Deserialize(new StringReader(value));

            Assert.AreEqual(@"test\with;characters", unescaped, "String unescaping was incorrect.");

            value = @"C:\Path\To\My\New\Information";
            unescaped = (string)serializer.Deserialize(new StringReader(value));
            Assert.AreEqual("C:\\Path\\To\\My\new\\Information", unescaped, "String unescaping was incorrect.");

            value = @"\""This\r\nis\Na\, test\""\;\\;,";
            unescaped = (string)serializer.Deserialize(new StringReader(value));

            Assert.AreEqual("\"This\\r\nis\na, test\";\\;,", unescaped, "String unescaping was incorrect.");
        }