tebru / gson-php

Gson implemented in PHP
Other
151 stars 18 forks source link

Consider adding support for type coercion #31

Open natebrunette opened 6 years ago

mattjanssen commented 5 years ago

Yes, please! Is the only way to currently do this with custom type adapters? Like "StringToFloat" for example?

natebrunette commented 5 years ago

Currently, yes. I was struggling with how to implement it in a natural way. The two options I see are to create a new set of type adapters that do conversions is specific predetermined ways, or to pass a config value to the readers and skip throwing an exception if an unexpected type is found. This will rely on PHP casting the values to the expected value.

Edit: enabling this could conceal errors in the json as well if it can’t be done per field/class. But it gets more complicated to handle it on that level.