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

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

Open
RubyRook opened this issue May 7, 2024 · 1 comment
Open

Comments

@RubyRook
Copy link

RubyRook commented May 7, 2024

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>),
    );
  • The difference is that, why static fromJson generate with DiIcon.fromJson(json['icon'] as Map<String, dynamic>) and factory fromJson generate with DiIcon.fromJson(json['icon']) while both static and factory fromJson have same parameter data type Object? json
  • The problem is that, it no error when i use with factory if json['icon'] not Map<String, dynamic> but it will when i use with static because DiIcon.fromJson(json['icon'] as Map<String, dynamic>)
  • One more thing, it save me a lot of time if compare with implements JsonConverter while my purpose just check json data type and return null is invalid.
@kevmoo
Copy link
Collaborator

kevmoo commented Aug 15, 2024

I'm confused. What exactly do you want?

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

No branches or pull requests

2 participants