A lightweight library for serializing and deserializing key-value pairs as strings.
All data is stored internally as strings, and parsing to the requested type T happens on retrieval.
NOTE: Designed to be stable and low-maintenance. Unit tests guarantee safe and predictable usage.
You can install the package via NuGet:
dotnet add package StringQueryMapOr via the Package Manager Console:
Install-Package StringQueryMap- Serialization: All key-value pairs are converted to string using
ToString()when added. - Deserialization: Values are parsed to the requested type
Ton retrieval. - Generic support: Works with any type
Tthat either has a built-in parser (21 types supported) or provides a public staticParse(string)method. - Custom delimiters and joiners: Flexible string syntax.
- Safe access:
ContainsKey,TryGet, andGetfor safe and exact retrieval.
var sqm = new SQM("=", ";");
sqm.Add("one", 1);
sqm.Add("pi", 3.14);
sqm.Add("name", "Alice");
string serialized = sqm.ToString(); // "one=1;pi=3.14;name=Alice"All values are stored internally as strings via
ToString().
SQM provides two parsing approaches:
- Exact Parsing –
Get<T>: throws exceptions if parsing fails or key not found. - Safe Parsing –
TryGet<T>: returns a boolean indicating success.
var data = "a=1;b=2;c=true";
var sqm = SQM.Parse(data, "=", ";");
// Exact parse
int a = sqm.Get<int>("a"); // 1
int b = sqm.Get<int>("b"); // 2
bool c = sqm.Get<bool>("c"); // true
// Safe parse
bool foundX = sqm.TryGet<int>("x", out int xValue); // falseCustom types must provide a public static Parse(string) method:
var customData = "item1=42;item2=100";
var customMapper = SQM.Parse(customData, "=", ";");
int value1 = customMapper.Get<CustomWithParse>("item1").Value;
int value2 = customMapper.Get<CustomWithParse>("item2").Value;bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, short, ushort,
string, Guid, DateTime, DateTimeOffset, TimeSpan, Version, Uri, IPAddress, BigInteger, CultureInfo,
and enums (any enum type)
These 21 types plus enums are parsed directly without reflection. All other types rely on reflection to locate a
public static Parse(string)method.
SQM
SQM()– creates an empty mapper with defaultJoiner = "="andDelimiter = ";"SQM(string joiner, string delimiter)– creates an empty mapperSQM(string data, string joiner, string delimiter)– parses from stringstring Joiner { get; }– joiner stringstring Delimiter { get; }– delimiter stringIEnumerable<string> AllKeys { get; }IEnumerable<object> AllValues { get; }– all values as stringsbool Add<T>(string key, T value)– adds or updates a key-value pair (value.ToString()internally)int AddRange<T>(IEnumerable<KeyValuePair<string, T>> pairs)– add multiple pairsbool Remove(string key)– removes a keyint RemoveRange(IEnumerable<string> keys)– remove multiple keysvoid Clear()– clear all pairsbool ContainsKey(string key)– check if key existsbool TryGet<T>(string key, out T value)– safe retrievalT Get<T>(string key)– exact retrieval (throws if key missing or parse fails)override string ToString()– serializes all key-value pairs to stringstatic SQM Parse(string data, string joiner, string delimiter)– parse string into SQMstatic bool TryParse(string data, string joiner, string delimiter, out SQM result)– safe parse
Serialization converts all values to string via
ToString(). Deserialization converts string values back to typeTusing either built-in parsing or the public staticParse(string)method.
- Created for KeZero, available to the community.
- Licensed under the MIT License.
- Creator: Alecio Furanze (https://github.com/afuranze)
- Repository: kezerocom/StringQueryMap (https://github.com/kezerocom/StringQueryMap)