ManagedFusion.Serialization.Serializer.SerializeValue C# (CSharp) Méthode

SerializeValue() private méthode

Serializes the value.
private SerializeValue ( object obj, int level, int levelLimit ) : object
obj object The obj.
level int
levelLimit int
Résultat object
        private object SerializeValue(object obj, int level, int levelLimit)
        {
            if (obj == null)
                return obj;

            Type objectType = obj.GetType();

            // make sure the object isn't an easily handled primity type
            if (Type.GetTypeCode(objectType) != TypeCode.Object)
                return obj;

            if (obj is DateTimeOffset)
                return obj;

            if (obj is IModelSerializer)
                return SerializeValue(((IModelSerializer)obj).GetSerializedModel(), level, levelLimit);

            if (obj is IDictionary<string, object>)
            {
                IDictionary<string, object> list = new Dictionary<string, object>();
                foreach (var o in ((IDictionary<string, object>)obj))
                    list.Add((o.Key ?? "").ToString(), SerializeValue(o.Value, level, levelLimit));
                return list;
            }

            if (obj is IDictionary)
            {
                IDictionary<string, object> list = new Dictionary<string, object>();
                foreach (DictionaryEntry o in ((IDictionary)obj))
                    list.Add((o.Key ?? "").ToString(), SerializeValue(o.Value, level, levelLimit));
                return list;
            }

            if (obj is IEnumerable)
            {
                object[] attrs = objectType.GetCustomAttributes(typeof(SerializableCollectionObjectAttribute), true);
                string collectionItemName = null;

                if (attrs.Length > 0)
                {
                    SerializableCollectionObjectAttribute attr = attrs[0] as SerializableCollectionObjectAttribute;
                    collectionItemName = CollectionItemMarker + attr.Name;
                }

                IList<object> list = new List<object>();
                IEnumerable collection = (IEnumerable)obj;

                if (attrs.Length == 0)
                {
                    foreach (object o in collection)
                        list.Add(SerializeValue(o, level, levelLimit));
                }
                else
                {
                    foreach (object o in collection)
                    {
                        IDictionary<string, object> list2 = new Dictionary<string, object>();
                        list2.Add(collectionItemName, SerializeValue(o, level, levelLimit));

                        list.Add(list2);
                    }
                }

                return list;
            }

            IDictionary<string, object> serializedObject = SerializeObject(obj, level, levelLimit);
            return serializedObject.Count == 0 ? obj : serializedObject;
        }