https://github.com/jitbit/jsonignoreprops
tiny helper class to exclude a property from Json Serialization
https://github.com/jitbit/jsonignoreprops
csharp json json-net newtonsoft-json
Last synced: about 2 months ago
JSON representation
tiny helper class to exclude a property from Json Serialization
- Host: GitHub
- URL: https://github.com/jitbit/jsonignoreprops
- Owner: jitbit
- Created: 2019-12-07T15:03:58.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-06-27T09:22:14.000Z (almost 3 years ago)
- Last Synced: 2025-12-17T18:34:33.053Z (4 months ago)
- Topics: csharp, json, json-net, newtonsoft-json
- Language: C#
- Homepage: https://www.jitbit.com/
- Size: 6.84 KB
- Stars: 10
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```csharp
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.
```csharp
var resolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" });
JsonConvert.SerializeObject(
YourObject,
new JsonSerializerSettings() {
ContractResolver = resolver //reuse
};
);
```
---