decodeNumber method Null safety
- dynamic src
Decode a number
Implementation
NumberValue decodeNumber(dynamic src) {
var p = src is GsonParsable
? src
: src is String
? GsonParsable(src)
: throw ('The src is not a valid input to decode an Array from');
if (!RegExp(r'[0-9\.]').hasMatch(p.actual())) {
throw p.error('Any number has to start with a number between 0 and 9');
}
var number = '';
while (RegExp(r'[0-9\.]').hasMatch(p.actual())) {
number += p.next();
}
NumberValue ret;
switch (p.actual()) {
case 'b':
ret = Byte(int.parse(number));
if (!p.ended) {
p.skip();
}
break;
case 's':
ret = Short(int.parse(number));
if (!p.ended) {
p.skip();
}
break;
case 'l':
ret = Long(int.parse(number));
if (!p.ended) {
p.skip();
}
break;
case 'f':
ret = Float(double.parse(number));
if (!p.ended) {
p.skip();
}
break;
case 'd':
ret = Double(double.parse(number));
if (!p.ended) {
p.skip();
}
break;
default:
if (number.contains('.')) {
ret = Double(double.parse(number));
} else {
ret = Integer(int.parse(number));
}
break;
}
return ret;
}