Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Add auto encoder/decoder #30

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Uniform APIs
  • Loading branch information
alfonsogarciacaro authored and Maxime Mangel committed Jun 7, 2018
commit de996532aef5e46928b85ed92431635cb05c442f
8 changes: 8 additions & 0 deletions src/Thoth.Json.Net/Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,11 @@ let optional key valDecoder fallback decoder =

let optionalAt path valDecoder fallback decoder =
custom (optionalDecoder (at path value) valDecoder fallback) decoder

type Auto =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfonsogarciacaro Can you please explain me what this type do ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It"s necessary to have a type in order to use static members for optional arguments (in Thoth.Json, ITypeResolver). Here I"m just using it to have the same API surface as Thoth.Json, but I guess a module should also work (we don"t need the injectable optional argument on the .NET side).

static member Generate<'T> (): Decoder<'T> =
let serializer = JsonSerializer()
for conv in Converters.converters do
serializer.Converters.Add(conv)
fun token ->
token.ToObject<'T>(serializer) |> Ok
4 changes: 0 additions & 4 deletions tests/Tests.Json.Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,8 @@ Expecting an object with a field named `version` but instead got:
e = Map [("oh", { a = 2.; b = 2. }); ("ah", { a = -1.5; b = 0. })]
} |> Encode.encodeAuto 4
// printfn "AUTO ENCODED %s" json
#if FABLE_COMPILER
let decoder = Auto.Generate<Record9>()
let result = decodeString decoder json
#else
let result = decodeStringAuto<Record9> json
#endif
match result with
| Error er -> failwith er
| Ok r2 ->
Expand Down