使用Json.NET将F#可变变量序列化为JSON会生成重复项 [英] Serialization of F# mutable variable to JSON using Json.NET generates duplicated items

查看:79
本文介绍了使用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属性为

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆