simc / crimson

Fast, efficient and easy-to-use JSON parser and serializer for Dart.
Apache License 2.0
229 stars 6 forks source link

Allow nested classes in freezed classes #17

Open buraktabn opened 1 year ago

buraktabn commented 1 year ago

Let's say we have a Tweet class that uses Reply as a field. This works on non freezed classes

@json
class Tweet {
  DateTime? created_at;

  String? tweet;

  Reply? reply;
}

@json
class Reply {
  DateTime? created_at;

  String? reply;
}

When I convert this to freezed class I see that generated readTweet looking for fromJson methods in Reply which not exists.

@freezed
class Tweet with _$Tweet {
  @json
  const factory Tweet({
    DateTime? created_at,
    String? tweet,
    Reply? reply
  }) = _Tweet;
}

@freezed
class Reply with _$Reply {
  @json
  const factory Reply({
    DateTime? created_at,
    String? reply,
  }) = _Reply;
}
// ---- .g ----
extension ReadTweet on Crimson {
  Tweet readTweet() {
    DateTime? created_at;
    String? tweet;
    Reply? reply;

    loop:
    while (true) {
      switch (iterObjectHash()) {
        case -1:
          break loop;

        case -3141059728106243719: // created_at
          created_at = skipNull() ? null : DateTime.parse(readString());
          break;
        case -5949187214024354806: // tweet
          tweet = readStringOrNull();
          break;
        case -777431743796252409: // reply
          reply = Reply.fromJson(read());
          break;

        default:
          skip();
          break;
      }
    }
    ...
  }
  ...
}
k-ane commented 1 year ago

Wondering if this feature exists or is planned already?

Definitely something I need before making the switch over to Crimson

keaganhilliard commented 11 months ago

Seems to work correctly if you remove the json_serializable package or if you add the fromBytes method.