From bfab3c77b0c4eeb2d51eb5fd83c8d9bd98f2ffec Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 5 Jun 2020 12:25:52 -0400 Subject: [PATCH] feat: Adding extesion methods for reading a JSON token as a float or a double --- .../Extensions/JsonReaderExtensions.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs index f99299e..82244fa 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -185,6 +185,44 @@ namespace ICD.Common.Utils.Extensions throw new InvalidCastException(message); } + /// + /// Gets the current value as a float. + /// + /// + /// + [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); + } + + /// + /// Gets the current value as a double. + /// + /// + /// + [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); + } + /// /// Gets the current value as a string. ///