k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
400 stars 33 forks source link

defaultValue always overwrite the deserialized value #187

Closed likeconan closed 2 years ago

likeconan commented 2 years ago

Say like we have a class like below with annotation defaultValue:

@jsonSerializable
class Test {
  @JsonProperty(defaultValue: 4)
  int value;
  Test(this.value);
}

Then deserialize it use below code:

var t1 = JsonMapper.deserialize<Test>( '''{"value":12}''');
var t2 = JsonMapper.deserialize<Test>( '''{}''');

the value of t1 is always 4 which is not expected behavior. Expecting value should be 12. the value of t2 is 4 which is correct since the value attribute's value is null.

Could we fix this or do I miss some configuration?

k-paxian commented 2 years ago

Thank you so much for taking your time to report it. On it