fromJson method

RecordBatch fromJson(
  1. Map<String, Object?> json
)

Implementation

RecordBatch fromJson(Map<String, Object?> json) {
  _checkJsonLimits(json);
  try {
    final schemaJson = json['schema'];
    final columnsJson = json['columns'];
    if (schemaJson is! Map || columnsJson is! List) {
      throw const FormatException('Batch requires schema and columns');
    }
    final declaredLength = json['length'];
    if (json.containsKey('length') && declaredLength is! int) {
      throw const FormatException('Batch length must be an integer');
    }
    if (declaredLength is int && declaredLength < 0) {
      throw const FormatException('Batch length must be non-negative');
    }
    final schema = ArrowSchema.fromJson(schemaJson.cast<String, Object?>());
    if (columnsJson.length != schema.fields.length) {
      throw const FormatException('Batch column count does not match schema');
    }
    final columns = <ArrowArray>[];
    for (var i = 0; i < columnsJson.length; i++) {
      final raw = columnsJson[i];
      if (raw is! Map) {
        throw FormatException('columns[$i] must be an object');
      }
      columns.add(
        _columnFromJson(schema.fields[i], raw.cast<String, Object?>()),
      );
    }
    return RecordBatch(schema, columns, rowCount: declaredLength as int?);
  } on FormatException {
    rethrow;
  } on ArgumentError catch (error) {
    throw FormatException('Invalid batch: $error');
  } on TypeError catch (error) {
    throw FormatException('Invalid batch shape: $error');
  }
}