QsNet
A query string encoding and decoding library for C#/.NET.
Ported from qs for JavaScript.
Highlights
- Nested dictionaries and lists:
foo[bar][baz]=qux
⇄{ "foo": { "bar": { "baz": "qux" } } }
- Multiple list formats (indices, brackets, repeat, comma)
- Dot-notation support (
a.b=c
) and"."
-encoding toggles - UTF-8 and Latin1 charsets, plus optional charset sentinel (
utf8=✓
) - Custom encoders/decoders, key sorting, filtering, and strict null handling
- Supports
DateTime
serialization via a pluggable serializer - Extensive tests (xUnit + FluentAssertions), performance-minded implementation
Usage
using QsNet;
// Decode
var obj = Qs.Decode("foo[bar]=baz&foo[list][]=a&foo[list][]=b");
// -> { "foo": { "bar": "baz", "list": ["a", "b"] } }
// Encode
var qs = Qs.Encode(new Dictionary<string, object?>
{
["foo"] = new Dictionary<string, object?> { ["bar"] = "baz" }
});
// -> "foo%5Bbar%5D=baz"
Design notes
- Performance: The implementation mirrors qs semantics but is optimized for C#/.NET. Deep parsing, list compaction, and cycle-safe compaction are implemented iteratively where it matters.
- Safety: Defaults (depth, parameterLimit) help mitigate abuse in user-supplied inputs; you can loosen them when you fully trust the source.
- Interop: Exposes knobs similar to qs (filters, sorters, custom encoders/decoders) to make migrations straightforward.
Special thanks to the authors of qs for JavaScript:
License
BSD 3-Clause © techouse