fromJson method
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 = Schema.fromJson(schemaJson.cast<String, Object?>());
if (columnsJson.length != schema.fields.length) {
throw const FormatException('Batch column count does not match schema');
}
final columns = <Array>[];
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?>()),
);
}
final length =
declaredLength as int? ??
(columns.isEmpty ? 0 : columns.first.length);
if (columns.any((column) => column.length != length)) {
throw const FormatException(
'Batch length does not match column length',
);
}
if (columns.isEmpty) {
return RecordBatch.fromStructArray(
Array.fromBuffers(StructType(const []), length, [null]),
).replaceSchemaMetadata(schema.metadata);
}
return RecordBatch.fromArrays(columns, schema: schema);
} on FormatException {
rethrow;
} on ArgumentError catch (error) {
throw FormatException('Invalid batch: $error');
} on TypeError catch (error) {
throw FormatException('Invalid batch shape: $error');
}
}