Created
March 31, 2012 23:11
-
-
Save JamesNK/2269465 to your computer and use it in GitHub Desktop.
Json.NET Metro Upgrade Kit
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 MetroPropertyNameResolver : DefaultContractResolver | |
{ | |
protected internal override string ResolvePropertyName(string propertyName) | |
{ | |
return ":::" + propertyName.ToUpper() + ":::"; | |
} | |
} | |
public class MetroStringConverter : JsonConverter | |
{ | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
writer.WriteValue(":::" + value.ToString().ToUpper() + ":::"); | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
return existingValue; | |
} | |
public override bool CanConvert(Type objectType) | |
{ | |
return objectType == typeof (string); | |
} | |
} | |
public class MetroColorConverter : JsonConverter | |
{ | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
Color color = (Color) value; | |
Color fixedColor = (color == Color.White || color == Color.Black) ? color : Color.Gray; | |
writer.WriteValue(":::" + fixedColor.ToKnownColor().ToString().ToUpper() + ":::"); | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
return Enum.Parse(typeof (Color), reader.Value.ToString()); | |
} | |
public override bool CanConvert(Type objectType) | |
{ | |
return objectType == typeof (Color); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much.
It is now much easier to build my Metro applications!