From 03e0a5f21e9311de53952e7e8e6133eaeb7c2ceb Mon Sep 17 00:00:00 2001 From: Dmytro Koval Date: Thu, 27 Mar 2025 15:51:02 +0200 Subject: [PATCH] JsonConvert update * numeric parser updated --- nanoFramework.Json/JsonConvert.cs | 35 ++++++++++++------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/nanoFramework.Json/JsonConvert.cs b/nanoFramework.Json/JsonConvert.cs index 34722f60..35d37f2e 100644 --- a/nanoFramework.Json/JsonConvert.cs +++ b/nanoFramework.Json/JsonConvert.cs @@ -974,37 +974,28 @@ private static JsonToken ParseValue(ref int jsonPos, ref byte[] jsonBytes, ref L return new JsonValue(double.Parse(token.TValue)); } - // int.MaxValue: 2,147,483,647 - // int.MinValue: -2,147,483,648 - // uint.MaxValue: 4,294,967,295 - // long.MaxValue: 9,223,372,036,854,775,807 - // long.MinValue: -9,223,372,036,854,775,808 - // If we are sure, don't go to the try catch - if (token.TValue.Length < 9) + // trying parse int value from lowest possible to highest + if(int.TryParse(token.TValue, out int _int32Value)) { - return new JsonValue(int.Parse(token.TValue)); + return new JsonValue(_int32Value); } - - if ((token.TValue.Length >= 12) && (token.TValue.Length < 20)) + + if(uint.TryParse(token.TValue, out uint _uint32Value)) { - return new JsonValue(long.Parse(token.TValue)); + return new JsonValue(_uint32Value); } - try + if (long.TryParse(token.TValue, out long _int64Value)) { - return new JsonValue(int.Parse(token.TValue)); + return new JsonValue(_int64Value); } - catch + + if (ulong.TryParse(token.TValue, out ulong _uint64Value)) { - try - { - return new JsonValue(long.Parse(token.TValue)); - } - catch - { - return new JsonValue(ulong.Parse(token.TValue)); - } + return new JsonValue(_uint64Value); } + // if execution goes beyond this point - there were too many digits + throw new DeserializationException(); } if (token.TType == TokenType.True)