Esempio n. 1
0
        private static void WriteObject(object o, TextWriter w, ObjectGraphContext context, int tabLevel)
        {
            // serialize object type and field count
            if (o is IDataObject)
            {
                // use data object code! :D
                var type = o.GetType();
                var data = (o as IDataObject).Data;
                w.WriteLine("p" + data.Count + ":");
                foreach (var kvp in data)
                {
                    var pname = kvp.Key;
                    var val   = kvp.Value;
                    var prop  = ObjectGraphContext.GetKnownProperties(type)[pname];
                    if (prop != null)
                    {
                        var ptype = prop.PropertyType;
                        WriteProperty(w, o, ptype, pname, val, context, tabLevel);
                    }
                    else
                    {
                        // TODO - if property doesn't exist, log a warning somewhere?
                        WriteProperty(w, o, typeof(object), pname, val, context, tabLevel);
                    }
                }
            }
            else
            {
                // use reflection :(
                var type  = o.GetType();
                var props = ObjectGraphContext.GetKnownProperties(type).Values.Where(p =>
                                                                                     p.HasAttribute <ForceSerializationWhenDefaultValueAttribute>() || // force serialization of property even if value is null/default?
                                                                                     !p.GetValue(o, null).SafeEquals(p.PropertyType.DefaultValue()));  // property value is not null/default?
                w.WriteLine("p" + props.Count() + ":");
                foreach (var p in props.OrderBy(p => GetSerializationPriority(p)))
                {
                    WriteProperty(w, o, p.PropertyType, p.Name, context.GetObjectProperty(o, p), context, tabLevel);
                }
            }

            // write end object
            for (int i = 0; i < tabLevel; i++)
            {
                w.Write('\t');
            }
            w.WriteLine(";");
        }
Esempio n. 2
0
        private static void WriteCollection(IEnumerable list, TextWriter w, ObjectGraphContext context, int tabLevel)
        {
            var tabs = new string('\t', tabLevel);

            // collections get size and elements listed out
            Type itemType;
            var  type   = list.GetType();
            bool isDict = false;

            if (type.GetGenericArguments().Length == 2)
            {
                // HACK - assume it's a dictionary, no real way to test
                itemType = typeof(KeyValuePair <,>).MakeGenericType(type.GetGenericArguments());
                w.WriteLine("d" + list.Cast <object>().Count() + ":" + tabs);
                isDict = true;
            }
            else if (type.BaseType.GetGenericArguments().Length == 2)
            {
                // HACK - Resources inherits from a dictionary type
                itemType = typeof(KeyValuePair <,>).MakeGenericType(type.BaseType.GetGenericArguments());
                w.WriteLine("d" + list.Cast <object>().Count() + ":" + tabs);
                isDict = true;
            }
            else if (type == typeof(DynamicDictionary))
            {
                itemType = typeof(KeyValuePair <object, object>);
                w.WriteLine("d" + list.Cast <object>().Count() + ":" + tabs);
                isDict = true;
            }
            else if (type.GetGenericArguments().Length == 1)
            {
                // HACK - assume it's a collection, no real way to test
                itemType = type.GetGenericArguments()[0];
                w.WriteLine("c" + list.Cast <object>().Count() + ":" + tabs);
            }
            else
            {
                // no generic type? probably a list of objects?
                itemType = typeof(object);
                w.WriteLine("c" + list.Cast <object>().Count() + ":" + tabs);
            }
            foreach (var item in list)
            {
                if (isDict)
                {
                    var keyprop = ObjectGraphContext.GetKnownProperties(itemType)["Key"];
                    var valprop = ObjectGraphContext.GetKnownProperties(itemType)["Value"];
                    Serialize(context.GetObjectProperty(item, keyprop), w, keyprop.PropertyType, context, tabLevel + 1);
                    Serialize(context.GetObjectProperty(item, valprop), w, valprop.PropertyType, context, tabLevel + 1);
                }
                else
                {
                    Serialize(item, w, itemType, context, tabLevel + 1);
                }
            }

            // write end object
            for (int i = 0; i < tabLevel; i++)
            {
                w.Write('\t');
            }
            w.WriteLine(";");
        }