dson icon indicating copy to clipboard operation
dson copied to clipboard

class with getter field throws FieldNotFoundException when using fromMap/fromJson

Open copydog opened this issue 5 years ago • 5 comments

I read the generated code, and the SerializableMap's code please allow me using the easiest way

  1. fromMap method will try to assign getter with value, because it uses class mirror fields as the list, and class mirror fields contains getter field.
  2. []= method will throw FieldNotFoundException when unsupported key is given
  3. So, fromMap will throw FieldNotFoundException, when getter field exists.

copydog avatar Mar 12 '19 05:03 copydog

Could you show me your code?

luisvt avatar Mar 13 '19 04:03 luisvt

Sorry, now I understand the error. By the moment you only have three options:

  1. You can add a setter so dson always find it
  2. You add @ignore annotation to the getter and it should be ignored by dson.
  3. You could override the addAll method so it looks something like next:
@serializable
class Person extends _$PersonSerializable {
  int _id;
  int get id => _id;

  @override
  addAll(Map map) => PersonClassMirror.setters.forEach((setter) => this[setter] = map[setter]);
}

I'm going to fix this in future versions.

luisvt avatar Mar 14 '19 05:03 luisvt

I think that the problem still persists. And using @ignore did not work for me. Getters like the following will still get serialized but it fails once object is being reconstructed.

@ignore
bool get isRunning => status == Task.RUNNING;

@ignore
bool get isPaused => status == Task.PAUSED;

HonzaKopecky avatar Oct 03 '19 15:10 HonzaKopecky

@luisvt Can the throwfieldnotfoundexception be removed?

mitty1987 avatar May 01 '20 02:05 mitty1987

What do you mean?

luisvt avatar May 02 '20 02:05 luisvt