Beispiel #1
0
        protected void SerializeValue(ISerializerFactory sf, ICalendarProperty prop, Type valueType, object v, StringBuilder result)
        {
            // Get a serializer to serialize the property's value.
            // If we can't serialize the property's value, the next step is worthless anyway.
            IStringSerializer valueSerializer = sf.Build(valueType, SerializationContext) as IStringSerializer;

            if (valueSerializer != null)
            {
                // Iterate through each value to be serialized,
                // and give it a property (with parameters).
                // FIXME: this isn't always the way this is accomplished.
                // Multiple values can often be serialized within the
                // same property.  How should we fix this?

                // NOTE:
                // We Serialize the property's value first, as during
                // serialization it may modify our parameters.
                // FIXME: the "parameter modification" operation should
                // be separated from serialization. Perhaps something
                // like PreSerialize(), etc.
                string value = valueSerializer.SerializeToString(v);

                // Get the list of parameters we'll be serializing
                ICalendarParameterCollection parameterList = prop.Parameters;
                if (v is ICalendarDataType)
                {
                    parameterList = ((ICalendarDataType)v).Parameters;
                }

                StringBuilder sb = new StringBuilder(prop.Name);
                if (parameterList.Any())
                {
                    // Get a serializer for parameters
                    IStringSerializer parameterSerializer = sf.Build(typeof(ICalendarParameter), SerializationContext) as IStringSerializer;
                    if (parameterSerializer != null)
                    {
                        // Serialize each parameter
                        List <string> parameters = new List <string>();
                        foreach (ICalendarParameter param in parameterList)
                        {
                            parameters.Add(parameterSerializer.SerializeToString(param));
                        }

                        // Separate parameters with semicolons
                        sb.Append(";");
                        sb.Append(string.Join(";", parameters.ToArray()));
                    }
                }
                sb.Append(":");
                sb.Append(value);

                result.Append(TextUtil.WrapLines(sb.ToString()));
            }
        }
Beispiel #2
0
        public override string SerializeToString(object obj)
        {
            ICalendarProperty prop = obj as ICalendarProperty;

            if (prop != null &&
                prop.Values != null &&
                prop.Values.Any())
            {
                // Don't serialize the property if the value is null

                // Push this object on the serialization context.
                SerializationContext.Push(prop);

                IDataTypeMapper mapper         = GetService <IDataTypeMapper>();
                Type            serializedType = mapper.GetPropertyMapping(prop);

                // Get a serializer factory that we can use to serialize
                // the property and parameter values
                ISerializerFactory sf = GetService <ISerializerFactory>();

                StringBuilder result = new StringBuilder();
                foreach (object v in prop.Values)
                {
                    // Only serialize the value to a string if it
                    // is non-null.
                    if (v != null)
                    {
                        // Get a serializer to serialize the property's value.
                        // If we can't serialize the property's value, the next step is worthless anyway.
                        IStringSerializer valueSerializer = sf.Build(v.GetType(), SerializationContext) as IStringSerializer;
                        if (valueSerializer != null)
                        {
                            // Iterate through each value to be serialized,
                            // and give it a property (with parameters).
                            // FIXME: this isn't always the way this is accomplished.
                            // Multiple values can often be serialized within the
                            // same property.  How should we fix this?

                            // NOTE:
                            // We Serialize the property's value first, as during
                            // serialization it may modify our parameters.
                            // FIXME: the "parameter modification" operation should
                            // be separated from serialization. Perhaps something
                            // like PreSerialize(), etc.
                            string value = valueSerializer.SerializeToString(v);

                            // Get the list of parameters we'll be serializing
                            ICalendarParameterCollection parameterList = prop.Parameters;
                            if (v is ICalendarDataType)
                            {
                                parameterList = ((ICalendarDataType)v).Parameters;
                            }

                            StringBuilder sb = new StringBuilder(prop.Name);
                            if (parameterList.Any())
                            {
                                // Get a serializer for parameters
                                IStringSerializer parameterSerializer = sf.Build(typeof(ICalendarParameter), SerializationContext) as IStringSerializer;
                                if (parameterSerializer != null)
                                {
                                    // Serialize each parameter
                                    List <string> parameters = new List <string>();
                                    foreach (ICalendarParameter param in parameterList)
                                    {
                                        parameters.Add(parameterSerializer.SerializeToString(param));
                                    }

                                    // Separate parameters with semicolons
                                    sb.Append(";");
                                    sb.Append(string.Join(";", parameters.ToArray()));
                                }
                            }
                            sb.Append(":");
                            sb.Append(value);

                            result.Append(TextUtil.WrapLines(sb.ToString()));
                        }
                    }
                }

                // Pop the object off the serialization context.
                SerializationContext.Pop();

                return(result.ToString());
            }
            return(null);
        }