Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove Utf8Json support #543

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Json.ISerializer>(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) =
Expand All @@ -2970,7 +2958,6 @@ let configureServices (services : IServiceCollection) =
services.AddSingleton<Json.ISerializer>(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:
Expand Down
1 change: 0 additions & 1 deletion src/Giraffe/Giraffe.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.*" />
<PackageReference Include="System.Text.Json" Version="7.0.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.*" />
<PackageReference Include="Utf8Json" Version="1.3.*" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.*" PrivateAssets="All" />
<PackageReference Include="Giraffe.ViewEngine" Version="1.4.*" />
</ItemGroup>
Expand Down
40 changes: 0 additions & 40 deletions src/Giraffe/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,6 @@ module NewtonsoftJson =
return serializer.Deserialize<'T>(jsonTextReader)
}

[<RequireQualifiedAccess>]
module Utf8Json =
open System.IO
open System.Text
open System.Threading.Tasks
open Utf8Json

/// <summary>
/// <see cref="Utf8Json.Serializer" /> 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.
/// </summary>
/// <remarks>https://github.com/neuecc/Utf8Json</remarks>
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)

[<RequireQualifiedAccess>]
module SystemTextJson =
open System
Expand Down
12 changes: 0 additions & 12 deletions tests/Giraffe.Tests/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand All @@ -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<Json.ISerializer>)
.Returns(Utf8Json.Serializer(resolver))
|> ignore

| SystemTextJson settings ->
let jsonOptions =
defaultArg settings SystemTextJson.Serializer.DefaultOptions
Expand All @@ -138,7 +128,6 @@ let mockJson (ctx : HttpContext) (settings : MockJsonSettings) =
type JsonSerializersData =

static member DefaultSettings = [
Utf8 None;
Newtonsoft None
SystemTextJson None
]
Expand All @@ -147,7 +136,6 @@ type JsonSerializersData =

static member PreserveCaseSettings =
[
Utf8 (Some Utf8Json.Resolvers.StandardResolver.Default)
Newtonsoft (Some (JsonSerializerSettings()))
SystemTextJson (Some (JsonSerializerOptions()))
]
Expand Down