【问题标题】:How to serialize immutable struct如何序列化不可变结构
【发布时间】:2019-10-08 17:22:36
【问题描述】:

我正在尝试使用 JSon.NET 序列化一个不可变结构,但我不知道该怎么做。序列化的结果是一个空的 json {}。我更喜欢使用 JsonNET 而不是像BinaryFormatter

结构

[Serializable]
    public struct Settings : IEquatable<Settings> {

        private readonly (
            TimeSpan from,
            TimeSpan until,
            TimeSpan repeatInterval,
            TimeSpan popupInterval,
            string notes
        ) _value;

        [JsonIgnore]
        public TimeSpan From => _value.from;
        [JsonIgnore]
        public TimeSpan Until => _value.until;
        [JsonIgnore]
        public TimeSpan Repeat => _value.repeatInterval;
        [JsonIgnore]
        public TimeSpan PopUpInterval => _value.popupInterval;
        [JsonIgnore]
        public string Notes => _value.notes;



        public Settings(
            TimeSpan from,
            TimeSpan until,
            TimeSpan repeatInterval,
            TimeSpan popUpInterval,
            string notes
        ) => _value = (
            from,
            until,
            repeatInterval,
            popUpInterval,
            notes
        );

        public bool Equals(Settings other) => _value == other._value;
        public override bool Equals(object obj) => obj is Settings other && this.Equals(other);
        public override int GetHashCode() => _value.GetHashCode();
        public override string ToString() => _value.ToString();



        public static bool operator ==(Settings a, Settings b) => a.Equals(b);
        public static bool operator !=(Settings a, Settings b) => !(a == b);
    }

计划

static void Main(string[] args) {
            Settings settings = new Settings(new TimeSpan(0),
                new TimeSpan(0,1,1),
                new TimeSpan(1,2,3),
                new TimeSpan(2,4,3),
                "adisor");
            var obj = JsonConvert.SerializeObject(settings);
            var newone = JsonConvert.DeserializeObject<Settings>(obj);
        }

【问题讨论】:

    标签: serialization struct .net-core json.net immutability


    【解决方案1】:

    对于序列化,JsonIgnore 属性需要从目标属性中移除。对于反序列化,将在反序列化过程中使用的构造函数需要使用JsonConstruct 属性进行标记。可选地,序列化名称(属性)需要通过JsonProperty 属性与反序列化名称(此处为构造函数参数)进行协调。

        public TimeSpan From => _value.from;
    
        public TimeSpan Until => _value.until;
    
        public TimeSpan Repeat => _value.repeatInterval;
    
        public TimeSpan PopUpInterval => _value.popupInterval;
    
        public string Notes => _value.notes;
    
        [JsonConstructor] //choose a constructor for deserialization
        public Settings(
            TimeSpan from,
            TimeSpan until,
            [JsonProperty("Repeat")]TimeSpan repeatInterval, //same name used for serialization
            TimeSpan popUpInterval,
            string notes
        ) => _value = (
            from,
            until,
            repeatInterval,
            popUpInterval,
            notes
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多