Skip to content

tiny helper class to exclude a property from Json Serialization

Notifications You must be signed in to change notification settings

jitbit/JsonIgnoreProps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

JsonIgnoreProps

This is a tiny helper class to exclude a property from Json Serialization when using Newtonsoft.Json

In case you have no access to the actual class you're serializing - so you can't add any attributes (like [JsonIgnore]), or you simply don't want to. Orif you would like to decide this at run time - which properties to serialize - then use this class.

Usage:

JsonConvert.SerializeObject(
	YourObject,
	new JsonSerializerSettings() {
		ContractResolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" })
	};
);

For better performance make sure you cache the contractResolver object, do not create it every time.

var resolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" });

JsonConvert.SerializeObject(
	YourObject,
	new JsonSerializerSettings() {
		ContractResolver = resolver //reuse
	};
);

About

tiny helper class to exclude a property from Json Serialization

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages