Exemplo n.º 1
0
        private static PrimitiveData serializeObject(object obj, Type peak, ReferenceSerializer references)
        {
            var type = obj.GetType();

            if (type.HasAttribute <ReferenceAttribute>())
            {
                return(new PrimitiveData(null, type, references.AddReference(obj)));
            }

            Dictionary <string, PrimitiveData> objectDictionary = new Dictionary <string, PrimitiveData>();

            foreach (var field in type.GetAllFields(peak, bindingFlags))
            {
                var subObj = field.GetValue(obj);

                if (subObj != null && !field.HasAttribute <NonSerializedAttribute>() && (subObj.GetType().IsValueType || subObj is string || subObj.GetType().HasParameterlessConstructor()))
                {
                    var data = serializeIntoData(subObj, typeof(object), references);
                    if (data.Type == PrimitiveDataType.Invaild)
                    {
                        throw new InvalidOperationException(string.Format("Object ({0}) could not be serialized.", subObj.GetType().Name));
                    }

                    objectDictionary.Add(field.Name, data);
                }
            }

            if (type.HasAttribute <ReferenceableAttribute>())
            {
                return(new PrimitiveData(objectDictionary, type, references.AddReference(obj)));
            }

            return(new PrimitiveData(objectDictionary, type));
        }
Exemplo n.º 2
0
        public static PrimitiveData SerializeIntoData(object obj, Type peak)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("Object was null.");
            }

            ReferenceSerializer references = new ReferenceSerializer();

            return(serializeIntoData(obj, peak, references));
        }
Exemplo n.º 3
0
        private static PrimitiveData serializeArray(object obj, ReferenceSerializer references)
        {
            var type = obj.GetType();

            var enumerable = obj as ICollection;

            List <PrimitiveData> array = new List <PrimitiveData>();

            foreach (var element in enumerable)
            {
                if (element != null && (element.GetType().IsValueType || element is string || element.GetType().HasParameterlessConstructor()))
                {
                    var data = serializeIntoData(element, typeof(object), references);
                    if (data.Type == PrimitiveDataType.Invaild)
                    {
                        throw new InvalidOperationException(string.Format("Object ({0}) could not be serialized.", element.GetType().Name));
                    }

                    array.Add(data);
                }
            }

            return(new PrimitiveData(array));
        }
Exemplo n.º 4
0
        private static PrimitiveData serializeIntoData(object obj, Type peak, ReferenceSerializer references)
        {
            var type = obj.GetType();

            //if (obj == null)
            //    yield return new PrimitiveData();

            if (type.IsSerializable && PrimitiveData.IsAcceptable(obj))
            {
                return(new PrimitiveData(obj));
            }

            if (obj is IEnumerable || obj is ICollection)
            {
                return(serializeArray(obj, references));
            }

            return(serializeObject(obj, peak, references));

            //Dictionary<string, PrimitiveData> objectDictionary = new Dictionary<string, PrimitiveData>();
            //foreach (var field in type.GetAllFields(peak, bindingFlags))
            //{
            //    var subObj = field.GetValue(obj);

            //    if (subObj != null && (subObj.GetType().IsValueType || subObj is string || subObj.GetType().HasParameterlessConstructor()))
            //    {
            //        var data = serializeIntoData(subObj, typeof(object));
            //        if (data.Type == PrimitiveDataType.Invaild)
            //            throw new InvalidOperationException(string.Format("Object ({0}) could not be serialized.", subObj.GetType().Name));

            //        objectDictionary.Add(field.Name, data);
            //    }
            //}

            //return new PrimitiveData(objectDictionary);
        }