JDeck 1.0.0-beta-004

This is a prerelease version of JDeck.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package JDeck --version 1.0.0-beta-004                
NuGet\Install-Package JDeck -Version 1.0.0-beta-004                
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="JDeck" Version="1.0.0-beta-004" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JDeck --version 1.0.0-beta-004                
#r "nuget: JDeck, 1.0.0-beta-004"                
#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 JDeck as a Cake Addin
#addin nuget:?package=JDeck&version=1.0.0-beta-004&prerelease

// Install JDeck as a Cake Tool
#tool nuget:?package=JDeck&version=1.0.0-beta-004&prerelease                

JDeck a System.Text.Json wrapper

JDeck is a Thoth.Json-like Json decoder based on System.Text.Json in a single file with no external dependencies. Plays well with other libraries that use System.Text.Json like FSharp.SystemTextJson

Note: While JDeck has no dependencies to start working right away, it is recommended to use FsToolkit.ErrorHandling

Usage

For most F# types, you can use the Decode.auto function to decode JSON as shown below:

#r "nuget: JDeck, 1.0.0-beta-*"

open JDeck

type Person = {
  name: string
  age: int
  emails: string list
}
let json = """{"name": "Alice", "age": 30, "emails": ["alice@name.com", "alice@age.com"] }"""

let result: Result<Person, DecodeError> = Decoding.auto(json)

match result with
| Ok person -> printfn $"Person: %A{person}"
| Error err -> printfn $"Error: %A{err}"

In cases where the data is inconclusive, you deserialize Discriminated Unions or does not play well with F# immutability, you can create a manual decoder.

#r "nuget: JDeck, 1.0.0-beta-*"

open System.Text.Json
open JDeck

type Person = {
  Name: string
  Age: int
  Emails: string list
}
type ServerResponse = { Data: Person; Message: string }

let personDecoder: Decoder<Person> = fun  person -> decode {
  let! name = person |> Required.Property.get("name", Optional.string)
  and! age = person |> Required.Property.get("age", Required.int)
  and! emails = person |> Required.Property.list("emails", Optional.string)
  return {
    Name = name |> Option.defaultValue "<missing name>"
    Age = age
    // Remove any optional value from the list
    Emails = emails |> List.choose id
  }
}
// Inconclusive data coming from the server
let person = """{"name": null, "age": 30, "emails": ["alice@name.com", "alice@age.com", null] }"""

let result: Result<ServerResponse, DecodeError> =
  // ServerResponse will decode automatically while Person will use the custom decoder
  Decoding.auto(
    $$"""{ "data": {{person}}, "message": "Success" }""",
    // Include your own decoder
    JsonSerializerOptions(PropertyNameCaseInsensitive = true) |> Codec.useDecoder personDecoder
  )

match result with
| Ok person -> printfn $"Person: %A{person}"
| Error err -> printfn $"Error: %A{err}"

Acknowledgements

Nothing is done in the void, in this case I'd like to thank the following libraries for their inspiration and ideas:

  • Thoth.Json for the inspiration and a cross-runtime solution to JSON decoding, compatible with F#, JS, and Python.
  • FsToolkit.ErrorHandling for the general mechanism for dealing with Result types and Computation expressions
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 is compatible.  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 is compatible. 
.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
1.0.0-beta-006 55 11/22/2024
1.0.0-beta-005 52 11/22/2024
1.0.0-beta-004 58 11/21/2024
1.0.0-beta-003 51 11/20/2024
1.0.0-beta-002 49 11/19/2024
1.0.0-beta-001 47 11/19/2024