using System.Collections.Generic; using Utf8Json; namespace RestEase { /// /// Default IRequestParamSerializer, using Json.NET /// public class JsonRequestQueryParamSerializer : RequestQueryParamSerializer { /// /// Serialize a query parameter whose value is scalar (not a collection), into a collection of name -> value pairs /// /// /// Most of the time, you will only return a single KeyValuePair from this method. However, you are given the flexibility, /// to return multiple KeyValuePairs if you wish. Duplicate keys are allowed: they will be serialized as separate query parameters. /// /// Type of the value to serialize /// Name of the query parameter /// Value of the query parameter /// Extra information which may be useful /// A colletion of name -> value pairs to use as query parameters public override IEnumerable> SerializeQueryParam(string name, T value, RequestQueryParamSerializerInfo info) { if (value == null) yield break; yield return new KeyValuePair(name, JsonSerializer.ToJsonString(value)); } /// /// Serialize a query parameter whose value is a collection, into a collection of name -> value pairs /// /// /// Most of the time, you will return a single KeyValuePair for each value in the collection, and all will have /// the same key. However this is not required: you can return whatever you want. /// /// Type of the value to serialize /// Name of the query parameter /// Values of the query parmaeter /// Extra information which may be useful /// A colletion of name -> value pairs to use as query parameters public override IEnumerable> SerializeQueryCollectionParam(string name, IEnumerable values, RequestQueryParamSerializerInfo info) { if (values == null) yield break; foreach (var value in values) { if (value != null) yield return new KeyValuePair(name, JsonSerializer.ToJsonString(value)); } } } }