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)

master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

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

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

Packages

No packages published