jitbit / JsonIgnoreProps

tiny helper class to exclude a property from Json Serialization

Home Page:https://www.jitbit.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

https://www.jitbit.com/


Languages

Language:C# 100.0%