From 0adad154c6845c3952355053520e98208c7a3363 Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Tue, 11 Feb 2020 11:32:03 -0500 Subject: [PATCH] feat: add extensions for getting JsonReader value as long or ulong --- .../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 a824008..6588843 100644 --- a/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonReaderExtensions.cs @@ -141,6 +141,44 @@ namespace ICD.Common.Utils.Extensions JsonToken.Integer); throw new InvalidCastException(message); } + + /// + /// Gets the current value as a long. + /// + /// + /// + [PublicAPI] + public static long GetValueAsLong([NotNull] this JsonReader extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (extends.TokenType == JsonToken.Integer) + return (long)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 an unsigned long. + /// + /// + /// + [PublicAPI] + public static ulong GetValueAsULong([NotNull] this JsonReader extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (extends.TokenType == JsonToken.Integer) + return (ulong)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.