Skip to content

why not generate fromJson and toJson functions in the *.g.dart file? #647

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

Closed
TheDevelolper opened this issue May 23, 2020 · 4 comments
Closed

Comments

@TheDevelolper
Copy link

So I'm using your library and the files are generating just fine.

I'm wondering though... I've got the following class:

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import './product.dart';

part 'product_group.g.dart';

@JsonSerializable(nullable: false)
class ProductGroup {
  final List<Product> products;

  ProductGroup({this.products});

  factory ProductGroup.fromJson(Map<String, dynamic> json) =>
      _$ProductGroupFromJson(json);

  Map<String, dynamic> toJson() => _$ProductGroupToJson(this);
}

All my models are going to have the same factory method and toJson methods. Would it not be better to just generate these in the .g.dart file?

@Tinhorn
Copy link

Tinhorn commented May 25, 2020

You might want to pass in another parameter when you are creating from json i.e
Map<String, dynamic> json toJson(Foo foo){ // do something with Foo _$ProductGroupToJson(this); }
It wouldn't hurt to have an option to generate a reasonable default which does

Map<String, dynamic> toJson() => _$ProductGroupToJson(this);

@kevmoo
Copy link
Collaborator

kevmoo commented May 28, 2020

We cannot do this until the language supports something like partial classes - dart-lang/language#252

@kevmoo kevmoo closed this as completed May 28, 2020
@baoxiehao
Copy link

baoxiehao commented May 29, 2020

So I'm using your library and the files are generating just fine.

I'm wondering though... I've got the following class:

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import './product.dart';

part 'product_group.g.dart';

@JsonSerializable(nullable: false)
class ProductGroup {
  final List<Product> products;

  ProductGroup({this.products});

  factory ProductGroup.fromJson(Map<String, dynamic> json) =>
      _$ProductGroupFromJson(json);

  Map<String, dynamic> toJson() => _$ProductGroupToJson(this);
}

All my models are going to have the same factory method and toJson methods. Would it not be better to just generate these in the .g.dart file?

I have the same problem with you. Mostly the default fromJson toJson are working fine. If I should have a different toJson (like fooToJson) I can just name a new method, why not?

But now, I have to write the same code just as the generated code (fromJson/toJson) every time, and it's boring while I'm using the live template.

@TheDevelolper
Copy link
Author

We cannot do this until the language supports something like partial classes - dart-lang/language#252

Oh I assumed that's what the "part" was doing.

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

No branches or pull requests

4 participants