3

Minimal reproducible code:

@JsonSerializable()
class A {
  final int _x;
  A(int x) : _x = x;

  factory A.fromJson(Map<String, dynamic> json) => _$AFromJson(json);
}

Note:

I don't want to make my private field _x public or define a public x getter.

iDecode
  • 22,623
  • 19
  • 99
  • 186
  • I read [this post](https://github.com/google/json_serializable.dart/issues/537) but it's not using a constructor. – iDecode Nov 13 '22 at 10:49

2 Answers2

1

This PR addresses what you want: https://github.com/google/json_serializable.dart/pull/1256/files#diff-0acaf4c472e452d1e5d215a15fcd2266ccd02ab6abdfac0080c2fca845eb9096

You will be able to explicitly set includeFromJson and includeToJson on the private fields you want to include.

Example:

class X {   
  @JsonKey(includeFromJson: true, includeToJson: true)
  int _includeMeToTheJsonParsing;
}

It was merged November 30th. Latest package version is v6.5.4, released at October 25th. So you will need to wait a little bit if you want the official release. Otherwise, you can point directly to the latest commit if you need it ASAP.

Gabriel Gava
  • 571
  • 4
  • 10
  • Could you also write the code for the same? – iDecode Dec 27 '22 at 19:40
  • What do you mean? – Gabriel Gava Dec 28 '22 at 20:14
  • Are you asking me how to use `includeFromJson` and `includeToJson`? Or to deal with the problem on the version 6.5.4? – Gabriel Gava Dec 29 '22 at 16:28
  • Yes, I want to know how I will use the `includeFromJson` and `includeToJson`. – iDecode Dec 29 '22 at 19:35
  • I wish you had provided the code as this question had a bounty. But since, the top answer is completely wrong, and it will get 25 reps automatically if don't do anything, I'm awarding 50 to you. – iDecode Jan 03 '23 at 21:15
  • I'm sorry, I forgot to answer you. Since it is not yet live, I cannot be sure if this is correct, but I assume it will look like this: `class X { @JsonKey(includeFromJson: true, includeToJson: true) int _includeMeToTheJsonParsing; }` Is that what you are looking for? Added same code to the response for better readability. – Gabriel Gava Jan 10 '23 at 09:26
0

If you don't want to make your private field public or define a public getter, you can still use a serialization library like json_serializable or built_value to serialize and deserialize the class, but you will need to define a custom toJson method that manually serializes the private field. Here's an example of using json_serializable with a custom toJson method:

import 'package:json_annotation/json_annotation.dart';

part 'my_class.g.dart';

@JsonSerializable()
class MyClass {
  final int _x;

  MyClass(this._x);

  Map<String, dynamic> toJson() => {
    'x': _x,
  };

  factory MyClass.fromJson(Map<String, dynamic> json) => MyClass(json['x'] as int);
}

You can then use the fromJson and toJson methods to serialize and deserialize your class:

import 'dart:convert';

void main() {
  // Serialize to JSON
  MyClass obj = MyClass(42);
  String json = jsonEncode(obj);

  // Deserialize from JSON
  MyClass obj2 = MyClass.fromJson(jsonDecode(json));
}
abhinavsinghvirsen
  • 1,853
  • 16
  • 25