From 59a147edb944806c113c429982e914dc173fcab7 Mon Sep 17 00:00:00 2001 From: Josh M <20140997+jcmrva@users.noreply.github.com> Date: Thu, 6 Jul 2023 11:11:00 -0600 Subject: [PATCH] remove Utf8Json --- DOCUMENTATION.md | 15 +------------ src/Giraffe/Giraffe.fsproj | 1 - src/Giraffe/Json.fs | 40 ---------------------------------- tests/Giraffe.Tests/Helpers.fs | 12 ---------- 4 files changed, 1 insertion(+), 67 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index f1a2a075..afdd3983 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -2942,21 +2942,9 @@ By default Giraffe offers three `Json.ISerializer` implementations out of the bo | Name | Description | Default | | :--- | :---------- | :------ | | `NewtonsoftJson.Serializer` | Uses `Newtonsoft.Json` aka Json.NET for JSON (de-)serialization in Giraffe. It is the most downloaded library on NuGet, battle tested by millions of users and has great support for F# data types. Use this json serializer for maximum compatibility and easy adoption. | True | -| `Utf8Json.Serializer` | Uses `Utf8Json` for JSON (de-)serialization in Giraffe. This is the fastest JSON serializer written in .NET with huge extensibility points and native support for directly serializing JSON content to the HTTP response stream via chunked encoding. This serializer has been specifically crafted for maximum performance and should be used when that extra perf is important. | False | | `SystemTextJson.Serializer` | Uses `System.Text.Json` for JSON (de-)serialization in Giraffe. `System.Text.Json` is a high performance serialization library, and aims to be the serialization library of choice for ASP.NET Core. For better support of F# types with `System.Text.Json`, look at [FSharp.SystemTextJson](https://github.com/Tarmil/FSharp.SystemTextJson). | False | -To use `Utf8Json.Serializer` instead of `NewtonsoftJson.Serializer`, register a new dependency of type `Json.ISerializer` during application configuration: - -```fsharp -let configureServices (services : IServiceCollection) = - // First register all default Giraffe dependencies - services.AddGiraffe() |> ignore - - // Now register Utf8Json.Serializer - this.AddSingleton(Utf8Json.Serializer(Utf8Json.Serializer.DefaultResolver)) |> ignore -``` - -Or to use `SystemTextJson.Serializer` instead of `NewtonsoftJson.Serializer`, register a new dependency of type `Json.ISerializer` during application configuration: +To use `SystemTextJson.Serializer` instead of `NewtonsoftJson.Serializer`, register a new dependency of type `Json.ISerializer` during application configuration: ```fsharp let configureServices (services : IServiceCollection) = @@ -2970,7 +2958,6 @@ let configureServices (services : IServiceCollection) = services.AddSingleton(SystemTextJson.Serializer(serializationOptions)) |> ignore ``` - #### Customizing JsonSerializerSettings You can change the default `JsonSerializerSettings` of the `NewtonsoftJson.Serializer` by registering a new instance of `NewtonsoftJson.Serializer` during application startup. For example, the [`Microsoft.FSharpLu` project](https://github.com/Microsoft/fsharplu/wiki/fsharplu.json) provides a Json.NET converter (`CompactUnionJsonConverter`) that serializes and deserializes `Option`s and discriminated unions much more succinctly. If you wanted to use it, and set the culture to German, your configuration would look something like: diff --git a/src/Giraffe/Giraffe.fsproj b/src/Giraffe/Giraffe.fsproj index 021d3ee6..1ef1bb3e 100644 --- a/src/Giraffe/Giraffe.fsproj +++ b/src/Giraffe/Giraffe.fsproj @@ -50,7 +50,6 @@ - diff --git a/src/Giraffe/Json.fs b/src/Giraffe/Json.fs index 0ac57dab..7d3ab8bb 100644 --- a/src/Giraffe/Json.fs +++ b/src/Giraffe/Json.fs @@ -80,46 +80,6 @@ module NewtonsoftJson = return serializer.Deserialize<'T>(jsonTextReader) } -[] -module Utf8Json = - open System.IO - open System.Text - open System.Threading.Tasks - open Utf8Json - - /// - /// is an alternative serializer with - /// great performance and supports true chunked transfer encoding. - /// - /// It uses Utf8Json as the underlying JSON serializer to (de-)serialize - /// JSON content. Utf8Json is currently - /// the fastest JSON serializer for .NET. - /// - /// https://github.com/neuecc/Utf8Json - type Serializer (resolver : IJsonFormatterResolver) = - - static member DefaultResolver = Utf8Json.Resolvers.StandardResolver.CamelCase - - interface Json.ISerializer with - member __.SerializeToString (x : 'T) = - JsonSerializer.ToJsonString (x, resolver) - - member __.SerializeToBytes (x : 'T) = - JsonSerializer.Serialize (x, resolver) - - member __.SerializeToStreamAsync (x : 'T) (stream : Stream) = - JsonSerializer.SerializeAsync(stream, x, resolver) - - member __.Deserialize<'T> (json : string) : 'T = - let bytes = Encoding.UTF8.GetBytes json - JsonSerializer.Deserialize(bytes, resolver) - - member __.Deserialize<'T> (bytes : byte array) : 'T = - JsonSerializer.Deserialize(bytes, resolver) - - member __.DeserializeAsync<'T> (stream : Stream) : Task<'T> = - JsonSerializer.DeserializeAsync(stream, resolver) - [] module SystemTextJson = open System diff --git a/tests/Giraffe.Tests/Helpers.fs b/tests/Giraffe.Tests/Helpers.fs index 1e36c072..37ca86f9 100644 --- a/tests/Giraffe.Tests/Helpers.fs +++ b/tests/Giraffe.Tests/Helpers.fs @@ -16,7 +16,6 @@ open Microsoft.AspNetCore.Builder open Microsoft.Extensions.DependencyInjection open Xunit open NSubstitute -open Utf8Json open System.Text.Json open Newtonsoft.Json open Giraffe @@ -105,7 +104,6 @@ let createHost (configureApp : 'Tuple -> IApplicationBuilder -> unit) type MockJsonSettings = | Newtonsoft of JsonSerializerSettings option - | Utf8 of IJsonFormatterResolver option | SystemTextJson of JsonSerializerOptions option let mockJson (ctx : HttpContext) (settings : MockJsonSettings) = @@ -119,14 +117,6 @@ let mockJson (ctx : HttpContext) (settings : MockJsonSettings) = .Returns(NewtonsoftJson.Serializer(jsonSettings)) |> ignore - | Utf8 settings -> - let resolver = - defaultArg settings Utf8Json.Serializer.DefaultResolver - ctx.RequestServices - .GetService(typeof) - .Returns(Utf8Json.Serializer(resolver)) - |> ignore - | SystemTextJson settings -> let jsonOptions = defaultArg settings SystemTextJson.Serializer.DefaultOptions @@ -138,7 +128,6 @@ let mockJson (ctx : HttpContext) (settings : MockJsonSettings) = type JsonSerializersData = static member DefaultSettings = [ - Utf8 None; Newtonsoft None SystemTextJson None ] @@ -147,7 +136,6 @@ type JsonSerializersData = static member PreserveCaseSettings = [ - Utf8 (Some Utf8Json.Resolvers.StandardResolver.Default) Newtonsoft (Some (JsonSerializerSettings())) SystemTextJson (Some (JsonSerializerOptions())) ]