Skip to content

Commit

Permalink
Prevents NullReferenceException on Xamarin iOS
Browse files Browse the repository at this point in the history
When getting the value of a key that’s not present in the JSON, Xamarin
iOS throws a NullReferenceException when T is a value type. The null
check at the start of Extensions.Convert<JToken, T>(token) fails
despite passing a null value in. May be a Xamarin bug or a limitation
of Mono generics running on iOS.

Workaround performs the null check inside JToken.Value method where the
Mono runtime correctly recognises the token as null.
  • Loading branch information
dolbz committed Jan 13, 2014
1 parent 547c3ba commit a24e306
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json/Linq/JToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public virtual T Value<T>(object key)
{
JToken token = this[key];

return Extensions.Convert<JToken, T>(token);
return token == null ? default(T) : Extensions.Convert<JToken, T>(token);
}

/// <summary>
Expand Down

0 comments on commit a24e306

Please sign in to comment.