simplified JsonPathConverter

This commit is contained in:
Ryan Peters 2023-03-19 01:58:18 +00:00
parent 9add92c170
commit cf46c2d550

View File

@ -13,12 +13,9 @@ namespace WeatherDashboard
foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite)) foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite))
{ {
var attribute = prop var attribute = prop.GetCustomAttribute<JsonPropertyAttribute>(true);
.GetCustomAttributes(true)
.OfType<JsonPropertyAttribute>() var jsonPath = attribute != null ? attribute.PropertyName : prop.Name;
.FirstOrDefault();
var jsonPath = (attribute != null ? attribute.PropertyName : prop.Name);
var token = jsonObject.SelectToken(jsonPath); var token = jsonObject.SelectToken(jsonPath);
if (token != null && token.Type != JTokenType.Null) if (token != null && token.Type != JTokenType.Null)
@ -30,6 +27,7 @@ namespace WeatherDashboard
return target; return target;
} }
// CanConvert is not called when [JsonConverter] attribute is used // CanConvert is not called when [JsonConverter] attribute is used
public override bool CanConvert(Type objectType) => false; public override bool CanConvert(Type objectType) => false;
@ -37,4 +35,4 @@ namespace WeatherDashboard
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
} }
} }