public static TrackableDictionaryTracker <TKey, TValue> Deserialize(ref MessagePackReader reader,
                                                                            MessagePackSerializerOptions options)
        {
            var count = reader.ReadArrayHeader();
            TrackableDictionaryTracker <TKey, TValue> tracker = new TrackableDictionaryTracker <TKey, TValue>();

            for (var i = 0; i < count; i++)
            {
                var operation = reader.ReadInt32();
                var key       = MessagePackSerializer.Deserialize <TKey>(ref reader, options);
                var value     = MessagePackSerializer.Deserialize <TValue>(ref reader, options);
                switch (operation)
                {
                case (int)TrackableDictionaryOperation.Add:
                    tracker.TrackAdd(key, value);
                    break;

                case (int)TrackableDictionaryOperation.Remove:
                    tracker.TrackRemove(key, value);
                    break;

                case (int)TrackableDictionaryOperation.Modify:
                    tracker.TrackModify(key, default(TValue), value);
                    break;
                }
            }

            return(tracker);
        }
コード例 #2
0
        public void Test_DictionaryTracker_Serialize()
        {
            var tracker = new TrackableDictionaryTracker <int, string>();

            tracker.TrackAdd(1, "One");
            AssertTrackerSerialize(tracker);
        }
コード例 #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                return(null);
            }

            var tracker = new TrackableDictionaryTracker <TKey, TValue>();

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

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

                var    key = JsonConvert.DeserializeObject <TKey>(str.Substring(1));
                TValue obj;
                switch (str[0])
                {
                case '+':
                    obj = serializer.Deserialize <TValue>(reader);
                    reader.Read();
                    tracker.TrackAdd(key, obj);
                    break;

                case '-':
                    reader.Read();
                    tracker.TrackRemove(key, default(TValue));
                    break;

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

            return(tracker);
        }