ByteDev.FormUrlEncoded
1.0.0
dotnet add package ByteDev.FormUrlEncoded --version 1.0.0
NuGet\Install-Package ByteDev.FormUrlEncoded -Version 1.0.0
<PackageReference Include="ByteDev.FormUrlEncoded" Version="1.0.0" />
paket add ByteDev.FormUrlEncoded --version 1.0.0
#r "nuget: ByteDev.FormUrlEncoded, 1.0.0"
// Install ByteDev.FormUrlEncoded as a Cake Addin
#addin nuget:?package=ByteDev.FormUrlEncoded&version=1.0.0
// Install ByteDev.FormUrlEncoded as a Cake Tool
#tool nuget:?package=ByteDev.FormUrlEncoded&version=1.0.0
ByteDev.FormUrlEncoded
.NET Standard library for serializing and deserializing form URL encoded (application/x-www-form-urlencoded
) data.
The form URL encoded format is a data format that encodes data as name-value pairs. It is most associated with web HTML forms and the visible web. However, it can also be used in other situations, for example as a form of data exchange by APIs. This library allows a consumer to serailize/deserialize data in the format without being tied to a web application environment such as ASP.NET.
Installation
ByteDev.FormUrlEncoded is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:
Install-Package ByteDev.FormUrlEncoded
Further details can be found on the nuget page.
Release Notes
Releases follow semantic versioning.
Full details of the release notes can be viewed on GitHub.
Usage
FormUrlEncodedSerializer
is the primary class in the library and allows the serialization and deserialization of form URL encoded content.
Serialization can be performed on any object. However, only the public instance properties of the object will have their values serialized.
It should be noted that when serialization of a object's property occurs the ToString
method will be called on the property.
The form URL encoded format is very flat so no serialization of inner types (e.g. a property's type's properties) or collection type's elements will occur.
The serializer supports:
- All built in .NET types:
- Reference types (object, string, etc.)
- Value types (integral and floating point numeric types, enum, bool, char, struct)
- Serialization specific settings via the
SerializeOptions
type parameter. - Deserialization specific settings via the
DeserializeOptions
type parameter. - Property attributes:
FormUrlEncodedPropertyNameAttribute
which overrides the property name to use during serialization/deserialization.FormUrlEncodedIgnoreAttribute
which prevents a property from being used during serialization/deserialization.
Code Examples
The follow demonstrates simple serialization and deserialization of a type including the use of the FormUrlEncodedPropertyName
and FormUrlEncodedIgnore
attributes.
// Entitiy class (class you want to serialize/deserialize)
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
[FormUrlEncodedPropertyName("emailAddress")]
public string Email { get; set; }
[FormUrlEncodedIgnore]
public int PayGrade { get; set; }
}
// Serialize an object to a form URL encoded string
var employee = new Employee
{
Name = "John Smith",
Age = 50,
Email = "john@somewhere.com",
PayGrade = 5
};
string data = FormUrlEncodedSerializer.Serialize(employee);
// data == "Name=John+Smith&Age=50&emailAddress=john%40somewhere.com"
// Deserialize a form URL encoded string to an object
string data = "Name=John+Smith&Age=50&emailAddress=john%40somewhere.com&PayGrade=5";
Employee employee = FormUrlEncodedSerializer.Deserialize<Employee>(data);
// employee.Name == "John Smith"
// employee.Age == 50
// employee.Email == "john@somewhere.com"
// employee.PayGrade == 0
Serialize Options
Serializing options are supported via the SerializeOptions
type parameter.
Options include:
Encode
- Indicates if all public property names and values should be URI encoded when serializing. True by default.EncodeSpaceAsPlus
- Indicates if spaces should be encoded as a plus when serializing. True by default. Property Encode must be true for this property to have any affect.IgnoreIfDefault
- Indicates if a property should be ignored if it is set to it's default value. False by default.IgnoreIfNull
- Indicates if a property should be ignored if it is set to null. True by default. If property IgnoreIfDefault is set to true this property is ignored.EnumHandling
- Indicates how enums should be handled during serialization. Default handling is by number.
Deserialize Options
Deserializing options are supported via the DeserializeOptions
type parameter.
Options include:
Decode
- Indicates if all pair names and values should be URI decoded when deserializing. True by default.DecodePlusAsSpace
- Indicates if the plus sign should be decoded as a space when deserializing. True by default. Property Decode must be true for this property to have any affect.EnumHandling
- Indicates how enums should be handled during deserialization. Default handling is by number.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- ByteDev.Reflection (>= 2.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,433 | 6/17/2021 |