@@ -2,7 +2,6 @@ namespace App
2
2
3
3
open System
4
4
open System.Collections .Generic
5
- open System.Threading .Tasks
6
5
open Oxpecker
7
6
8
7
[<AutoOpen>]
@@ -60,6 +59,7 @@ module HttpHandlers =
60
59
open Npgsql
61
60
open System.Text
62
61
open Microsoft.AspNetCore .Http
62
+ open System.Text .Json
63
63
64
64
let private extra =
65
65
{
@@ -169,56 +169,33 @@ module HttpHandlers =
169
169
ctx.SetContentType( " text/plain" )
170
170
ctx.WriteBytes( result)
171
171
172
+ let jsonSimple value : EndpointHandler =
173
+ let options = JsonSerializerOptions( JsonSerializerDefaults.Web)
174
+ fun ctx ->
175
+ ctx.Response.WriteAsJsonAsync( value, options)
176
+
172
177
let endpoints =
173
178
[|
174
179
route " /plaintext" <| utf8Const " Hello, World!"
175
- route " /json" <| jsonChunked {| message = " Hello, World!" |}
180
+ route " /json" <| jsonSimple {| message = " Hello, World!" |}
176
181
route " /fortunes" fortunes
177
182
route " /db" singleQuery
178
183
route " /queries/{count?}" multipleQueries
179
184
route " /updates/{count?}" multipleUpdates
180
185
|]
181
186
182
-
183
187
module Main =
184
- open SpanJson
185
- open Microsoft.AspNetCore .Http
186
188
open Microsoft.AspNetCore .Builder
187
- open Microsoft.AspNetCore .Hosting
188
189
open Microsoft.Extensions .DependencyInjection
189
190
open Microsoft.Extensions .Hosting
190
191
open Microsoft.Extensions .Logging
191
- open System.Buffers
192
-
193
- type SpanJsonSerializer () =
194
- interface Serializers.IJsonSerializer with
195
- member this.Serialize ( value , ctx , chunked ) =
196
- ctx.Response.ContentType <- " application/json"
197
- if chunked then
198
- if ctx.Request.Method <> HttpMethods.Head then
199
- JsonSerializer.Generic.Utf8.SerializeAsync<_>( value, stream = ctx.Response.Body) .AsTask()
200
- else
201
- Task.CompletedTask
202
- else
203
- task {
204
- let buffer = JsonSerializer.Generic.Utf8.SerializeToArrayPool<_>( value)
205
- ctx.Response.Headers.ContentLength <- buffer.Count
206
- if ctx.Request.Method <> HttpMethods.Head then
207
- do ! ctx.Response.Body.WriteAsync( buffer)
208
- ArrayPool< byte>. Shared.Return( buffer.Array)
209
- else
210
- return ()
211
- }
212
- member this.Deserialize _ =
213
- failwith " Not implemented"
214
192
215
193
[<EntryPoint>]
216
194
let main args =
217
195
let builder = WebApplication.CreateBuilder( args)
218
196
builder.Services
219
197
.AddRouting()
220
198
.AddOxpecker()
221
- .AddSingleton< Serializers.IJsonSerializer>( SpanJsonSerializer())
222
199
|> ignore
223
200
builder.Logging.ClearProviders() |> ignore
224
201
let app = builder.Build()
0 commit comments