Created
May 15, 2021 22:53
-
-
Save aspnetde/f34aa459375b0301a80c26b9ee44a8dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Infrastructure | |
open System.Text.Encodings.Web | |
open System.Text.Json | |
open System.Text.Json.Serialization | |
[<RequireQualifiedAccess>] | |
module JsonSerializer = | |
let private setupOptions (options: JsonSerializerOptions) = | |
options.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase | |
options.MaxDepth <- 0 | |
options.Encoder <- JavaScriptEncoder.UnsafeRelaxedJsonEscaping | |
options.Converters.Add( | |
JsonFSharpConverter( | |
JsonUnionEncoding.UnwrapSingleCaseUnions | |
||| JsonUnionEncoding.FSharpLuLike, | |
unionTagNamingPolicy = JsonNamingPolicy.CamelCase | |
) | |
) | |
options.IgnoreNullValues <- true | |
options | |
let private options = JsonSerializerOptions() |> setupOptions | |
let serialize value = | |
JsonSerializer.Serialize(value, options) | |
let deserialize<'T> (json: string) = | |
JsonSerializer.Deserialize<'T>(json, options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment