ByteSize.fromJson constructor

ByteSize.fromJson(
  1. Map<String, Object> entityMap
)

Initialize ByteSize object from JSON object according to order of precedence i.e from 'b' -> 'PB', where 'b' has higher precedence to 'KB' and 'MB' has higher precedence to 'GB' and so on.

Implementation

ByteSize.fromJson(Map<String, Object> entityMap) {
  ByteSize bs;

  try {
    if (entityMap.containsKey(_BitSymbol)) {
      bs = FromBits(entityMap[_BitSymbol] is String
          ? int.parse(entityMap[_BitSymbol] as String)
          : entityMap[_BitSymbol] as int);
    } else if (entityMap.containsKey(_ByteSymbol)) {
      bs = FromBytes(entityMap[_ByteSymbol] is String
          ? num.parse(entityMap[_ByteSymbol] as String)
          : entityMap[_ByteSymbol] as int);
    } else if (entityMap.containsKey(_KiloByteSymbol)) {
      bs = FromKiloBytes(entityMap[_KiloByteSymbol] is String
          ? num.parse(entityMap[_KiloByteSymbol] as String)
          : entityMap[_KiloByteSymbol] as int);
    } else if (entityMap.containsKey(_MegaByteSymbol)) {
      bs = FromMegaBytes(entityMap[_MegaByteSymbol] is String
          ? num.parse(entityMap[_MegaByteSymbol] as String)
          : entityMap[_MegaByteSymbol] as int);
    } else if (entityMap.containsKey(_GigaByteSymbol)) {
      bs = FromGigaBytes(entityMap[_GigaByteSymbol] is String
          ? num.parse(entityMap[_GigaByteSymbol] as String)
          : entityMap[_GigaByteSymbol] as int);
    } else if (entityMap.containsKey(_TeraByteSymbol)) {
      bs = FromTeraBytes(entityMap[_TeraByteSymbol] is String
          ? num.parse(entityMap[_TeraByteSymbol] as String)
          : entityMap[_TeraByteSymbol] as int);
    } else if (entityMap.containsKey(_PetaByteSymbol)) {
      bs = FromPetaBytes(entityMap[_PetaByteSymbol] is String
          ? num.parse(entityMap[_PetaByteSymbol] as String)
          : entityMap[_PetaByteSymbol] as int);
    } else {
      throw Exception;
    }

    _clone(bs);
  } on Exception {
    throw FormatException('Json object not in correct format');
  }
}