Created
April 12, 2013 20:18
-
-
Save pedroreys/5374808 to your computer and use it in GitHub Desktop.
Defining a custom JsonConverter and using it with the built-in JsonConverterAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Message | |
{ | |
[JsonConverter(typeof(SHA256StringJsonConverter))] | |
public string Password { get; set; } | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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