Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
323 stars 44 forks source link

how to deal with case mismatch for record #130

Closed yuuuxt closed 1 year ago

yuuuxt commented 1 year ago

if I want to de-serialize a received {"message":"some message", "type":"some type", "code":123} to type SampleTarget = {Message: string; Type: string; code: int}, the mismatch of the case will stop me: The received keys are all lower cases, and the F# type needs to be Pascal Case.

Serialization case is similar.

(I take this example just because we cannot have type SampleTarget = {...; type: string})

I only found unionTagCaseInsensitive in documentation, which doesn't fit here.

It would be good if there is an option to help deal with such an issue. Any suggestions? Thanks!

Tarmil commented 1 year ago

Record fields use options from the JsonSerializerOptions. So you can use either JsonSerializerOptions(PropertyNameCaseInsensitive = true) or JsonSerializerOptions(PropertyNamingPolicy = JsonNamingPolicy.CamelCase)