freezed icon indicating copy to clipboard operation
freezed copied to clipboard

Can't create custom method toJson

Open ProcnerKacper opened this issue 2 years ago • 11 comments

Describe the bug

While creating a freezed class with json_serializable I want to create a custom method toJson but generating freezer code taking always method created by json_serializable in this case _$$_CustomToJson

in generated file custom.freezed.dart

@override
  Map<String, dynamic> toJson() {
    return _$$_CustomToJson(this);
  }

To Reproduce

fluter 3.0.2

dependencies: freezed_annotation: ^2.0.3 json_annotation: ^4.5.0

dev_dependencies: freezed: ^2.0.3+1 json_serializable: ^6.2.0

import 'package:freezed_annotation/freezed_annotation.dart';

part 'custom.freezed.dart';
part 'custom.g.dart';

@freezed
class Custom with _$Custom {
  factory Custom({
    required String id,
    required String name,
  }) = _Custom;

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

  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
      'name': name,
    };
  }
}

also tried with

  @override
  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
      'name': name,
    };
  }

and wit annotation @Freezed(toJson: false) but with this, I can't add toJson without error in freezed file

Expected behavior if defined in class method toJosn should be executed instead of generated one

ProcnerKacper avatar Jun 28 '22 22:06 ProcnerKacper

As specified in the readme, if you want to define methods in your Freezed classes, you need to define an empty private constructor:

@freezed
class Custom with _$Custom {
+  Custom._();
  factory Custom({
    required String id,
    required String name,
  }) = _Custom;

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

  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
      'name': name,
    };
  }
}

rrousselGit avatar Jul 06 '22 14:07 rrousselGit

The main problem is that if someone wants to override toJson() method generated by json_serializable it is not possible without removing part 'custom.g.dart'; to not generate a file and create their own method fromJson() as well

ProcnerKacper avatar Jul 12 '22 20:07 ProcnerKacper

Why so?

rrousselGit avatar Jul 15 '22 12:07 rrousselGit

why does it is happen in this way you ask?

ProcnerKacper avatar Jul 18 '22 18:07 ProcnerKacper

@rrousselGit Can we return dynamic value from toJson . The scenario is while using Unions, I want to return either a Json or a String or an integer based on the type. Is this possible?

prajwal27 avatar Aug 04 '22 03:08 prajwal27

import 'package:freezed_annotation/freezed_annotation.dart';

part 'custom.freezed.dart';
part 'custom.g.dart';

@freezed
class Custom with _$Custom {
  Custom._();
  factory Custom.test1({
    required String id,
    required String name,
  }) = CustomTest1;

  factory Custom.test2({
    required String id,
    required String description,
  }) = CustomTest2;

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

  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
    };
  }
}
Custom custom = CustomTest1(id: 'id', name: 'name');
print('custom: ${custom.toJson()}');

The above code's output was this:

custom: {id: id, name: name, runtimeType: test1}

prajwal27 avatar Aug 04 '22 04:08 prajwal27

I can't get an overriden toJson() to work either, even after defining an empty private constructor.

andcea avatar Aug 07 '22 16:08 andcea

import 'package:freezed_annotation/freezed_annotation.dart';

part 'custom.freezed.dart';
part 'custom.g.dart';

@freezed
class Custom with _$Custom {
  Custom._();
  factory Custom.test1({
    required String id,
    required String name,
  }) = CustomTest1;

  factory Custom.test2({
    required String id,
    required String description,
  }) = CustomTest2;

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

  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
    };
  }
}
Custom custom = CustomTest1(id: 'id', name: 'name');
print('custom: ${custom.toJson()}');

The above code's output was this:

custom: {id: id, name: name, runtimeType: test1}

Which version of freezed are you using? This doesn't work for me

avalanche-tm avatar Aug 15 '22 20:08 avalanche-tm

import 'package:freezed_annotation/freezed_annotation.dart';

part 'custom.freezed.dart';
part 'custom.g.dart';

@freezed
class Custom with _$Custom {
  Custom._();
  factory Custom.test1({
    required String id,
    required String name,
  }) = CustomTest1;

  factory Custom.test2({
    required String id,
    required String description,
  }) = CustomTest2;

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

  Map<String, dynamic> toJson() {
    return {
      'custom_id': id,
    };
  }
}
Custom custom = CustomTest1(id: 'id', name: 'name');
print('custom: ${custom.toJson()}');

The above code's output was this:

custom: {id: id, name: name, runtimeType: test1}

Which version of freezed are you using? This doesn't work for me

Using 2.0.3 version.

prajwal27 avatar Aug 16 '22 14:08 prajwal27

Faced the same problem and so far there is nothing left but to define my own toJson and disable generation via the flag @Freezed(toJson: false), or use another method like toCustomJson within which to reuse the generated one as needed.

PackRuble avatar May 02 '24 07:05 PackRuble