json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Map unrecognized keys

Open imtoori opened this issue 4 years ago • 4 comments

Is there a way to map unrecognized keys to a Map?

For example I know that a json has some properties, but I want to parse also the unrecognized keys in order to implement custom logic depending on those.'

I imagine a property like Map<String, dynamic> otherFields

imtoori avatar Jan 13 '20 12:01 imtoori

No such thing is provided now...an interesting feature request.

As with many of these, I worry about the added complexity.

kevmoo avatar Jan 23 '20 17:01 kevmoo

I solved implementing the feature in the toJson/fromJson methods in my own model, but I think it would be useful to have a built-in feature

imtoori avatar Jan 23 '20 17:01 imtoori

@imtoori how did you implement it?

I also think it would be useful. Now dart code has to know about every field in deserialized resource from API not to accidently null any that he is not using (when sending resource back). It would be great to just have fields that are useful in the class code and rest in the 'otherFields' map.

Brazol avatar Feb 13 '20 11:02 Brazol

You can find an example here

https://github.com/GetStream/stream-chat-dart/tree/master/lib/src/models/message.dart

imtoori avatar Feb 13 '20 11:02 imtoori