Skip to content

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)

License

Nerven/StringParser

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

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

About

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)

Resources

License

Stars

Watchers

Forks

Packages

No packages published