Reputation: 61
Here is my code:
open Newtonsoft.Json
open Newtonsoft.Json.Converters
type T = {
mutable name : string;
mutable height : int;
}
let a = { name = "abc"; height = 180;}
a.height <- 200
let b = JsonConvert.SerializeObject(a, Formatting.Indented)
printfn "%s" b
The output of the code is:
{
"name@": "abc",
"height@": 200,
"name": "abc",
"height": 200
}
How can I avoid the outputs with '@' in the properity?
Upvotes: 6
Views: 721
Reputation: 460
I had the same issue and I had to look eventually this was what worked, after so much digging through Json.Net docs.
open System
open System.Runtime.Serialization
[<CLIMutable>]
[<DataContract>]
type T = {
[<DataMember>] mutable name : string;
[<DataMember>] mutable height : int }
Upvotes: 1
Reputation: 47904
Try this:
[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type T = {
name : string;
height : int;
}
MemberSerialization.OptOut
causes only public members to be serialized (skipping private fields which are an implementation detail of records). The CLIMutable
attribute is intended specifically for serialization and saves from having to prefix each member with mutable
.
Upvotes: 3
Reputation: 16878
With the help of DataMemberAttribute
you can specify names of the serialized members:
type T = {
[<field: DataMember(Name="name")>]
mutable name : string;
[<field: DataMember(Name="height")>]
mutable height : int;
}
Upvotes: 2
Reputation: 2840
Did you try to add attributes [<...>] in front of the property? Because that attribute will only apply to the property, not to the generated backend. Not sure which attribute JSON.NET reacts to, however.
Upvotes: 1