/// <summary> /// Returns a list of parameters for a given options class. If a key prefix is provided, the /// keys for the new parameters will be nested under the key prefix. E.g. if the key prefix /// `foo` is passed and the options class contains a parameter `bar`, then a parameter /// with key `foo[bar]` will be returned. /// </summary> /// <param name="options">The options class for which to create the list of parameters.</param> /// <param name="keyPrefix">The key under which new keys should be nested, if any.</param> /// <returns>The list of parameters</returns> private static List <Parameter> FlattenParamsOptions(INestedOptions options, string keyPrefix) { List <Parameter> flatParams = new List <Parameter>(); if (options == null) { return(flatParams); } foreach (var property in options.GetType().GetRuntimeProperties()) { var value = property.GetValue(options, null); // Fields on a class which are never set by the user will contain null values (for // reference types), so skip those to avoid encoding them in the request. if (value == null) { continue; } var attribute = property.GetCustomAttribute <JsonPropertyAttribute>(); if (attribute == null) { continue; } string key = attribute.PropertyName; string newPrefix = NewPrefix(key, keyPrefix); flatParams.AddRange(FlattenParamsValue(value, newPrefix)); } return(flatParams); }
/// <summary> /// Creates the HTTP query string for a given options class. /// </summary> /// <param name="requestString">The string to which the query string will be appended.</param> /// <param name="options">The options class for which to create the query string.</param> public static void CreateQuery(ref string requestString, INestedOptions options) { List <Parameter> flatParams = FlattenParams(options); foreach (Parameter param in flatParams) { RequestStringBuilder.ApplyParameterToRequestString(ref requestString, param.Key, param.Value); } }
public static void CreateQuery(ref string requestString, INestedOptions options) { List <Parameter> list = FlattenParams(options); foreach (Parameter item in list) { RequestStringBuilder.ApplyParameterToRequestString(ref requestString, item.Key, item.Value); } }
/// <summary> /// Returns a list of parameters for a given options class. If a key prefix is provided, the /// keys for the new parameters will be nested under the key prefix. E.g. if the key prefix /// `foo` is passed and the options class contains a parameter `bar`, then a parameter /// with key `foo[bar]` will be returned. /// </summary> /// <param name="options">The options class for which to create the list of parameters.</param> /// <param name="keyPrefix">The key under which new keys should be nested, if any.</param> /// <returns>The list of parameters</returns> private static List <KeyValuePair <string, object> > FlattenParamsOptions( INestedOptions options, string keyPrefix) { List <KeyValuePair <string, object> > flatParams = new List <KeyValuePair <string, object> >(); if (options == null) { return(flatParams); } foreach (var property in options.GetType().GetRuntimeProperties()) { // `[JsonExtensionData]` tells the serializer to write the values contained in // the collection as if they were class properties. var extensionAttribute = property.GetCustomAttribute <JsonExtensionDataAttribute>(); if (extensionAttribute != null) { var extensionValue = property.GetValue(options, null) as IDictionary; flatParams.AddRange(FlattenParamsDictionary(extensionValue, keyPrefix)); continue; } // Skip properties not annotated with `[JsonProperty]` var attribute = property.GetCustomAttribute <JsonPropertyAttribute>(); if (attribute == null) { continue; } var value = property.GetValue(options, null); // Fields on a class which are never set by the user will contain null values (for // reference types), so skip those to avoid encoding them in the request. if (value == null) { continue; } string key = attribute.PropertyName; string newPrefix = NewPrefix(key, keyPrefix); flatParams.AddRange(FlattenParamsValue(value, newPrefix)); } return(flatParams); }
/// <summary> /// Creates the HTTP query string for a given options class. /// </summary> /// <param name="requestString">The string to which the query string will be appended.</param> /// <param name="options">The options class for which to create the query string.</param> public static void CreateQuery(ref string requestString, INestedOptions options) { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; string jsonString = JsonConvert.SerializeObject(options, settings); var jobj = JObject.Parse(jsonString); if (jobj.Properties().Any(x => x.Name.Contains("[") && x.Name.Contains("]"))) //filter[] specific parsing for the querystring { jsonString = BuildRequestStringFromJObject(jobj); } jsonString = jsonString == "{}" ? string.Empty : "?" + jsonString; requestString = requestString + jsonString; }
private static List <Parameter> FlattenParamsOptions(INestedOptions options, string keyPrefix) { List <Parameter> list = new List <Parameter>(); if (options == null) { return(list); } foreach (PropertyInfo runtimeProperty in options.GetType().GetRuntimeProperties()) { object value = runtimeProperty.GetValue(options, null); if (value != null) { JsonPropertyAttribute customAttribute = runtimeProperty.GetCustomAttribute <JsonPropertyAttribute>(); if (customAttribute != null) { string propertyName = customAttribute.PropertyName; string keyPrefix2 = NewPrefix(propertyName, keyPrefix); list.AddRange(FlattenParamsValue(value, keyPrefix2)); } } } return(list); }
/// <summary> /// Returns a list of parameters for a given options class. If the class contains /// containers (e.g. dictionaries, lists, arrays or other options classes), the parameters /// will be computed recursively and a flat list will be returned. /// </summary> /// <param name="options">The options class for which to create the list of parameters.</param> /// <returns>The list of parameters</returns> private static List <Parameter> FlattenParams(INestedOptions options) { return(FlattenParamsOptions(options, null)); }