Parse arbitrary strings from e.g. a configuration file or user input into the desired .NET type, with nullable/enum/.NET primitives support and easily extended (Library, C#/.NET Standard 1.1)

GitHub
github.com/Nerven/StringParser
NuGet
Install-Package Nerven.StringParser.Core (Nerven.StringParser.Core)
.NET platform
.NET Standard 1.1
Development
Maintenance
License
MIT License
Source (git)
git@github.com:Nerven/StringParser.git
https://github.com/Nerven/StringParser.git
Source (archive)
.tar.gz
.zip

Readme

Extensible C# string parser with decent defaults, .NET primitives support, enum support, nullable support.

using Nerven.StringParser.Core.Build;
var builder = new StringParserBuilder
{
PreSteps =
{
NullableParseStep.Default, // Adds Nullable<> suppoort
},
Steps =
{
EnumParseStep.Ordinal, // Parses enums
CustomParseStep.Define(typeof(ushort), _s => // Custom parsing
{
ushort _value;
return ushort.TryParse(_s, out _value)
? ParseStep.Valid((ushort)(_value + 1))
: ParseStep.InvalidString();
}),
},
PostSteps =
{
ConvertibleParseStep.Default, // Parses most .NET primitives and other types implementing IConvertible
},
};
var stringParser = builder.Build();
stringParser.Parse<bool?>("True"); // -> true
stringParser.Parse<UriKind>("RelativeOrAbsolute"); // -> UriKind.RelativeOrAbsolute
stringParser.Parse<ushort?>("0"); // -> 1

License

The MIT License (MIT)

Copyright © Victor Blomberg <victor.blomberg@nerven.se>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.