Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJson factory constructor #11

Closed
SunlightBro opened this issue Jan 4, 2023 · 2 comments
Closed

fromJson factory constructor #11

SunlightBro opened this issue Jan 4, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@SunlightBro
Copy link

SunlightBro commented Jan 4, 2023

I really like the factory constructor syntax from json_serializable, to be able to call Model.fromJson(uint8List).
I'm aware this would just be syntactic-sugar with out much value, but could help migrating from json_serializable to crimson, and it could be just optionally.

Example what I mean

If there is a factory constructor that returns _$<className>(buffer) (or some other name convention)

@json
class Model {
  // ...
  factory Model.fromJson(List<int> buffer) => _$Model(buffer);
}

then generate additional extensions like so:

Model _$Model(List<int> buffer) => ModelExt.fromJson(buffer);

extension ModelExt on Model {
  static Model fromJson(List<int> buffer) {
    final crimson = Crimson(buffer);
    return crimson.readModel();
  }

  Uint8List toJson() {
    final writer = CrimsonWriter();
    writer.writeModel(this);
    return writer.toBytes();
  }
}

extension ModelList on List<Model> {
  static List<Model> fromJson(List<int> buffer) {
    final crimson = Crimson(buffer);
    return crimson.readModelList();
  }

  Uint8List toJson() {
    final writer = CrimsonWriter();
    writer.writeModelList(this);
    return writer.toBytes();
  }
}

then you can call

final Model model = Model.fromJson(singleModelUint8List);
final Uint8List foo = model.toJson();

final List<Model> list = ModelList.fromJson(listUint8List);
final Uint8List bar = list.toJson();

If this is something you want I can create a PR.

@simc
Copy link
Owner

simc commented Jan 4, 2023

I really like this idea. I think it would be a great addition 🙏

@simc simc added the enhancement New feature or request label Jan 4, 2023
@SunlightBro
Copy link
Author

@simc After working on it for a while now, the only way to make it work (together with freezed) is to always use @Freezed(fromJson: false, toJson: false)

Example
@Freezed(fromJson: false, toJson: false)
class FJFreezed with _$FJFreezed {
// ignore: invalid_annotation_target
@json
const factory FJFreezed({
  required String name,
  required int age,
}) = _FJFreezed;

factory FJFreezed.fromJson(List<int> buffer) => _$FJFreezedFromJson(buffer);
}

which makes it quite unintuitive to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants