Skip to content

Commit

Permalink
Rename module_identifiers to module_identifiers_by_game
Browse files Browse the repository at this point in the history
Handle fields and nested converters in prop name changed converter
  • Loading branch information
HebaruSan committed Aug 20, 2023
1 parent f0c315d commit f6d439f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
39 changes: 34 additions & 5 deletions Core/Converters/JsonPropertyNamesChangedConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object instance = Activator.CreateInstance(objectType);
var props = objectType.GetTypeInfo().DeclaredProperties.ToList();
JObject jo = JObject.Load(reader);
var changes = mapping;
foreach (JProperty jp in jo.Properties())
Expand All @@ -59,14 +58,44 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
name = jp.Name;
}
PropertyInfo prop = props.FirstOrDefault(pi => pi.CanWrite && (
pi.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName == name
|| pi.Name == name));
prop?.SetValue(instance, jp.Value.ToObject(prop.PropertyType, serializer));
PropertyInfo prop = objectType.GetTypeInfo().DeclaredProperties.FirstOrDefault(pi =>
pi.CanWrite
&& (pi.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName ?? pi.Name) == name);
if (prop != null)
{
prop.SetValue(instance,
GetValue(prop.GetCustomAttribute<JsonConverterAttribute>(),
jp.Value, prop.PropertyType, serializer));
}
else
{
// No property, maybe there's a field
FieldInfo field = objectType.GetTypeInfo().DeclaredFields.FirstOrDefault(fi =>
(fi.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName ?? fi.Name) == name);
if (field != null)
{
field.SetValue(instance,
GetValue(field.GetCustomAttribute<JsonConverterAttribute>(),
jp.Value, field.FieldType, serializer));
}
}
}
return instance;
}

private static object GetValue(JsonConverterAttribute attrib,
JToken value, Type outputType, JsonSerializer serializer)
=> attrib != null ? ApplyConverter((JsonConverter)Activator.CreateInstance(attrib.ConverterType,
attrib.ConverterParameters),
value, outputType, serializer)
: value.ToObject(outputType, serializer);

private static object ApplyConverter(JsonConverter converter,
JToken value, Type outputType, JsonSerializer serializer)
=> converter.CanRead ? converter.ReadJson(new JTokenReader(value),
outputType, null, serializer)
: value.ToObject(outputType, serializer);

/// <summary>
/// This is what you need to override in your child class
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion GUI/Labels/ModuleLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace CKAN.GUI
{
[JsonObject(MemberSerialization.OptIn)]
[JsonConverter(typeof(ModuleIdentifiersRenamedConverter))]
public class ModuleLabel
{
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
Expand Down Expand Up @@ -41,7 +42,7 @@ public class ModuleLabel
[DefaultValue(false)]
public bool HoldVersion;

[JsonProperty("module_identifiers", NullValueHandling = NullValueHandling.Ignore)]
[JsonProperty("module_identifiers_by_game", NullValueHandling = NullValueHandling.Ignore)]
public HashSet<string> ModuleIdentifiers = new HashSet<string>();

/// <summary>
Expand Down Expand Up @@ -72,4 +73,16 @@ public void Remove(string identifier)
ModuleIdentifiers.Remove(identifier);
}
}

/// <summary>
/// Protect old clients from trying to load a file they can't parse
/// </summary>
public class ModuleIdentifiersRenamedConverter : JsonPropertyNamesChangedConverter
{
protected override Dictionary<string, string> mapping
=> new Dictionary<string, string>
{
{ "module_identifiers", "module_identifiers_by_game" }
};
}
}

0 comments on commit f6d439f

Please sign in to comment.