Skip to content

Instantly share code, notes, and snippets.

@JamesNK
Created March 31, 2012 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesNK/2269465 to your computer and use it in GitHub Desktop.
Save JamesNK/2269465 to your computer and use it in GitHub Desktop.
Json.NET Metro Upgrade Kit
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);
}
}
@XiXora
Copy link

XiXora commented Apr 1, 2012

Thank you so much.
It is now much easier to build my Metro applications!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment