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)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
source
tests/Nerven.StringParser.Tests.Core
.gitignore
GitVersionConfig.yaml
LICENSE.txt
Nerven.DotSettings
Nerven.StringParser.sln
Nerven.StringParser.sln.DotSettings
README.md
Settings.StyleCop
appveyor.yml
build.cmd
build.complete.cmd
build.proj
build.test.cmd
pre-build.cmd

README.md

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