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)); }
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)); }
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)); }
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); }