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.54k stars 392 forks source link

factory fromJson and static fromJson provide two different g.dart result #1425

Open RubyRook opened 2 months ago

RubyRook commented 2 months ago

I want to make code much shorter without use JsonConverter. While factory fromJson cannot return null I need to use static fromJson instead. But, g.dart file didn't provide what i want.

Example with factory: `import 'package:json_annotation/json_annotation.dart';

part 'di.category.g.dart';

@JsonSerializable(explicitToJson: true,) class DiCategory { String id; String en_name; String km_name; String slug; String parent; bool popular; DiIcon? icon;

DiCategory({ required this.id, required this.en_name, required this.km_name, required this.slug, required this.parent, required this.popular, this.icon, });

factory DiCategory.fromJson(Map<String, dynamic> json) => $DiCategoryFromJson(json); Map<String, dynamic> toJson()=> $DiCategoryToJson(this); }

@JsonSerializable() class DiIcon { String url; String width; String height;

DiIcon({ required this.url, required this.width, required this.height, });

factory DiIcon.fromJson(Object? json) => $DiIconFromJson(json is Map<String, dynamic> ? json:{}); Map<String, dynamic> toJson()=> $DiIconToJson(this); }`

Inside di.category.g.dart DiCategory _$DiCategoryFromJson(Map<String, dynamic> json) => DiCategory( id: json['id'] as String, en_name: json['en_name'] as String, km_name: json['km_name'] as String, slug: json['slug'] as String, parent: json['parent'] as String, popular: json['popular'] as bool, icon: json['icon'] == null ? null : DiIcon.fromJson(json['icon']), );

Example with static: `import 'package:json_annotation/json_annotation.dart';

part 'di.category.g.dart';

@JsonSerializable(explicitToJson: true,) class DiCategory { String id; String en_name; String km_name; String slug; String parent; bool popular; DiIcon? icon;

DiCategory({ required this.id, required this.en_name, required this.km_name, required this.slug, required this.parent, required this.popular, this.icon, });

factory DiCategory.fromJson(Map<String, dynamic> json) => $DiCategoryFromJson(json); Map<String, dynamic> toJson()=> $DiCategoryToJson(this); }

@JsonSerializable() class DiIcon { String url; String width; String height;

DiIcon({ required this.url, required this.width, required this.height, });

static DiIcon? fromJson(Object? json) => json is Map<String, dynamic> ? $DiIconFromJson(json):null; Map<String, dynamic> toJson()=> $DiIconToJson(this); } ` Inside di.category.g.dart

DiCategory _$DiCategoryFromJson(Map<String, dynamic> json) => DiCategory( id: json['id'] as String, en_name: json['en_name'] as String, km_name: json['km_name'] as String, slug: json['slug'] as String, parent: json['parent'] as String, popular: json['popular'] as bool, icon: json['icon'] == null ? null : DiIcon.fromJson(json['icon'] as Map<String, dynamic>), );