public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var objectType = typeof(T);
            var tracker    = (T)Activator.CreateInstance(objectType);

            var           length = reader.ReadArrayHeader();
            List <string> list   = new List <string>();

            for (var i = 0; i < length; i++)
            {
                list.Add(reader.ReadString());
            }

            for (var i = 0; i < length; i++)
            {
                var pi = objectType.GetProperty(list[i]);
                if (reader.TryReadNil())
                {
                    pi.SetValue(tracker, null, null);
                }
                else
                {
                    var data   = reader.ReadBytes();
                    var method = SerializeMethodCache.GetDeserializeMethod(pi.PropertyType);
                    var value  = method.Invoke(null, new object[] { data.Value, options, null });
                    pi.SetValue(tracker, value, null);
                }
            }

            return(tracker);
        }
Exemplo n.º 2
0
        public static IPocoTracker <T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var tracker = new TrackablePocoTracker <T>();
            var length  = reader.ReadArrayHeader();

            List <string> list = new List <string>();

            for (var i = 0; i < length; i++)
            {
                list.Add(reader.ReadString());
            }

            var objectType = typeof(T);

            for (var i = 0; i < length; i++)
            {
                var pi = objectType.GetProperty(list[i]);
                if (reader.TryReadNil())
                {
                    tracker.TrackSet(pi, null, null);
                }
                else
                {
                    var data   = reader.ReadBytes();
                    var method = SerializeMethodCache.GetDeserializeMethod(pi.PropertyType);
                    var value  = method.Invoke(null, new object[] { data.Value, options, null });
                    tracker.TrackSet(pi, null, value);
                }
            }

            return(tracker);
        }
Exemplo n.º 3
0
        public static void Serialize(ref MessagePackWriter writer, IPocoTracker <T> value,
                                     MessagePackSerializerOptions options)
        {
            var tracker = (TrackablePocoTracker <T>)value;

            writer.WriteArrayHeader(tracker.ChangeMap.Count);
            foreach (var item in tracker.ChangeMap)
            {
                writer.Write(item.Key.Name);
            }

            foreach (var item in tracker.ChangeMap)
            {
                if (item.Value.NewValue == null)
                {
                    writer.WriteNil();
                }
                else
                {
                    var method = SerializeMethodCache.GetSerializeMethod(item.Value.NewValue.GetType());
                    var stream = new MemoryStream(10);
                    method.Invoke(null, new object[] { stream, item.Value.NewValue, options, null });
                    writer.Write(stream.ToArray());
                }
            }
        }
        public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
        {
            var properties = new List <Changed>();

            foreach (var pi in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(ITracker).IsAssignableFrom(pi.PropertyType) == false)
                {
                    continue;
                }

                var subTracker = (ITracker)pi.GetValue(value, null);
                if (subTracker != null && subTracker.HasChange)
                {
                    properties.Add(new Changed(pi.Name, subTracker));
                }
            }

            writer.WriteArrayHeader(properties.Count);
            foreach (var pi in properties)
            {
                writer.Write(pi.PropertyName);
            }

            foreach (var pi in properties)
            {
                if (pi.Value == null)
                {
                    writer.WriteNil();
                }
                else
                {
                    var type   = pi.Value.GetType();
                    var method = SerializeMethodCache.GetSerializeMethod(type);
                    var stream = new MemoryStream(10);
                    method.Invoke(null, new object[] { stream, pi.Value, options, null });
                    writer.Write(stream.ToArray());
                }
            }
        }