public
Created

Json.NET Metro Upgrade Kit

  • Download Gist
JsonMetroUpgrade.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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);
}
}

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.