Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BeachyHeadCode opened this issue Jul 1, 2024 · 2 comments

Comments

@BeachyHeadCode
Copy link

BeachyHeadCode commented Jul 1, 2024

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
Copy link

elgonzo commented Jul 2, 2024

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
Copy link
Author

@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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants