Closed inputusernamehere closed 5 months 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.
It could be worth looking at this: https://github.com/pocketberserker/Utf8Json.FSharpExtensions
which adds some F# primitives to what Utf8Json will serialize/deserialize.
@inputusernamehere What did you end up doing in the end? Is there something worth putting back into Giraffe or potentially PRing directly to Utf8Json
?
I used ResizeArray<string>
instead of string list
, which serializes correctly. Not sure what's best to do for Giraffe here.
Just to keep recorded, I tested with giraffe-template::1.4.0
and I'm still having this problem.
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
@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"]}
@64J0 I believe this can be closed.
Good catch @esbenbjerre.
Potentially related PRs:
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: