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

Exclude nested data? #1

Open
aim12340 opened this issue Nov 25, 2021 · 1 comment
Open

Exclude nested data? #1

aim12340 opened this issue Nov 25, 2021 · 1 comment

Comments

@aim12340
Copy link

I'm trying to extend your code to ignore nested data.

E.g. I have a product -> part -> supplier setup, and I want the product and part serialized but not the supplier.

When serializing a product I'd like to be able to use something like
jsSettings.ContractResolver = new IgnorePropertiesResolver(new[] { "part.supplier" });

I know I can use just "supplier" but that's open to side effects as it doens't specify a particular property.

Any ideas?

@TuxedoCatGames
Copy link

I needed this same functionality and got it working like this

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
	JsonProperty property = base.CreateProperty(member, memberSerialization);

	var propertyAndDeclaringType = property.DeclaringType.Name + "." + property.PropertyName;

	if (this.ignoreProps.Contains(property.PropertyName) || this.ignoreProps.Contains(propertyAndDeclaringType))
	{
		property.ShouldSerialize = _ => false;
	}

	return property;
}

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