Exemplo n.º 1
0
        public Dictionary <TKey, TValue> Unmarshall(JsonUnmarshallerContext context)
        {
            context.Read();
            if (context.CurrentTokenType == JsonToken.Null)
            {
                return(new Dictionary <TKey, TValue>());
            }
            Dictionary <TKey, TValue> dictionary = new AlwaysSendDictionary <TKey, TValue>();

            while (!context.Peek(JsonToken.ObjectEnd))
            {
                KeyValuePair <TKey, TValue> keyValuePair = KVUnmarshaller.Unmarshall(context);
                dictionary.Add(keyValuePair.Key, keyValuePair.Value);
            }
            context.Read();
            return(dictionary);
        }
        public Dictionary <TKey, TValue> Unmarshall(JsonUnmarshallerContext context)
        {
            context.Read(); // Read { or null
            if (context.CurrentTokenType == JsonToken.Null)
            {
                return(new Dictionary <TKey, TValue>());
            }

            // If a dictionary is present in the response, use AlwaysSendDictionary,
            // so if the response was empty, reusing the object in the request we will
            // end up sending the same empty collection back.
            Dictionary <TKey, TValue> dictionary = new AlwaysSendDictionary <TKey, TValue>();

            while (!context.Peek(JsonToken.ObjectEnd)) // Peek }
            {
                KeyValuePair <TKey, TValue> item = KVUnmarshaller.Unmarshall(context);
                dictionary.Add(item.Key, item.Value);
            }
            context.Read(); // Read }
            return(dictionary);
        }
        public Dictionary <TKey, TValue> Unmarshall(XmlUnmarshallerContext context)
        {
            var originalDepth = context.CurrentDepth;
            var targetDepth   = originalDepth + 1;

            // If a dictionary is present in the response, use AlwaysSendDictionary,
            // so if the response was empty, reusing the object in the request we will
            // end up sending the same empty collection back.
            var dictionary = new AlwaysSendDictionary <TKey, TValue>();

            while (context.Read())
            {
                if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    break;
                }

                var item = KVUnmarshaller.Unmarshall(context);
                dictionary.Add(item.Key, item.Value);
            }

            return(dictionary);
        }