Skip to content

Commit

Permalink
Merge branch 'releases/v1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Oct 7, 2022
2 parents e0d3097 + df09f75 commit 1bda46b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/FSharp.SystemTextJson/Record.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ type JsonRecordConverter<'T>(options: JsonSerializerOptions, fsOptions: JsonFSha
let fields = FSharpType.GetRecordFields(recordType, true)

let allProperties =
let all = recordType.GetProperties(BindingFlags.Instance ||| BindingFlags.Public)
let allPublic =
recordType.GetProperties(BindingFlags.Instance ||| BindingFlags.Public)
let all =
if fields[0].GetGetMethod(true).IsPublic then
allPublic
else
Array.append fields allPublic
if fsOptions.IncludeRecordProperties then
all
else
Expand Down
50 changes: 50 additions & 0 deletions tests/FSharp.SystemTextJson.Tests/Test.Record.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ module NonStruct =
let actual = JsonSerializer.Serialize({ bx = 1; by = "b" }, options)
Assert.Equal("""{"bx":1,"by":"b"}""", actual)

type internal Internal = { ix: int }

[<Fact>]
let ``serialize non-public record`` () =
let actual = JsonSerializer.Serialize({ ix = 1 }, options)
Assert.Equal("""{"ix":1}""", actual)

[<Fact>]
let ``deserialize non-public record`` () =
let actual = JsonSerializer.Deserialize<Internal>("""{"ix":1}""", options)
Assert.Equal({ ix = 1 }, actual)

type PrivateFields = private { px: int }

[<Fact>]
let ``serialize record with private fields`` () =
let actual = JsonSerializer.Serialize({ px = 1 }, options)
Assert.Equal("""{"px":1}""", actual)

[<Fact>]
let ``deserialize record with private fields`` () =
let actual = JsonSerializer.Deserialize<PrivateFields>("""{"px":1}""", options)
Assert.Equal({ px = 1 }, actual)

[<Fact>]
let ``not fill in nulls`` () =
try
Expand Down Expand Up @@ -396,6 +420,32 @@ module Struct =
let actual = JsonSerializer.Serialize({ bx = 1; by = "b" }, options)
Assert.Equal("""{"bx":1,"by":"b"}""", actual)

[<Struct>]
type internal Internal = { ix: int }

[<Fact>]
let ``serialize non-public record`` () =
let actual = JsonSerializer.Serialize({ ix = 1 }, options)
Assert.Equal("""{"ix":1}""", actual)

[<Fact>]
let ``deserialize non-public record`` () =
let actual = JsonSerializer.Deserialize<Internal>("""{"ix":1}""", options)
Assert.Equal({ ix = 1 }, actual)

[<Struct>]
type PrivateFields = private { px: int }

[<Fact>]
let ``serialize record with private fields`` () =
let actual = JsonSerializer.Serialize({ px = 1 }, options)
Assert.Equal("""{"px":1}""", actual)

[<Fact>]
let ``deserialize record with private fields`` () =
let actual = JsonSerializer.Deserialize<PrivateFields>("""{"px":1}""", options)
Assert.Equal({ px = 1 }, actual)

[<Fact>]
let ``not fill in nulls`` () =
try
Expand Down

0 comments on commit 1bda46b

Please sign in to comment.