使用 Json.NET 将 F# 可变变量序列化为 JSON 会生成重复项 [英] Serialization of F# mutable variable to JSON using Json.NET generates duplicated items
本文介绍了使用 Json.NET 将 F# 可变变量序列化为 JSON 会生成重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
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
代码的输出为:
{
"name@": "abc",
"height@": 200,
"name": "abc",
"height": 200
}
如何避免属性中带有@"的输出?
How can I avoid the outputs with '@' in the properity?
推荐答案
试试这个:
[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type T = {
name : string;
height : int;
}
MemberSerialization.OptOut
仅导致公共成员被序列化(跳过作为记录实现细节的私有字段).CLIMutable
属性是 专门用于序列化,并且不必为每个成员添加 mutable
前缀.
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
.
这篇关于使用 Json.NET 将 F# 可变变量序列化为 JSON 会生成重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文