Serializing F# Record type to JSON includes '@' character after each property

前端 未结 2 1699
面向向阳花
面向向阳花 2020-12-03 10:45

The DataContractJsonSerializer creates JSON for F# record types that includes the \'@\' character after each property name. Does anyone know if it is possible to get JSON th

相关标签:
2条回答
  • 2020-12-03 11:30

    Although Daniel's solution will work correctly, it is rather tedious to have to add attributes to every property in the record. It turns out that Json.NET produces more readable JSON out of the box. For my application I do not need to use the DataContractSerializer specifically, so JSON.net it is!

    0 讨论(0)
  • 2020-12-03 11:34

    It's using the name of the compiler generated backing fields. You can use DataMemberAttribute to provide your own names.

    [<DataContract>]
    type Update = {
        [<field: DataMember(Name="name")>]
        name: string;
        [<field: DataMember(Name="latitude")>]
        latitude: decimal;
        [<field: DataMember(Name="longitude")>]
        longitude: decimal;
        [<field: DataMember(Name="heart_rate")>]
        heart_rate: int}
    
    0 讨论(0)
提交回复
热议问题