mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-14 20:25:01 +00:00
feat: Adding extesion methods for reading a JSON token as a float or a double
This commit is contained in:
@@ -185,6 +185,44 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
throw new InvalidCastException(message);
|
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>
|
/// <summary>
|
||||||
/// Gets the current value as a string.
|
/// Gets the current value as a string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user