fromJson static method

Operation fromJson(
  1. Map data, {
  2. DataDecoder? dataDecoder,
})

Creates new Operation from JSON payload.

If dataDecoder parameter is not null then it is used to additionally decode the operation's data object. Only applied to insert operations.

Implementation

static Operation fromJson(Map data, {DataDecoder? dataDecoder}) {
  dataDecoder ??= _passThroughDataDecoder;
  final map = Map<String, dynamic>.from(data);
  if (map.containsKey(Operation.insertKey)) {
    final data = dataDecoder(map[Operation.insertKey]);
    final dataLength = data is String ? data.length : 1;
    return Operation(
        Operation.insertKey, dataLength, data, map[Operation.attributesKey]);
  } else if (map.containsKey(Operation.deleteKey)) {
    final int? length = map[Operation.deleteKey];
    return Operation(Operation.deleteKey, length, '', null);
  } else if (map.containsKey(Operation.retainKey)) {
    final int? length = map[Operation.retainKey];
    return Operation(
        Operation.retainKey, length, '', map[Operation.attributesKey]);
  }
  throw ArgumentError.value(data, 'Invalid data for Delta operation.');
}