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
Automatically cache Newtonsoft.json converters
  • Loading branch information
alfonsogarciacaro authored and Maxime Mangel committed Jun 7, 2018
commit f53ca7a37e11b9b129e969a3883a210ca95eb92c
7 changes: 3 additions & 4 deletions src/Thoth.Json.Net/Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,14 @@ let optionalAt path 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 GenerateDecoder<'T> (): Decoder<'T> =
let serializer = JsonSerializer()
for conv in Converters.converters do
serializer.Converters.Add(conv)
serializer.Converters.Add(Converters.CacheConverter.Singleton)
fun token ->
token.ToObject<'T>(serializer) |> Ok

static member DecodeString<'T>(json: string): 'T =
let settings = JsonSerializerSettings(Converters = Converters.converters)
let settings = JsonSerializerSettings(Converters = [|Converters.CacheConverter.Singleton|])
JsonConvert.DeserializeObject<'T>(json, settings)

static member DecodeString(json: string, t: System.Type): obj =
let settings = JsonSerializerSettings(Converters = Converters.converters)
let settings = JsonSerializerSettings(Converters = [|Converters.CacheConverter.Singleton|])
JsonConvert.DeserializeObject(json, t, settings)
9 changes: 1 addition & 8 deletions src/Thoth.Json.Net/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,7 @@ let encode (space: int) (token: JToken) : string =
let encodeAuto (space: int) (value: obj) : string =
// TODO: Can we set indentation space?
let format = if space = 0 then Formatting.None else Formatting.Indented
let settings = JsonSerializerSettings(Converters = Converters.converters,
Formatting = format)
JsonConvert.SerializeObject(value, settings)

let encodeWithConverter<'T> (converter: JsonConverter) (space: int) (value: obj) : string =
// TODO: Can we set indentation space?
let format = if space = 0 then Formatting.None else Formatting.Indented
let settings = JsonSerializerSettings(Converters = [|converter|],
let settings = JsonSerializerSettings(Converters = [|Converters.CacheConverter.Singleton|],
Formatting = format)
JsonConvert.SerializeObject(value, settings)

Expand Down