public class MyObject { public string Name { get; set; } public int Age { get; set; } [JsonIgnore] public string SecretData { get; set; } [OnSerializing] internal void OnSerializingMethod(StreamingContext context) { SecretData = "encrypted data"; } } var myObj = new MyObject { Name = "John Doe", Age = 32, SecretData = "sensitive data" }; string serializedObj = JsonConvert.SerializeObject(myObj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.All, Converters = new List{new StringEnumConverter("G")} });
public class MyObject { public string Name { get; set; } public int Age { get; set; } [JsonIgnore] public string SecretData { get; set; } [JsonProperty(PropertyName = "Hash")] internal string Hash { get; set; } [OnSerializing] internal void OnSerializingMethod(StreamingContext context) { SecretData = "encrypted data"; Hash = MD5.Create(Encoding.UTF8.GetBytes(Name + Age + SecretData)).ToString(); } } var myObj = new MyObject { Name = "John Doe", Age = 32, SecretData = "sensitive data" }; string serializedObj = JsonConvert.SerializeObject(myObj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore, TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.All, Converters = new ListIn this example, we again use the `OnSerializingMethod` to set the `SecretData` property to an encrypted value. We also add a new property called `Hash` to the object, which is created by hashing together the `Name`, `Age`, and `SecretData` properties. This `Hash` property is then serialized along with the rest of the object.{new StringEnumConverter("G")} });