LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

JsonSerializer.Deserialize errors when encountering Long datatype

Open vidrir opened this issue 10 years ago • 2 comments

Code to reproduce issue:

var reqString = $"{{\"id\": {long.MaxValue} }}";
var test = JsonSerializer.Deserialize(reqString);

The issue can be found in LiteDB\Serializer\Json\JsonReader.cs - ReadValue(JsonToken token). The switch statement chooses JsonTokenType.Number, and because of the lack of ".", tries to convert the value to an Int32. Because the value is too big, it throws and overflow exception.

vidrir avatar Feb 16 '16 16:02 vidrir

By default, JSON specs supports only int. To use long number you need "JSON long representation". {"id":{"$numberLong":"123456789"}}

https://github.com/mbdavid/LiteDB/blob/master/LiteDB/Serializer/Json/JsonReader.cs#L159

mbdavid avatar Feb 16 '16 16:02 mbdavid

Unfortunately, this solution is not very convenient in practice. In particular, now I have a need to save in DB json which I receive from the server as a string. And now, unexpectedly, I began to get a System.OverflowException.

It would be much better if the already built-in JsonSerializer could automatically use int64 if the value does not fit in int32. And double if the value does not fit in int64. And do not throw an exception.

But since this is not the case, I will have to write "my" JsonSerializer which can do this.

mikhail-khalizev avatar May 01 '20 14:05 mikhail-khalizev