Skip to content

Instantly share code, notes, and snippets.

@layomia
Last active March 23, 2020 21:04
Show Gist options
  • Save layomia/3e15d368ff4216f9c228e460723cac64 to your computer and use it in GitHub Desktop.
Save layomia/3e15d368ff4216f9c228e460723cac64 to your computer and use it in GitHub Desktop.
JsonIgnore_API_change

Approach A

/// <summary>
/// Prevents a property from being serialized or deserialized.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class JsonIgnoreAttribute : JsonAttribute
{
    /// <summary>
    /// Specifies the condition that must be met before a property will be ignored. Default value: <see cref="JsonIgnoreCondition.Always"/>.
    /// </summary>
    public JsonIgnoreCondition Condition { get; }

    /// <summary>
    /// Initializes a new instance of <see cref="JsonIgnoreAttribute"/>.
    /// </summary>
    public JsonIgnoreAttribute(JsonIgnoreCondition condition = JsonIgnoreCondition.Always) { }
}

Approach B

/// <summary>
/// Prevents a property from being serialized or deserialized.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class JsonIgnoreAttribute : JsonAttribute
{
    /// <summary>
    /// Specifies the condition that must be met before a property will be ignored. Default value: <see cref="JsonIgnoreCondition.Always"/>.
    /// </summary>
    public JsonIgnoreCondition Condition { get; set; } = JsonIgnoreCondition.Always;

    /// <summary>
    /// Initializes a new instance of <see cref="JsonIgnoreAttribute"/>.
    /// </summary>
    public JsonIgnoreAttribute() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment