繁体   English   中英

序列化中的Json.net和JsonIgnore

[英]Json.net and JsonIgnore in Serializing

我有一个对象,该对象具有Seri​​alizable属性,该类继承自抽象类,该抽象类继承自其他类,该其他类也继承自接口

我用过

 string included = JsonConvert.SerializeObject(msg,
                              Formatting.Indented,
                              new JsonSerializerSettings { /*ContractResolver = new NotificationPropertyResolver()*/ TypeNameHandling  = TypeNameHandling.All});

因为味精是我要在SignalR中发送此对象的接口,所以我看到它不忽略任何成员,我已经装饰了接口和类

有解决方案吗? 我也尝试过将解析器与我自己的属性一起使用-但结果仍然相同

班级很大,但是...

 [Serializable]
[WebAPINotification(Type = typeof(CSensor), Group = "Sensor")]
public class SensorsStateModeNotification : SensorNotification, IBPMPackagedNotification

public abstract class SensorNotification : BasicLanNotification, ISensNotification

[Serializable]
public class BasicLanNotification : BasicNotification, ILanNotification

[Serializable]
public abstract class BasicNotification : INotification, ISerializable, IOpSerializable

[JsonIgnore]
    public long SentAt 
    {
        get
        {
            return m_sentAt;
        }
        set
        {
            m_sentAt = value;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [JsonIgnore]
    public ENotificationGateway NotificationGateway
    {
        get
        {
            return m_NotifyGateway;
        }
        set
        {
            m_NotifyGateway = value;
        }
    }

您的类型实现ISerializable接口-优先于属性。 如果您不想序列化类的成员,只需不要在ISerializable.GetObjectData实现中返回它们。 请参阅下面的SSCCE (顺便说一句,将来,如果您想获得更好的答案,也应该提供一个答案)作为示例。

public class StackOverflow_18127665
{
    public class WebAPINotificationAttribute : Attribute
    {
        public Type Type { get; set; }
        public string Group { get; set; }
    }
    public class CSensor { }
    public interface INotification { }
    public interface IOpSerializable { }
    public interface IBPMPackagedNotification { }
    public interface ILanNotification { }
    public interface ISensNotification { }
    [Serializable]
    [WebAPINotification(Type = typeof(CSensor), Group = "Sensor")]
    public class SensorsStateModeNotification : SensorNotification, IBPMPackagedNotification { }

    public abstract class SensorNotification : BasicLanNotification, ISensNotification { }

    [Serializable]
    public class BasicLanNotification : BasicNotification, ILanNotification { }

    [Serializable]
    public abstract class BasicNotification : INotification, ISerializable, IOpSerializable
    {
        long m_sentAt;
        ENotificationGateway m_NotifyGateway;
        [JsonIgnore]
        public long SentAt
        {
            get
            {
                return m_sentAt;
            }
            set
            {
                m_sentAt = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public ENotificationGateway NotificationGateway
        {
            get
            {
                return m_NotifyGateway;
            }
            set
            {
                m_NotifyGateway = value;
            }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Comment the lines below not to have this serialized
            info.AddValue("NotificationGateway", this.NotificationGateway);
            info.AddValue("SentAt", this.SentAt);
        }

        #endregion
    }
    public enum ENotificationGateway { First, Second }
    public static void Test()
    {
        BasicNotification msg = new BasicLanNotification
        {
            SentAt = 123,
            NotificationGateway = ENotificationGateway.First
        };
        var str = JsonConvert.SerializeObject(
            msg,
            Newtonsoft.Json.Formatting.Indented,
            new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });
        Console.WriteLine(str);
    }
}
public class NotificationPropertyResolver : DefaultContractResolver
    {
        public NotificationPropertyResolver()
        {
            IgnoreSerializableAttribute = true;
            IgnoreSerializableInterface = true;
        }

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM