Farse 0.1.2-alpha

This is a prerelease version of Farse.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Farse --version 0.1.2-alpha
                    
NuGet\Install-Package Farse -Version 0.1.2-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Farse" Version="0.1.2-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Farse" Version="0.1.2-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Farse" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Farse --version 0.1.2-alpha
                    
#r "nuget: Farse, 0.1.2-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Farse@0.1.2-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Farse&version=0.1.2-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Farse&version=0.1.2-alpha&prerelease
                    
Install as a Cake Tool

Build NuGet Version

Farse

Simple parsing library for F# using System.Text.Json.

Heavily inspired by Thoth.Json.Net and its composability. Farse uses a slightly different syntax, includes a computational expression and a few custom operators that simplify parsing. It also tries to keep a low overhead while still providing utility and acceptable error messages.

Installation

Things are still changing but a pre-release version is available.

dotnet add package Farse --prerelease

Benchmarks

There are some initial benchmarks here.

BenchmarkDotNet v0.14.0, macOS Sequoia 15.4.1 (24E263) [Darwin 24.4.0]  
Apple M1 Pro, 1 CPU, 8 logical and 8 physical cores  
.NET SDK 9.0.203  
  [Host]     : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD DEBUG
  DefaultJob : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
| Method                    | Mean      | Ratio | Gen0   | Gen1   | Allocated | Alloc Ratio |
|-------------------------- |----------:|------:|-------:|-------:|----------:|------------:|
| 'System.Text.Json (Auto)' |  3.646 us |  0.72 | 0.4082 | 0.0076 |    2562 B |        0.64 |
| System.Text.Json          |  3.667 us |  0.72 | 0.1106 |      - |     696 B |        0.17 |
| Farse                     |  5.089 us |  1.00 | 0.6332 |      - |    4016 B |        1.00 |
| 'Newtonsoft.Json (Auto)'  |  6.301 us |  1.24 | 1.5182 | 0.0229 |    9544 B |        2.38 |
| Thoth.System.Text.Json    |  8.176 us |  1.61 | 1.5869 | 0.0305 |    9944 B |        2.48 |
| Newtonsoft.Json           |  8.522 us |  1.67 | 2.8229 | 0.1526 |   17720 B |        4.41 |
| Thoth.Json.Net            | 10.036 us |  1.97 | 3.3569 | 0.1526 |   21136 B |        5.26 |

Example

Given the following JSON string.

{
    "id": "c8eae96a-025d-4bc9-88f8-f204e95f2883",
    "name": "Alice",
    "age": null,
    "email": "alice@domain.com",
    "groups": [
        "01458283-b6e3-4ae7-ae54-a68eb587cdc0",
        "bf00d1e2-ee53-4969-9507-86bed7e96432",
        "927eb20f-cd62-470c-aafc-c3ce6b9248b0"
    ],
    "subscription": {
        "plan": "Pro",
        "isCanceled": true,
        "renewsAt": null
    }
}

And the following (optional) operators.

/// <summary>
/// Parses a required field.
/// </summary>
let (&=) = Parse.req

/// <summary>
/// Parses an optional field.
/// </summary>
let (?=) = Parse.opt

We can parse the JSON string into the following types.

open Farse
open Farse.Operators

type UserId = UserId of Guid

module UserId =

    let asString (UserId x) =
        string x

    let parser =
        Parse.guid
        |> Parser.map UserId

type Email = Email of string

module Email =

    let asString (Email x) = x

    let fromString str =
        // Some validation
        Ok (Email str)

    let parser =
        Parse.string
        |> Parser.validate fromString

type GroupId = GroupId of Guid

module GroupId =

    let asString (GroupId x) =
        string x

    let parser =
        Parse.guid
        |> Parser.map GroupId

type Plan =
    | Pro
    | Standard
    | Free

module Plan =

    let fromString = function
        | "Pro" -> Ok Pro
        | "Standard" -> Ok Standard
        | "Free" -> Ok Free
        | invalid -> Error $"Invalid plan: %s{invalid}."

    let asString = function
        | Pro -> "Pro"
        | Standard -> "Standard"
        | Free -> "Free"

    let parser =
        Parse.string
        |> Parser.validate fromString

type Subscription = {
    Plan: Plan
    IsCanceled: bool
    RenewsAt: DateTime option
}

type User = {
    Id: UserId
    Name: string
    Age: int option
    Email: Email
    Groups: GroupId list
    Subscription: Subscription
}

module User =
    open Parse

    let parser =
        parser {
            let! id = "id" &= UserId.parser
            let! name = "name" &= string
            let! age = "age" ?= int
            let! email = "email" &= Email.parser
            let! groups = "groups" &= list GroupId.parser

            let! subscription = "subscription" &= parser {
                let! plan = "plan" &= Plan.parser
                let! isCanceled = "isCanceled" &= bool
                let! renewsAt = "renewsAt" ?= dateTime
    
                return {
                    Plan = plan
                    IsCanceled = isCanceled
                    RenewsAt = renewsAt
                }
            }
      
            return {
                Id = id
                Name = name
                Age = age
                Email = email
                Groups = groups
                Subscription = subscription
            }
        }

Then we can just run the parser.

let user =
    User.parser
    |> Parser.parse json
    |> Result.defaultWith failwith

printfn "%s" user.Name

There is a few different ways to parse fields from an object directly.

Combining:

parser {
    let! plan = "subscription" &= ("plan" &= Plan.parser)
    let! isCanceled = "subscription" &= ("isCanceled" &= bool)
    let! renewsAt = "subscription" &= ("renewsAt" ?= dateTime)

    return {
        Plan = plan
        IsCanceled = isCanceled
        RenewsAt = renewsAt
    }
}

Partial application:

parser {
    let fromSub = req "subscription"

    let! plan = "plan" &= Plan.parser |> fromSub
    let! isCanceled = "isCanceled" &= bool |> fromSub
    let! renewsAt = "renewsAt" ?= dateTime |> fromSub

    return {
        Plan = plan
        IsCanceled = isCanceled
        RenewsAt = renewsAt
    }
}

Get object directly:

req "subscription" Subscription.Parser
|> Parser.parse json

We can also convert types back into a JSON string.

let jsonString =
    JsonString.create [
        "id", JStr (UserId.asString user.Id)
        "name", JStr user.Name
        "age",
            user.Age
            |> Option.map (Int >> JNum)
            |> JOpt
        "email", JStr (Email.asString user.Email)
        "groups",
            user.Groups
            |> List.map (GroupId.asString >> JStr)
            |> JArr
        "subscription",
            JObj [
                "plan", JStr (Plan.asString user.Subscription.Plan)
                "isCanceled", JBit user.Subscription.IsCanceled
                "renewsAt",
                    user.Subscription.RenewsAt
                    |> Option.map (DateTime.asString >> JStr)
                    |> JOpt
            ]
    ]

let json =
    jsonString
    |> JsonString.asString

printfn "%s" json
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.0 150 3/28/2026
0.8.0 122 2/26/2026
0.7.3 108 1/25/2026
0.7.2 101 1/17/2026
0.7.1 118 1/1/2026
0.7.0 192 12/22/2025
0.6.0 217 11/9/2025
0.5.2 180 10/5/2025
0.5.1 194 9/25/2025
0.5.0 235 9/21/2025
0.4.0 173 9/13/2025
0.3.0 242 8/27/2025
0.2.7 117 8/23/2025
0.2.6 184 8/19/2025
0.2.5 157 8/17/2025
0.2.4 193 8/13/2025
0.2.3 188 8/8/2025
0.2.2 271 8/5/2025
0.2.1 234 8/4/2025
0.1.2-alpha 209 5/16/2025
Loading failed