feat: Adding extesion methods for reading a JSON token as a float or a double

This commit is contained in:
Chris Cameron
2020-06-05 12:25:52 -04:00
parent 4a68c0caad
commit bfab3c77b0

View File

@@ -185,6 +185,44 @@ namespace ICD.Common.Utils.Extensions
throw new InvalidCastException(message);
}
/// <summary>
/// Gets the current value as a float.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static float GetValueAsFloat([NotNull] this JsonReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (extends.TokenType == JsonToken.Float || extends.TokenType == JsonToken.Integer)
return (float)extends.Value;
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value,
JsonToken.Integer);
throw new InvalidCastException(message);
}
/// <summary>
/// Gets the current value as a double.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static double GetValueAsDouble([NotNull] this JsonReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (extends.TokenType == JsonToken.Float || extends.TokenType == JsonToken.Integer)
return (double)extends.Value;
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value,
JsonToken.Integer);
throw new InvalidCastException(message);
}
/// <summary>
/// Gets the current value as a string.
/// </summary>