Alve.Json 0.3.0

dotnet add package Alve.Json --version 0.3.0
NuGet\Install-Package Alve.Json -Version 0.3.0
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="Alve.Json" Version="0.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Alve.Json --version 0.3.0
#r "nuget: Alve.Json, 0.3.0"
#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.
// Install Alve.Json as a Cake Addin
#addin nuget:?package=Alve.Json&version=0.3.0

// Install Alve.Json as a Cake Tool
#tool nuget:?package=Alve.Json&version=0.3.0

Alve.Json

Nuget

A simple F# library for decoding and encoding JSON values, inspired by Elm's Json.Decode. It is based on the System.Text.Json APIs.

Installation

Via NuGet

Usage

Assume we have this JSON (adapted from https://json.org/example.html):

{
  "menu": {
    "header": "SVG Viewer",
    "items": [
      { "id": "Open" },
      {
        "id": "OpenNew",
        "label": "Open New"
      },
      null,
      {
        "id": "ZoomIn",
        "label": "Zoom In"
      },
      {
        "id": "ZoomOut",
        "label": "Zoom Out"
      },
      {
        "id": "OriginalView",
        "label": "Original View"
      },
      null,
      { "id": "Quality" },
      { "id": "Pause" },
      { "id": "Mute" },
      null,
      { "id": "Help" },
      {
        "id": "About",
        "label": "About Adobe CVG Viewer..."
      }
    ]
  }
}

And these F# types:

type Item = {
    id: string
    label: string option
}

type MenuItem =
    | Item of Item
    | Separator

type Menu = {
    header: string
    items: MenuItem list
}

type Config = {
    menu: Menu
}

Then we can write datatype decoders in the following way:

open Alve.Json.Decode

let itemDecoder = map2 (fun id label -> { id = id; label = label }) (field "id" jstring) (field "label" jstring |> optional)
let menuItemDecoder = map1 (fun item -> match item with
                                            | None -> Separator
                                            | Some item -> Item item) (nullable itemDecoder)
let menuItemListDecoder = jlist menuItemDecoder
let menuDecoder = map2 (fun header items -> { header = header; items = items }) (field "header" jstring) (field"items" menuItemListDecoder)
let configDecoder = map1 (fun menu -> { menu = menu }) (field "menu" menuDecoder)

//Now, decode our JSON:

printfn "%A" (decodeString configDecoder json)

Alternatively, we can use the jsonDecode computation expression to simplify the mapping operation:

open Alve.Json.Decode

let itemDecoder = jsonDecode {
    let! id = field "id" jstring
    let! label = optional (field "label" jstring)
    return {
        id = id
        label = label
    }
}
let menuItemDecoder = jsonDecode {
    let! item = nullable itemDecoder
    return match item with
            | None -> Separator
            | Some item -> Item item
}
let menuDecoder = jsonDecode {
    let itemsDecoder = jlist menuItemDecoder
    let! items = (field "items" itemsDecoder)
    let! header = (field "header" jstring)
    return {
        items = items
        header = header
    }
}
let configDecoder = jsonDecode {
    let! menu = field "menu" menuDecoder
    return {
        menu = menu
    }
}

//Now, decode our JSON:

printfn "%A" (decodeString configDecoder json)

Performance

Alve.Json requires the entire JSON to be present in memory for decoding. Therefore, it is unsuited for processing enormous amounts of data. Also, the functional programming style (bind, apply etc.) is not very good for performance and may generate some garbage, so do not use this in performance-sensitive applications.

License

Alve.Json is licensed under the MIT License.

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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.3.0 586 1/20/2020
0.2.0 465 12/1/2019
0.1.0 492 10/9/2019