bignerdranch / Freddy

A reusable framework for parsing JSON in Swift.
MIT License
1.09k stars 120 forks source link

Type decoding should be safe? #249

Closed mpaneghel closed 7 years ago

mpaneghel commented 7 years ago

In JSONDecodable, all types now can be bridged from one to another, for example a String can be retrieved even if the original value in the JSON was an Int, an Int from an original String value, etc.

This makes Freddy type unsafe and very hard to use when the validity of the data is important.

So here comes the question: shouldn't Freddy throw an error when trying to get types which are clearly not compatible (String->Int), (Int->String) etc instead of just making the conversion behind the scenes?

zwaldowski commented 7 years ago

In #76 and the follow-up PRs, we made the explicit decision to be more lenient in the stock JSONDecodable implementations to allow different versions of a client and server to evolve together. This was a commonly requested feature. There's no representational loss to represent common types as String or convert a String to an Int when it's valid to do so. It's not unsafe. We'd love to hear more about your use case for "when the validity of the data is important".