google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 397 forks source link

[Feature request] compute support #1346

Open jopmiddelkamp opened 1 year ago

jopmiddelkamp commented 1 year ago

Describe the solution you'd like I'm using freezed in the example but in the background that is using this package.

I would like to have generated option for json serialization via isolates.

So that this:

@freezed
class DisbursementEntity with _$DisbursementEntity {
  const factory DisbursementEntity({
    required String asset,
    required String disbursementPlatformDomain,
    required String organizationName,
    required String signature,
  }) = _DisbursementEntity;

  static Future<DisbursementEntity> fromJsonStringCompute(
    String jsonString,
  ) async {
    final result = await compute(
      (jsonString) => DisbursementEntity.fromJson(jsonDecode(jsonString)),
      jsonString,
    );
    return result;
  }

  factory DisbursementEntity.fromJson(Map<String, dynamic> json) =>
      _$DisbursementEntityFromJson(json);
}

extension DisbursementEntityX on DisbursementEntity {
  Future<String> toJsonStringCompute() async {
    final result = await compute(
      (disbursement) => jsonEncode(disbursement.toJson()),
      this,
    );
    return result;
  }
}

Can become this:

@freezed
class DisbursementEntity with _$DisbursementEntity {
  const factory DisbursementEntity({
    required String asset,
    required String disbursementPlatformDomain,
    required String organizationName,
    required String signature,
  }) = _DisbursementEntity;

  static Future<DisbursementEntity> fromJsonStringCompute(String json)  =>
      _$DisbursementEntityFromJsonStringCompute(json);

  factory DisbursementEntity.fromJson(Map<String, dynamic> json) =>
      _$DisbursementEntityFromJson(json);
}