public Player(dynamic obj) { Id = ChoUtility.CastTo <int>(obj.Id); Sea = ChoUtility.CastTo <int>(obj.Sea); First = obj.First; Last = obj.Last; Team = obj.Team; Coll = obj.Coll; Num = ChoUtility.CastTo <int>(obj.Num); Age = ChoUtility.CastTo <int>(obj.Age); Hgt = ChoUtility.CastTo <int>(obj.Hgt); Wgt = ChoUtility.CastTo <int>(obj.Wgt); Pos = obj.Pos; Flg = String.IsNullOrEmpty(obj.Flg) ? "None" : obj.Flg; Trait = String.IsNullOrEmpty(obj.Trait) ? "None" : obj.Trait; Attr = new PlayerAttr(); Attr.Str = ChoUtility.CastTo <int>(obj.Attr_Str); Attr.Agi = ChoUtility.CastTo <int>(obj.Attr_Agi); Per = new PlayerPer(); Per.Lea = ChoUtility.CastTo <int>(obj.Per_Lea); Per.Wor = ChoUtility.CastTo <int>(obj.Per_Wor); Skills = new PlayerSkills(); Skills.WR = ChoUtility.CastTo <int>(obj.Skills_WR); Skills.TE = ChoUtility.CastTo <int>(obj.Skills_TE); }
/// <inheritdoc /> public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); object targetObj = Activator.CreateInstance(objectType); dynamic ctx = serializer.Context.Context; StringComparison comparision = ctx != null? ChoUtility.CastTo <StringComparison>(ctx.StringComparision, StringComparison.InvariantCultureIgnoreCase) : StringComparison.InvariantCultureIgnoreCase; foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite)) { string jsonPath = null; ChoJSONPathAttribute att1 = prop.GetCustomAttributes(true) .OfType <ChoJSONPathAttribute>() .FirstOrDefault(); if (att1 != null && !att1.JSONPath.IsNullOrWhiteSpace()) { jsonPath = att1.JSONPath; } else if (att1 == null) { JsonPropertyAttribute att = prop.GetCustomAttributes(true) .OfType <JsonPropertyAttribute>() .FirstOrDefault(); jsonPath = att != null ? att.PropertyName : null; // prop.Name; } if (serializer.ContractResolver is DefaultContractResolver) { var resolver = (DefaultContractResolver)serializer.ContractResolver; jsonPath = resolver.GetResolvedPropertyName(jsonPath); } JToken token = jsonPath.IsNullOrWhiteSpace() ? jo.GetProperty(prop.Name, comparision) : jo.SelectToken(jsonPath); if (token != null && token.Type != JTokenType.Null) { object value = token.ToObject(prop.PropertyType, serializer); prop.SetValue(targetObj, value, null); } } return(targetObj); }