Overriding JSON serializer in Giraffe
source link: https://wkalmar.github.io/post/overriding-json-serializer-in-giraffe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Overriding JSON serializer in Giraffe
calendar
Dec 23, 2021
clock
2 min read
I use my side-project KyivStationWalk as a set of my opinionated takes on software architecture. No wonder I use F# here. One of the types I use in this project to describe my domain is the subway station branch. There are 3 branches in Kyiv named by their traditional colors.
type Branch =
| Red
| Blue
| Green
Branch
type with JsonFSharpConverter(JsonUnionEncoding.BareFieldlessTags)
attribute.Here’s an example of a serialized subway station. Pay attention to how neat looks branch
property.
{
"id": "5c2e1d0c867a6335386700d9",
"name": {
"en": "Svyatoshyn",
"ua": "Святошин"
},
"branch": "Red",
"location": {
"lattitude": 50.457903,
"longitude": 30.390614
}
}
But there is a downside. Since the default serializer is Newtonsoft.Json you have to bake in serialization into your request handlers. (please note that logging and error handling is omitted for brevity)
let getApproved =
fun next httpContext ->
task {
let! routes = DbAdapter.getApprovedRoutes |> Async.StartAsTask
let domainRoutes = DbMappers.dbRoutesToRoutes routes
let result = RouteModels.toShortRoutes domainRoutes
let serialized = JsonSerializer.Serialize(result, Common.serializerOptions)
return! text serialized next httpContext
}
json
instead of text
. In order to achieve this, you have to register the serializer of your choice in ASP.NET Core services registration section
let configureServices (services : IServiceCollection) =
services.AddGiraffe() |> ignore
services.AddSingleton<Json.ISerializer>(SystemTextJson.Serializer(Common.serializerOptions)) |> ignore
let getApproved =
fun next httpContext ->
task {
let! routes = DbAdapter.getApprovedRoutes |> Async.StartAsTask
let domainRoutes = DbMappers.dbRoutesToRoutes routes
let result = RouteModels.toShortRoutes domainRoutes
return! json result next httpContext
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK