使用Json.NET将F#可变变量序列化为JSON会生成重复项

前端之家收集整理的这篇文章主要介绍了使用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
}

如何以正确的“@”来避免输出

解决方法

试试这个:

[<CLIMutable>]
[<JsonObject(MemberSerialization=MemberSerialization.OptOut)>]
type T = {
    name : string;
    height : int;
    }

MemberSerialization.OptOut仅导致公共成员被序列化(跳过作为记录的实现细节的私有字段). CLIMutable属性intended specifically for serialization,并且不必为每个成员添加可变的前缀.

猜你在找的Json相关文章