cognitedata / oryx

.NET Cross platform and highly composable middleware for building web request handlers in F#
Apache License 2.0
202 stars 10 forks source link

Add support for FSharp.Json serialization library #109

Open ghost opened 1 year ago

ghost commented 1 year ago

It would be nice to have support for F# FSharp.Json library to the already existing Thoth, Newtonsoft and SystemTextJson.

Will try implementing that for my local project for now, if it works -> will create a PR in this repo.

Any suggestions on impoving the code will be highly appreciated. 🙂

ghost commented 1 year ago

@dbrattli could you please help with understanding next things:?

        // json handler w/ FSharp.Json library
        let json<'TResult> (options: JsonConfig) (source: HttpHandler<HttpContent>): HttpHandler<'TResult> =
            let parser (stream: Stream) =
               use sr = new StreamReader(stream)
               // `ReadToEnd` can throw OOM exception. What is the best way to handle it in Oryx?
               let textJson = sr.ReadToEnd()
               Json.deserializeEx<'TResult> options textJson

            parse parser source
  1. What is the best way to handle OOM exception for StreamReader.ReadToEnd()? I have only seen one for Thoth, but I don't know if it will be an OK to return Result wrapped model

    match ret with
                | Ok result -> return result
                | Error error -> return raise (JsonDecodeException)
  2. What is the purpose of JsonPushStreamContent and do I need to implement that as well?