Skip to content

Commit

Permalink
docs: update examples for ones that actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelMunoz committed Nov 20, 2024
1 parent fb3be11 commit 2b15073
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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
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
Expand All @@ -16,9 +16,9 @@ For most F# types, you can use the `Decode.auto` function to decode JSON as show
open JDeck
type Person = {
Name: string
Age: int
Emails: string list
name: string
age: int
emails: string list
}
let json = """{"name": "Alice", "age": 30, "emails": ["alice@name.com", "alice@age.com"] }"""
Expand All @@ -44,18 +44,17 @@ type Person = {
}
type ServerResponse = { Data: Person; Message: string }
module Person =
let Decoder person = decode {
let! name = person |> Required.Property.get("name", Optional.string)
and! age = person |> Required.Property.get("name", 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
}
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] }"""
Expand All @@ -64,7 +63,7 @@ let result: Result<ServerResponse, DecodeError> =
Decoding.auto(
$$"""{ "data": {{person}}, "message": "Success" }""",
// Include your own decoder
JsonSerializerOptions() |> Decode.useDecoder Person.Decoder
JsonSerializerOptions(PropertyNameCaseInsensitive = true) |> Decode.useDecoder personDecoder
)
match result with
Expand Down
29 changes: 14 additions & 15 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ For most F# types, you can use the `Decode.auto` function to decode JSON as show
open JDeck
type Person = {
Name: string
Age: int
Emails: string list
name: string
age: int
emails: string list
}
let json = """{"name": "Alice", "age": 30, "emails": ["alice@name.com", "alice@age.com"] }"""
Expand All @@ -44,18 +44,17 @@ type Person = {
}
type ServerResponse = { Data: Person; Message: string }
module Person =
let Decoder person = decode {
let! name = person |> Required.Property.get("name", Optional.string)
and! age = person |> Required.Property.get("name", 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
}
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] }"""
Expand All @@ -64,7 +63,7 @@ let result: Result<ServerResponse, DecodeError> =
Decoding.auto(
$$"""{ "data": {{person}}, "message": "Success" }""",
// Include your own decoder
JsonSerializerOptions() |> Decode.useDecoder Person.Decoder
JsonSerializerOptions(PropertyNameCaseInsensitive = true) |> Decode.useDecoder personDecoder
)
match result with
Expand Down

0 comments on commit 2b15073

Please sign in to comment.