Bir yöntem uygulayarak bir json dizesine göre List<string>
döndürür.deserialize empty Bir Dizgiye Dize <string>
Boş bir dizeyi seri hale getirmeye çalıştığımı fark ettim. Kilitlenmez, bir istisna kaldırmaz. Boş bir List<string>
yerine null
değerini döndürür.
Sorun, List<string>
yerine null
değerini döndürmek için ne dokunabilirim?
return JsonConvert.DeserializeObject(content, typeof(List<string>));
DÜZENLEME Jenerik yöntemi: NullValueHandling
için ayarlayarak Ayrıca yapabileceğini
return JsonConvert.DeserializeObject(content, typeof(List<string>)) ?? new List<string>();
:
public object Deserialize(string content, Type type) {
if (type.GetType() == typeof(Object))
return (Object)content;
if (type.Equals(typeof(String)))
return content;
try
{
return JsonConvert.DeserializeObject(content, type);
}
catch (IOException e) {
throw new ApiException(HttpStatusCode.InternalServerError, e.Message);
}
}
'type.GetType()' is; System.Type 'dan istediğiniz gibi olmayan bazı somut tipler verecektir. Eğer istiyorsan (type == typeof (Object)). Bir sonraki '' '' de '==' de kullanabilirsiniz (tutarlılık için). –