Skip to content

Commit 62b02c1

Browse files
authored
Updated custom serialization example to SwaggerProvider 2.0 (#239)
(Newtonsoft -> System.Text.Json version 8.0)
1 parent d5ec2f8 commit 62b02c1

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

docs/Customization.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,35 @@ Serializer is configurable but not replaceable! Type provider emit type with [Js
8888

8989
</Note>
9090

91-
```fsharp {highlight:['9-10', '24-28', '33-34']}
91+
```fsharp {highlight:['20-28', '33-34']}
9292
open System
9393
open SwaggerProvider
94-
open Newtonsoft.Json
94+
open System.Text.Json
95+
open System.Text.Json.Serialization
9596
9697
let [<Literal>] Schema = "https://petstore.swagger.io/v2/swagger.json"
9798
type PetStore = OpenApiClientProvider<Schema, PreferAsync=true>
9899
100+
let jsonSerializerSettings =
101+
// nuget: System.Text.Json
102+
let settings = JsonSerializerOptions(
103+
//PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
104+
// ...
105+
IgnoreNullValues = true
106+
)
107+
// nuget: FSharp.SystemTextJson
108+
JsonFSharpOptions.Default().AddToJsonSerializerOptions settings
109+
settings
110+
99111
type MyApiClient() =
100112
// inherit from provided API client
101113
inherit PetStore.Client()
102114
103-
let jsonSerializerSettings =
104-
let settings =
105-
JsonSerializerSettings(
106-
NullValueHandling = NullValueHandling.Ignore,
107-
Formatting = Formatting.None)
108-
[ // Your custom JsonConverter's for F# types
109-
Swagger.Serialization.OptionConverter () :> JsonConverter
110-
Swagger.Serialization.ByteArrayConverter () :> JsonConverter
111-
]
112-
|> List.iter settings.Converters.Add
113-
settings
114-
115115
// Overload default implementation of Serialize & Deserialize
116116
override __.Serialize(value:obj): string =
117-
JsonConvert.SerializeObject(value, jsonSerializerSettings)
117+
JsonSerializer.Serialize(value, jsonSerializerSettings)
118118
override __.Deserialize(value, retTy:Type): obj =
119-
JsonConvert.DeserializeObject(value, retTy, jsonSerializerSettings)
119+
JsonSerializer.Deserialize(value, retTy, jsonSerializerSettings)
120120
121121
122122
[<EntryPoint>]

0 commit comments

Comments
 (0)