Skip to content

Instantly share code, notes, and snippets.

@pedroreys
Created April 12, 2013 20:18
Show Gist options
  • Save pedroreys/5374808 to your computer and use it in GitHub Desktop.
Save pedroreys/5374808 to your computer and use it in GitHub Desktop.
Defining a custom JsonConverter and using it with the built-in JsonConverterAttribute
public class Message
{
[JsonConverter(typeof(SHA256StringJsonConverter))]
public string Password { get; set; }
}
public class SHA256StringJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var hashedPassword = value.ToString().ToSHA256Hash();
writer.WriteValue(hashedPassword);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// The value should be hashed already. Nothing to do here.
return reader.Value;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment