Exemple #1
0
        /// <summary>
        /// Сериализует указанный объект в указанный сериализатор
        /// </summary>
        /// <param name="o">Объект для сериализации</param>
        /// <param name="writer">Сериализатор</param>
        /// <returns>
        /// <see langword="true"/> при успехе
        /// </returns>
        private static bool Serialize(object o, SerializeWriter writer)
        {
            var t = o.GetType();

            if (t == typeof(int))
            {
                var i = (int)o;
                return(writer.InOut(ref i));
            }
            if (t == typeof(double))
            {
                var d = (double)o;
                return(writer.InOut(ref d));
            }
            if (t == typeof(float))
            {
                var f = (float)o;
                return(writer.InOut(ref f));
            }
            if (t == typeof(string))
            {
                var s = (string)o;
                return(writer.InOut(ref s));
            }
            if (t == typeof(bool))
            {
                var b = (bool)o;
                return(writer.InOut(ref b));
            }

            if (t.IsArray)
            {
                if (!writer.InOut(SpecialChars.ArrayStart))
                {
                    return(false);
                }
                var first = true;
                foreach (var value in (Array)o)
                {
                    if (!first)
                    {
                        writer.InOut(SpecialChars.Delimiter);
                    }
                    else
                    {
                        first = false;
                    }
                    if (!Serialize(value, writer))
                    {
                        return(false);
                    }
                }
                return(writer.InOut(SpecialChars.ArrayEnd));
            }

            if (t == typeof(Color))
            {
                var c = (Color)o;
                int r = c.R, g = c.G, b = c.B, a = c.A;
                if (!writer.InOut(SpecialChars.ObjectStart))
                {
                    return(false);
                }
                writer.WriteName("r");
                if (!writer.InOut(ref r))
                {
                    return(false);
                }
                writer.InOut(SpecialChars.Delimiter);
                writer.WriteName("g");
                if (!writer.InOut(ref g))
                {
                    return(false);
                }
                writer.InOut(SpecialChars.Delimiter);
                writer.WriteName("b");
                if (!writer.InOut(ref b))
                {
                    return(false);
                }
                writer.InOut(SpecialChars.Delimiter);
                writer.WriteName("a");
                if (!writer.InOut(ref a))
                {
                    return(false);
                }
                return(writer.InOut(SpecialChars.ObjectEnd));
            }
            if (t == typeof(ColorDescription))
            {
                var cd      = (ColorDescription)o;
                var comment = cd.Comment;
                var level   = cd.Level;
                if (!writer.InOut(SpecialChars.ObjectStart))
                {
                    return(false);
                }
                writer.WriteName("comment");
                writer.InOut(ref comment);
                writer.InOut(SpecialChars.Delimiter);
                writer.WriteName("color");
                Serialize(cd.Color, writer);
                writer.InOut(SpecialChars.Delimiter);
                writer.WriteName("level");
                writer.InOut(ref level);
                return(writer.InOut(SpecialChars.ObjectEnd));
            }
            if (t == typeof(Settings))
            {
                var settings = (Settings)o;
                if (!writer.InOut(SpecialChars.ObjectStart))
                {
                    return(false);
                }

                var first = true;
                foreach (var name in settings.Names)
                {
                    if (!first)
                    {
                        writer.InOut(SpecialChars.Delimiter);
                    }
                    else
                    {
                        first = false;
                    }
                    writer.WriteName(name);
                    if (!Serialize(settings[name], writer))
                    {
                        return(false);
                    }
                }

                return(writer.InOut(SpecialChars.ObjectEnd));
            }
            return(false);
        }