readValueOfType method

Object? readValueOfType(
  1. int type,
  2. ReadBuffer buffer
)

Reads a value of the indicated type from buffer.

The codec can be extended by overriding this method, calling super for types that the extension does not handle. See the discussion at writeValue.

Implementation

Object? readValueOfType(int type, ReadBuffer buffer) {
  switch (type) {
    case _valueNull:
      return null;
    case _valueTrue:
      return true;
    case _valueFalse:
      return false;
    case _valueInt32:
      return buffer.getInt32();
    case _valueInt64:
      return buffer.getInt64();
    case _valueFloat64:
      return buffer.getFloat64();
    case _valueLargeInt:
    case _valueString:
      final int length = readSize(buffer);
      return utf8.decoder.convert(buffer.getUint8List(length));
    case _valueUint8List:
      final int length = readSize(buffer);
      return buffer.getUint8List(length);
    case _valueInt32List:
      final int length = readSize(buffer);
      return buffer.getInt32List(length);
    case _valueInt64List:
      final int length = readSize(buffer);
      return buffer.getInt64List(length);
    case _valueFloat32List:
      final int length = readSize(buffer);
      return buffer.getFloat32List(length);
    case _valueFloat64List:
      final int length = readSize(buffer);
      return buffer.getFloat64List(length);
    case _valueList:
      final int length = readSize(buffer);
      final List<Object?> result = List<Object?>.filled(length, null);
      for (int i = 0; i < length; i++) {
        result[i] = readValue(buffer);
      }
      return result;
    case _valueMap:
      final int length = readSize(buffer);
      final Map<Object?, Object?> result = <Object?, Object?>{};
      for (int i = 0; i < length; i++) {
        result[readValue(buffer)] = readValue(buffer);
      }
      return result;
    default: throw const FormatException('Message corrupted');
  }
}