BusHero / Pevac

A parser combinator library around the System.Text.Json.Utf8JsonReader class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pevac Some other name that doesn't matter

Pevac is a simple, lightweight library for constructing functional, forward only parsers that use Utf8JsonReader struct as the source of tokens. Parsers can then be used to simplify constuction of custom deserializers for the System.Text.Json.

It is heavely inspired by the Sprache parser combinator library!

Motivation

The scope of the project is to simplify building cusom deserializers for the System.Text.Json by providing the posibility of building functional parsers that can consume an instance of Utf8JsonReader struct.

It is impossible to use the Utf8JsonReader with any existing parser combinator library because of the limitation imposed on the struct.

  • Utf8JsonReader cannot be specified as a generic parameter.
  • All the interaction with the Utf8JsonReader struct should be through a reference.

Usage

Unlike most parser-building frameworks, you use Pevac directly from your program code, and don't need to set up any build-time code generation tasks. Pevac itself is a single tiny assembly.

Pevac provides a number of build-in functions that can make bigger parsers from smaller once, often callable via Linq query comprehensions:

Parser<Foo> parser = from _ in Parser.StartObject
                     from propertyName in Parser.PropertyName
                     from value in Parser.String
                     from __ in Parser.EndObject
                     select new Foo(propertyName, value)

var json = "{ \"foo\" : \"bar\" }";
var bytes = Encoding.UTF8.GetBytes(foo);
var reader = new Utf8JsonReader(bytes);
var foo = parser.Parse(reader, default);

Assert.AreEqual(new Foo("foo", "bar"), foo);

Credits

About

A parser combinator library around the System.Text.Json.Utf8JsonReader class

License:MIT License


Languages

Language:C# 100.0%