private SetterMethod Read(IDataReader reader, ObjectMember field, object value, Dictionary <string, object> values) { string name = field.Name; if (!reader.IsEmptyElement) { var serializerAttribute = field.Member.GetCustomAttribute <CustomFormatterAttribute>(false); var serializer = serializerAttribute?.Formatter ?? FormatterFromReader(reader, out var type); int cycleId = reader.ReadId(); reader.ReadStartNode(name); object cycleValue = values[name] = serializer.Read(reader, value); if (CycleUtility.ValidCycleType(serializer.Type)) { CycleUtility.GetInstance(reader).AddReference(cycleId, cycleValue); } reader.ReadEndNode(name); // no setter method required return(null); } else { SetterMethod referenceReader = ReadReferencedObject(reader, field.Set); values[name] = null; reader.ReadEmptyNode(field.Name); return(referenceReader); } }
private object ReadArray(IDataReader reader, object o) { List <SetterFunction> objectGetters = new List <SetterFunction>(); long arrayLength = reader.ReadArrayLength(); for (long i = 0; i < arrayLength; i++) { objectGetters.Add(DeserializeItem(reader)); } Array array = (Array)Activator.CreateInstance(Type, objectGetters.Count); var cycleUtility = CycleUtility.GetInstance(reader); cycleUtility.AddCycleSetter(setArray); return(array); void setArray() { for (int i = 0; i < objectGetters.Count; i++) { array.SetValue(objectGetters[i].Invoke(), i); } } }
public static CycleUtility GetInstance(IDataWriter writer) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (!writerCycles.TryGetValue(writer, out var cycleUtility)) { cycleUtility = new CycleUtility(); writerCycles.Add(writer, cycleUtility); } return(cycleUtility); }
private SetterFunction ReadCyclicObject(IDataReader reader) { var cycleUtility = CycleUtility.GetInstance(reader); int refId = reader.ReadReference(); if (refId >= 0) { return(() => cycleUtility.FromReference(refId)); } else { return(null); } }
private SetterMethod ReadReferencedObject(IDataReader reader, ObjectSetter setter) { var cycleUtility = CycleUtility.GetInstance(reader); int id = reader.ReadReference(); if (id >= 0) { return((obj) => setter(obj, cycleUtility.FromReference(id))); } else { return(null); } }
private object ReadList(IDataReader reader, object o) { IList list = (IList)Activator.CreateInstance(Type); var cycleUtility = CycleUtility.GetInstance(reader); long arrayLength = reader.ReadArrayLength(); for (long i = 0; i < arrayLength; i++) { var func = DeserializeItem(reader); cycleUtility.AddCycleSetter(() => list.Add(func())); } return(list); }
public object Read(IDataReader reader, object o) { var values = new Dictionary <string, object>(); var cycleActions = new List <SetterMethod>(); foreach (var field in members) { if (field.HasGetter()) { object existingValue = o == null ? null : field.Get(o); var action = Read(reader, field, existingValue, values); if (action != null) { cycleActions.Add(action); } } } if (o == null) { if (constructor == null) { o = FormatterServices.GetSafeUninitializedObject(Type); } else { o = constructor.Info.Invoke(GetConstructorParameters(values, constructor.Attribute)); } } foreach (var field in members) { if (field.HasSetter()) { var value = values[field.Name]; field.Set(o, value); } } if (cycleActions.Count > 0) { var cycleUtility = CycleUtility.GetInstance(reader); cycleUtility.AddCycleSetters(cycleActions.Select(e => (Action)(() => e.Invoke(o)))); } return(o); }
public T Deserialize <T>(IDataReader reader, object obj) { object value = null; var type = typeof(T); if (!reader.IsEmptyElement) { var formatter = FormatterFactory.Get(type); int id = reader.ReadId(); reader.ReadStartNode(type.Name); value = formatter.Read(reader, obj); if (id > -1) { CycleUtility.GetInstance(reader).AddReference(id, value); } reader.ReadEndNode(type.Name); } CycleUtility.RemoveInstance(reader); return((T)value); }
public void Serialize(IDataWriter writer, object obj) { try { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var type = obj.GetType(); var formatter = FormatterFactory.Get(type); writer.WriteStartNode(type.Name); formatter.Write(writer, obj); writer.WriteEndNode(type.Name); CycleUtility.RemoveInstance(writer); } catch { } }
public void Write(IDataWriter writer, object o) { if (CycleUtility.ValidCycleType(o.GetType())) { var cycleUtil = CycleUtility.GetInstance(writer); if (cycleUtil.TryGetReferenceId(o, out int id)) { writer.WriteReference(id); return; } else { writer.WriteId(id); } } foreach (var field in members) { if (field.HasGetter()) { var fieldValue = field.Get(o); Write(writer, field, fieldValue); } } }