fromJson static method

  1. @Deprecated('No longer supported')
BitmapDescriptor fromJson(
  1. Object json
)

The inverse of .toJson.

Implementation

// TODO(stuartmorgan): Remove this in the next breaking change.
@Deprecated('No longer supported')
static BitmapDescriptor fromJson(Object json) {
  assert(json is List<dynamic>);
  final List<dynamic> jsonList = json as List<dynamic>;
  assert(_validTypes.contains(jsonList[0]));
  switch (jsonList[0]) {
    case _defaultMarker:
      assert(jsonList.length <= 2);
      if (jsonList.length == 2) {
        assert(jsonList[1] is num);
        final num secondElement = jsonList[1] as num;
        assert(0 <= secondElement && secondElement < 360);
        return DefaultMarker(hue: secondElement);
      }
      return const DefaultMarker();
    case _fromBytes:
      assert(jsonList.length == 2);
      assert(jsonList[1] != null && jsonList[1] is List<int>);
      assert((jsonList[1] as List<int>).isNotEmpty);
      return BytesBitmap(byteData: jsonList[1] as Uint8List);
    case _fromAsset:
      assert(jsonList.length <= 3);
      assert(jsonList[1] != null && jsonList[1] is String);
      assert((jsonList[1] as String).isNotEmpty);
      if (jsonList.length == 3) {
        assert(jsonList[2] != null && jsonList[2] is String);
        assert((jsonList[2] as String).isNotEmpty);
        return AssetBitmap(
            name: jsonList[1] as String, package: jsonList[2] as String);
      }
      return AssetBitmap(name: jsonList[1] as String);
    case _fromAssetImage:
      assert(jsonList.length <= 4);
      assert(jsonList[1] != null && jsonList[1] is String);
      assert((jsonList[1] as String).isNotEmpty);
      assert(jsonList[2] != null && jsonList[2] is double);
      if (jsonList.length == 4) {
        assert(jsonList[3] != null && jsonList[3] is List<dynamic>);
        assert((jsonList[3] as List<dynamic>).length == 2);
        final List<dynamic> sizeList = jsonList[3] as List<dynamic>;
        return AssetImageBitmap(
            name: jsonList[1] as String,
            scale: jsonList[2] as double,
            size: Size((sizeList[0] as num).toDouble(),
                (sizeList[1] as num).toDouble()));
      }
      return AssetImageBitmap(
          name: jsonList[1] as String, scale: jsonList[2] as double);
    case AssetMapBitmap.type:
      assert(jsonList.length == 2);
      assert(jsonList[1] != null && jsonList[1] is Map<String, dynamic>);
      final Map<String, dynamic> jsonMap =
          jsonList[1] as Map<String, dynamic>;
      assert(jsonMap.containsKey('assetName'));
      assert(jsonMap.containsKey('bitmapScaling'));
      assert(jsonMap.containsKey('imagePixelRatio'));
      assert(jsonMap['assetName'] is String);
      assert(jsonMap['bitmapScaling'] is String);
      assert(jsonMap['imagePixelRatio'] is double);
      assert(!jsonMap.containsKey('width') || jsonMap['width'] is double);
      assert(!jsonMap.containsKey('height') || jsonMap['height'] is double);
      final double? width =
          jsonMap.containsKey('width') ? jsonMap['width'] as double : null;
      final double? height =
          jsonMap.containsKey('height') ? jsonMap['height'] as double : null;
      return AssetMapBitmap(jsonMap['assetName'] as String,
          bitmapScaling:
              mapBitmapScalingFromString(jsonMap['bitmapScaling'] as String),
          imagePixelRatio: jsonMap['imagePixelRatio'] as double,
          width: width,
          height: height);
    case BytesMapBitmap.type:
      assert(jsonList.length == 2);
      assert(jsonList[1] != null && jsonList[1] is Map<String, dynamic>);
      final Map<String, dynamic> jsonMap =
          jsonList[1] as Map<String, dynamic>;
      assert(jsonMap.containsKey('byteData'));
      assert(jsonMap.containsKey('bitmapScaling'));
      assert(jsonMap.containsKey('imagePixelRatio'));
      assert(jsonMap['byteData'] is Uint8List);
      assert(jsonMap['bitmapScaling'] is String);
      assert(jsonMap['imagePixelRatio'] is double);
      assert(!jsonMap.containsKey('width') || jsonMap['width'] is double);
      assert(!jsonMap.containsKey('height') || jsonMap['height'] is double);
      final double? width =
          jsonMap.containsKey('width') ? jsonMap['width'] as double : null;
      final double? height =
          jsonMap.containsKey('height') ? jsonMap['height'] as double : null;
      return BytesMapBitmap(jsonMap['byteData'] as Uint8List,
          bitmapScaling:
              mapBitmapScalingFromString(jsonMap['bitmapScaling'] as String),
          width: width,
          height: height,
          imagePixelRatio: jsonMap['imagePixelRatio'] as double);
    default:
      break;
  }
  throw ArgumentError('Unrecognized BitmapDescriptor type ${jsonList[0]}');
}