JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.76k stars 3.25k forks source link

[Bug] CreateProperties adds JsonIgnore fields when it should not MemberSerialization.OptOut #2968

Open BeachyHeadCode opened 3 months ago

BeachyHeadCode commented 3 months ago

Source/destination types


 public class SomeClass {
    [JsonIgnore]
    public string activeProject = string.Empty;
}

Source/destination JSON

No example required. Should not create a json file.

{""}

Expected behavior

When [JsonIgnore] is used on a public field it should be ignored per OptOut.

Actual behavior

Class members with the custom attribute tag [JsonIgnore] are shown in the IList<JsonProperty> when called by the method CreateProperties(Type type, MemberSerialization memberSerialization).

Steps to reproduce

JsonSerializerSettings settings = new JsonSerializerSettings()
{
    Formatting = Formatting.Indented
};

SomeClass @object = new SomeClass();

string json = JsonConvert.SerializeObject(@object, settings);

Follow the code all the way to

IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)

and MemberSerialization is set to MemberSerialization.OptOut

elgonzo commented 3 months ago

There is no bug here.

First,

{""}

serializer will not generate an output like {""} (which by the way would not be valid json either). For instances of your example class SomeClass, the serializaer will create an empty json object {}.

When [JsonIgnore] is used on a public field it should be ignored per OptOut.

Yes, and it does exactly that already. The [JsonIgnore] on the activeProject field leads to this field not being in the json data.

IList CreateProperties(Type type, MemberSerialization memberSerialization)

If you look at the JsonProperty instance representing the activeProject, you'll notice that its JsonProperty.Ignored property is set to true (which then leads to the serializer excluding it from serialization).

Should not create a json file.

That's an incorrect expectation. You serialize a SomeClass instance, hence you should get a json output representing this instance. The result being an empty json object {} is correctly representing a SomeClass instance, as there are no members in SomeClass participating in serialization. (Any json result deviating from this would be either due to a custom contract resolver or a custom json converter altering the behavior of the serializer.)

(P.S.: I am not associated or related to the Newtonsoft.Json project and its author/maintainer. I am just a fellow user of the library... or perhaps former user, as these days it's pretty much always STJ over Newtonsoft.Json.)

BeachyHeadCode commented 3 months ago

@elgonzo as mentioned in my initial entry there is no json example. The {""} is me implying that with the text above it. If that is what you got hung up on then ignore {""} as that means nothing to me or anyone else.

[JsonIgnore] does not function as intended. CreateProperties returns the field with [JsonIgnore] attribute when the field is public. That does not comply with OptOut. It should be excluded but is not. I would give you screenshots of the objects, but my company blocks the upload of images to GitHub. Only the access modifier of the field is being acknolleged. If i change:

public class SomeClass {
    [JsonIgnore]
    public string activeProject = string.Empty;
}

To:

public class SomeClass {
    [JsonIgnore]
    private string activeProject = string.Empty;
}

It won't show. Same with:

public class SomeClass {
    private string activeProject = string.Empty;
}