Skip to content

JsonConvert Numberic parser update #415

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions nanoFramework.Json/JsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading