0

Problem: Static class members are being serialized when they actually should not.

Note that I have the preview features activated which enables me to use static abstract attributes in interfaces. To have Newtonsoft ignore any fields/properties when serializing, I use a self-defined attribute taken from here:

public class JsonIgnoreSerializationAttribute : Attribute { }
class JsonPropertiesResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
    protected override List<MemberInfo> GetSerializableMembers(Type objectType)
    {
        //Return properties that do NOT have the JsonIgnoreSerializationAttribute
        return objectType.GetProperties()
                         .Where(pi => !Attribute.IsDefined(pi, typeof(JsonIgnoreSerializationAttribute)))
                         .ToList<MemberInfo>();
    }
}

This the interface, simplified for the sake of illustration:

public interface IApiObject
{
    public string ApiObjectId { get; set; }

    static abstract string ObjectName { get; }
}

Now I have a class ApiObject implementing the interface:

 public abstract class ApiObject : IApiObject
 {
    [JsonProperty(PropertyName = "api_object_id")]
    [JsonIgnoreSerialization]
    public string? ApiObjectId { get; set; }

    [JsonIgnoreSerialization]
    public static string ObjectName { get; }
 }

In reality there are many more fields, but when serializing, regular members marked with this attribute are being ignored while static members are not - they are being serialized which I actually want to avoid because it causes errors on the API.

Serialization:

 jsonString = JsonConvert.SerializeObject(batch, new JsonSerializerSettings { ContractResolver = new JsonPropertiesResolver() });

I wonder if this is

  • a bug or
  • Newtonsoft is just not supporting the preview feature
  • or me not understanding/seeing something very obvious

Any suggestions?

Aileron79
  • 523
  • 5
  • 27
  • Are you saying that `[JsonIgnore]` doesn't work so you had to create your own attribute? – ProgrammingLlama Aug 03 '22 at 07:04
  • [JsonIgnore] works only for deserialization, at least for me. In fact, I use that decorator as well. [JsonIgnore] [NotMapped] [JsonIgnoreSerialization] – Aileron79 Aug 03 '22 at 07:05
  • *[JsonIgnore] works only for deserialization, at least for me.* -- can you share a [mcve] reproducing that? – dbc Aug 03 '22 at 14:55
  • It seems you took `JsonPropertiesResolver` from [this answer](https://stackoverflow.com/a/35234883/3744182) by [Jraco11](https://stackoverflow.com/users/3174275/jraco11), but this contract resolver is broken precisely because it ignores `JsonIgnoreAttribute`. – dbc Aug 03 '22 at 15:16
  • 1
    To fix it, use `base.GetSerializableMembers(objectType).Where(pi => !Attribute.IsDefined(pi, typeof(JsonIgnoreSerializationAttribute))).ToList();` as shown in [this comment](https://stackoverflow.com/questions/11564091/making-a-property-deserialize-but-not-serialize-with-json-net#comment61914098_35234883) by [Alain](https://stackoverflow.com/users/529618/alain) – dbc Aug 03 '22 at 15:17
  • Does fixing `JsonPropertiesResolver` and applying `[JsonIgnore]` fix your problem? – dbc Aug 03 '22 at 19:44
  • Thanks everybody, especially @dbc, whose suggestion solved the issue for me! – Aileron79 Aug 04 '22 at 06:49

0 Answers0