Download from NuGet.
dotnet add package Tizzani.QueryStringSerializer --version 8.0.0
var order = new Order
{
Customer = new Customer { Name = "Jack Sparrow" },
Items = new List<OrderItem>
{
new OrderItem { Description = "Rum", Quantity = 5 },
new OrderItem { Description = "Jar of Dirt", Quantity = 1 }
}
};
var queryString = QueryStringSerializer.Serialize(order);
// "Customer.Name=Jack+Sparrow&Items.Description=Rum&Items.Quantity=5&Items.Description=Jar+of+Dirt&Items.Quantity=1";
var queryString = "Customer.Name=Jack+Sparrow&Items.Description=Rum&Items.Quantity=5&Items.Description=Jar+of+Dirt&Items.Quantity=1";
var order = QueryStringSerializer.Deserialize<Order>(queryString);
To configure how query strings are serialized, use QueryStringSerializerOptions
.
enum Status { Placed, Canceled, Completed }
var order = new Order { Status = Status.Canceled };
var qs1 = QueryStringSerializer.Serialize(order); // "Status=Canceled"
var qs2 = QueryStringSerializer.Serialize(order, new() { EnumsAsStrings = false }); // "Status=1"