Ejemplo n.º 1
0
        /// <summary>
        /// Given an object, attempt to serialize it into a form suitable for URL Encoding
        /// </summary>
        /// <remarks>Currently only supports objects which implement IDictionary</remarks>
        /// <param name="body">Object to attempt to serialize</param>
        /// <returns>Key/value collection suitable for URL encoding</returns>
        protected virtual IEnumerable <KeyValuePair <string, string> > SerializeBodyForUrlEncoding(object body)
        {
            if (body == null)
            {
                return(Enumerable.Empty <KeyValuePair <string, string> >());
            }

            if (DictionaryIterator.CanIterate(body.GetType()))
            {
                return(this.TransformDictionaryToCollectionOfKeysAndValues(body));
            }
            else
            {
                throw new ArgumentException("BodySerializationMethod is UrlEncoded, but body does not implement IDictionary or IDictionary<TKey, TValue>", nameof(body));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Takes an IDictionary or IDictionary{TKey, TValue}, and emits KeyValuePairs for each key
 /// Takes account of IEnumerable values, null values, etc
 /// </summary>
 /// <param name="dictionary">Dictionary to transform</param>
 /// <returns>A set of KeyValuePairs</returns>
 protected virtual IEnumerable <KeyValuePair <string, string> > TransformDictionaryToCollectionOfKeysAndValues(object dictionary)
 {
     foreach (var kvp in DictionaryIterator.Iterate(dictionary))
     {
         if (kvp.Value != null && !(kvp.Value is string) && kvp.Value is IEnumerable)
         {
             foreach (var individualValue in (IEnumerable)kvp.Value)
             {
                 var stringValue = this.ToStringHelper(individualValue);
                 yield return(new KeyValuePair <string, string>(this.ToStringHelper(kvp.Key), stringValue));
             }
         }
         else if (kvp.Value != null)
         {
             yield return(new KeyValuePair <string, string>(this.ToStringHelper(kvp.Key), this.ToStringHelper(kvp.Value)));
         }
     }
 }