diff --git a/src/SendGrid/SendGridClient.cs b/src/SendGrid/SendGridClient.cs index b5c5a7fd9..ecb07fdcc 100644 --- a/src/SendGrid/SendGridClient.cs +++ b/src/SendGrid/SendGridClient.cs @@ -17,6 +17,7 @@ namespace SendGrid using System.Threading; using System.Threading.Tasks; using SendGrid.Helpers.Reliability; + using System.IO; /// /// A HTTP client wrapper for interacting with SendGrid's API @@ -331,16 +332,19 @@ private string BuildUrl(string urlPath, string queryParams = null) if (queryParams != null) { - var ds_query_params = JsonConvert.DeserializeObject>(queryParams); + var ds_query_params = ParseJson(queryParams); string query = "?"; foreach (var pair in ds_query_params) { - if (query != "?") + foreach(var element in pair.Value) { - query = query + "&"; - } + if (query != "?") + { + query = query + "&"; + } - query = query + pair.Key + "=" + pair.Value.ToString(); + query = query + pair.Key + "=" + element; + } } url = url + query; @@ -348,5 +352,51 @@ private string BuildUrl(string urlPath, string queryParams = null) return url; } + + /// + /// Parses a JSON string without removing duplicate keys. + /// + /// + /// This function flattens all Objects/Array. + /// This means that for example {'id': 1, 'id': 2, 'id': 3} and + /// {'id': [1, 2, 3]} result in the same output. + /// + /// The JSON string to parse. + /// A dictionary of all values. + private Dictionary> ParseJson(string json) + { + var dict = new Dictionary>(); + + using(var sr = new StringReader(json)) + using (var reader = new JsonTextReader(sr)) + { + var propertyName = ""; + while (reader.Read()) + { + switch (reader.TokenType) + { + case JsonToken.PropertyName: + { + propertyName = reader.Value.ToString(); + if(!dict.ContainsKey(propertyName)) + dict.Add(propertyName, new List()); + break; + } + case JsonToken.Boolean: + case JsonToken.Integer: + case JsonToken.Float: + case JsonToken.Bytes: + case JsonToken.String: + case JsonToken.Date: + { + dict[propertyName].Add(reader.Value); + break; + } + } + } + } + + return dict; + } } }