giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.12k stars 266 forks source link

Serializing empty F# lists with Utf8Json #358

Closed inputusernamehere closed 5 months ago

inputusernamehere commented 5 years ago

I'm having a hard time serializing some F# records that contain empty lists. I assume that this isn't a bug and I'm just doing something wrong. I'm trying to create tests akin to these samples: https://github.com/giraffe-fsharp/Giraffe/blob/master/samples/SampleApp/SampleApp.Tests/Tests.fs

But instead of GetAsync I'm calling PostAsync with some serialized content. This worked fine until I added an empty list to one of my tests. Instead of serializing the empty list into an empty array, an empty object, null, skipping it or any number of other options, it now throws InvalidOperationException: The input list was empty.. If this is intended (I assume it is), then what's the intended way of serializing lists that could potentially be empty?

Repro:

dotnet new giraffe

Change a few lines around the entry point:

open Utf8Json
type Test =
    {
        a : string list
    }
[<EntryPoint>]
let main _ =
    JsonSerializer.ToJsonString({ a = []}) |> ignore
inputusernamehere commented 5 years ago

Apparently this is an issue not just with empty lists but all F# lists. ResizeArray seems to serialize correctly so that can be used instead.

slang25 commented 5 years ago

It could be worth looking at this: https://github.com/pocketberserker/Utf8Json.FSharpExtensions

which adds some F# primitives to what Utf8Json will serialize/deserialize.

dustinmoris commented 5 years ago

@inputusernamehere What did you end up doing in the end? Is there something worth putting back into Giraffe or potentially PRing directly to Utf8Json?

inputusernamehere commented 5 years ago

I used ResizeArray<string> instead of string list, which serializes correctly. Not sure what's best to do for Giraffe here.

64J0 commented 1 year ago

Just to keep recorded, I tested with giraffe-template::1.4.0 and I'm still having this problem.

64J0 commented 1 year ago

Other than that, I noticed that Utf8Json is archived nowadays -> https://github.com/neuecc/Utf8Json


Update:

It was already pointed here -> https://github.com/giraffe-fsharp/Giraffe/issues/451

64J0 commented 1 year ago

@inputusernamehere in order to fix this, you can use this package.

After creating the template project, you could run this command to add the package to the project:

dotnet add package FSharp.SystemTextJson --version 1.2.42

And to test you can replace the Program.fs contents with:

module giraffe_json.App

open System.Text.Json

// ---------------------------------
// Models
// ---------------------------------

type Test =
    {
        a : string list
        b : string list
    }

[<EntryPoint>]
let main args =
    printfn "Serialized result: %s" (JsonSerializer.Serialize({ a = []; b = ["111"; "222" ] }))

    0

The result is:

Serialized result: {"a":[],"b":["111","222"]}
esbenbjerre commented 5 months ago

@64J0 I believe this can be closed.

64J0 commented 5 months ago

Good catch @esbenbjerre.

Potentially related PRs: