Last active
October 20, 2018 19:54
-
-
Save jlewin/4f1ae1425fcdd5391c759d2a74408a16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
foreach(var mode in item.Value.Modes) | |
{ | |
string directory = $@"c:\temp\themes\{item.Key}"; | |
Directory.CreateDirectory(directory); | |
var theme = item.Value.GetTheme(mode, item.Value.DefaultColor); | |
theme.ThemeName = $"{item.Key} {mode}"; | |
File.WriteAllText( | |
Path.Combine(directory, $"{item.Key}-{mode}.json"), | |
JsonConvert.SerializeObject( | |
theme, | |
Formatting.Indented, | |
new JsonSerializerSettings | |
{ | |
ContractResolver = new ThemeContractResolver() | |
})); | |
} | |
public class ThemeContractResolver : DefaultContractResolver | |
{ | |
private static Type ColorType = typeof(Color); | |
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) | |
{ | |
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); | |
return props.Where(p => p.Writable).ToList(); | |
} | |
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | |
{ | |
JsonProperty property = base.CreateProperty(member, memberSerialization); | |
if (ColorType.IsAssignableFrom(property.PropertyType)) | |
{ | |
property.ShouldSerialize = (instance) => | |
{ | |
// Serialize Color property as long as we're not the default value | |
return property.ValueProvider.GetValue(instance) is Color color | |
&& color != Color.Transparent; | |
}; | |
} | |
return property; | |
} | |
} | |
public class WritablePropertiesOnlyResolver : DefaultContractResolver | |
{ | |
private static Type ColorType = typeof(Color); | |
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) | |
{ | |
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); | |
return props.Where(p => p.Writable).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment