Exemplo n.º 1
0
        public static IListTracker <T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var tracker = new TrackableListTracker <T>();
            var length  = reader.ReadArrayHeader();

            for (int i = 0; i < length; i++)
            {
                var operation = reader.ReadInt32();
                var index     = reader.ReadInt32();
                var value     = MessagePackSerializer.Deserialize <T>(ref reader, options);
                switch ((TrackableListOperation)operation)
                {
                case TrackableListOperation.Insert:
                    tracker.TrackInsert(index, value);
                    break;

                case TrackableListOperation.Remove:
                    tracker.TrackRemove(index, value);
                    break;

                case TrackableListOperation.Modify:
                    tracker.TrackModify(index, default(T), value);
                    break;

                case TrackableListOperation.PushFront:
                    tracker.TrackPushFront(value);
                    break;

                case TrackableListOperation.PushBack:
                    tracker.TrackPushBack(value);
                    break;

                case TrackableListOperation.PopFront:
                    tracker.TrackPopFront(default(T));
                    break;

                case TrackableListOperation.PopBack:
                    tracker.TrackPopBack(default(T));
                    break;
                }
            }
            return(tracker);
        }
Exemplo n.º 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.StartArray)
            {
                return(null);
            }

            var tracker = new TrackableListTracker <T>();

            reader.Read();
            while (true)
            {
                if (reader.TokenType != JsonToken.StartArray)
                {
                    break;
                }
                reader.Read();

                var str = (string)reader.Value;
                reader.Read();

                if (str.Length < 2)
                {
                    throw new Exception("Wrong index token: " + str);
                }

                int index;
                if (str[1] == 'F')
                {
                    index = -2;
                }
                else if (str[1] == 'B')
                {
                    index = -1;
                }
                else if (int.TryParse(str.Substring(1), out index) == false)
                {
                    throw new Exception("Invalid token: " + str);
                }

                T obj;
                switch (str[0])
                {
                case '+':
                    obj = serializer.Deserialize <T>(reader);
                    reader.Read();
                    if (index == -2)
                    {
                        tracker.TrackPushFront(obj);
                    }
                    else if (index == -1)
                    {
                        tracker.TrackPushBack(obj);
                    }
                    else
                    {
                        tracker.TrackInsert(index, obj);
                    }
                    break;

                case '-':
                    if (index == -2)
                    {
                        tracker.TrackPopFront(default(T));
                    }
                    else if (index == -1)
                    {
                        tracker.TrackPopBack(default(T));
                    }
                    else
                    {
                        tracker.TrackRemove(index, default(T));
                    }
                    break;

                case '=':
                    obj = serializer.Deserialize <T>(reader);
                    reader.Read();
                    tracker.TrackModify(index, default(T), obj);
                    break;
                }

                if (reader.TokenType != JsonToken.EndArray)
                {
                    throw new Exception("Wrong token type: " + reader.TokenType);
                }

                reader.Read();
            }

            return(tracker);
        }