serialization - 使用 Json.net 往返 F# 可区分联合时出现问题

标签 serialization f# json.net ravendb

我在 F# 中有以下枚举

 type MyEnum = 
    | Value1
    | Value2
    | Value3

这是编译器在编译代码时生成的内容

   [Serializable]
    [DebuggerDisplay("{__DebugDisplay(),nq}")]
    public class MyEnum : IEquatable<MyEnum>, IStructuralEquatable, IComparable<MyEnum>, IComparable, IStructuralComparable
    {
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public bool IsValue1 { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public bool IsValue2 { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public bool IsValue3 { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public int Tag { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public static MyEnum Value1 { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public static MyEnum Value2 { get; }
        [CompilerGenerated]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [DebuggerNonUserCode]
        public static MyEnum Value3 { get; }

        [CompilerGenerated]
        public override sealed int CompareTo(MyEnum obj);
        [CompilerGenerated]
        public override sealed int CompareTo(object obj);
        [CompilerGenerated]
        public override sealed int CompareTo(object obj, IComparer comp);
        [CompilerGenerated]
        public override sealed bool Equals(MyEnum obj);
        [CompilerGenerated]
        public override sealed bool Equals(object obj);
        [CompilerGenerated]
        public override sealed bool Equals(object obj, IEqualityComparer comp);
        [CompilerGenerated]
        public override sealed int GetHashCode();
        [CompilerGenerated]
        public override sealed int GetHashCode(IEqualityComparer comp);

        public static class Tags
        {
            public const int Value1 = 0;
            public const int Value2 = 1;
            public const int Value3 = 2;
        }
    }

问题 当上述枚举类型用于序列化时,需要为每个属性进行非静态 set 进行反序列化,则会失败。事实上,我已经将它与 RavenDB 和 Json.Net 序列化一起使用,我得到了以下内容

myenummember = {}

如何通过序列化器有效地使用 F# 枚举类型?

最佳答案

您可以提供知道如何来回转换的 JsonConverter 实例。

关于serialization - 使用 Json.net 往返 F# 可区分联合时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924976/

相关文章:

javascript - 缩短用于搜索和许多过滤器的查询字符串

java - 以互操作方式序列化消息的最佳方式是什么?

xml-serialization - JSON.NET、XmlSerializer 和 "Specified"属性

django - 如何通过 django rest api 中的一个序列化器发布多个模型数据

未提供 C# XML 序列化 int 标记值

visual-studio-2013 - 错误构建 F# :"Files in libraries or multiple-file applications must begin with a namespace or module declaration"

f# - 如何将记录元组解构为私有(private)值

c# - 从 C# 方法返回 F# 接口(interface)

c# - 将数据保存到 DocumentDb 中的多个集合中

c# - 如何将名称-值对数组的 JSON 数组反序列化为 List<MyClass>