Objectmapper: Opinions wanted: ObjectMapper vs Codable

Created on 11 Oct 2018  路  10Comments  路  Source: tristanhimmelman/ObjectMapper

Hi all, I am the developer of this project and I have a question for you all.

Over the last few years I have not been actively developing on iOS/macOS (only supporting this project). As such, I have not had the opportunity to work closely with Codable to understand how it differs from ObjectMapper. This project started in the early days of Swift before Codable existed. Since supporting this project is a fair bit of work so I am hoping to understand how ObjectMapper compares to Codable. What use cases does ObjectMapper handle for you that you cannot handle with Codable? Are there any pros/cons to either tool? Which library do you prefer?

Thanks for sharing your input. This will help me evaluate whether it makes sense to continue supporting this tool long term.

help wanted question

Most helpful comment

There are a lot of applications using ObjectMapper and many developers prefer it over Codable. ObjectMapper is still better for applying custom transforms. Getting the json from the Mappable object is another plus. Not failing the whole object when there is one key missing in the response whether the property is Optional or Non-optional is still not possible with Codable, it will fail the whole thing if you don't write your own encoders which is extremely painful to write for every model.

It is a huge effort to replace ObjectMapper with Codable in larger projects so it should be supported even if we have to introduce major changes in ObjectMapper to support the latest Swift syntax.

All 10 comments

I don't really like codable now because it's not flexible. For example, the null field problem. Maybe it's because I can't use it.

There are a lot of applications using ObjectMapper and many developers prefer it over Codable. ObjectMapper is still better for applying custom transforms. Getting the json from the Mappable object is another plus. Not failing the whole object when there is one key missing in the response whether the property is Optional or Non-optional is still not possible with Codable, it will fail the whole thing if you don't write your own encoders which is extremely painful to write for every model.

It is a huge effort to replace ObjectMapper with Codable in larger projects so it should be supported even if we have to introduce major changes in ObjectMapper to support the latest Swift syntax.

@kamirana4 positive solution!

I can't say anything about Codable (haven't used it yet) but wanted to say thx for objectmapper and that I would love to see continuous support.

Not failing the whole object when there is one key missing in the response whether the property is Optional or Non-optional is still not possible with Codable, it will fail the whole thing if you don't write your own encoders which is extremely painful to write for every model.

@kamirana4 I've just migrated my models to Codable some days ago and as far I could not see this issue. If a property has Optional type it's not necessary to write custom init(from decoder: Decoder) method. JSONDecoder will use automatically decodeIfPresent for it.

@tristanhimmelman I think that ObjectMapper still has some advantages:

  • it's said to be faster than Codable (or more precisely than JSONEncoder/Decoder)
  • it has type transformers support (but for Codable we have the external library for that https://github.com/jamesruston/CodableExtensions)

What's better in Codable?

  • less code (in most cases it's enough to declare properties, without json - property mapping)
  • builtin support for snake case <> camel case names conversion (JSONDecoder.keyDecodingStrategy)

There are probably plenty of apps based on ObjectMapper, so it may be better to keep at least some support for it. But IMO it's not worth to add new features now.

BTW: Object Mapper was the best model mapper before Codable! Thank you for your hard work :)

Thanks for the feedback everyone. I will do my best to keep the project up and running smoothly

Not failing the whole object when there is one key missing in the response whether the property is Optional or Non-optional is still not possible with Codable, it will fail the whole thing if you don't write your own encoders which is extremely painful to write for every model.

@kamirana4 I've just migrated my models to Codable some days ago and as far I could not see this issue. If a property has Optional type it's not necessary to write custom init(from decoder: Decoder) method. JSONDecoder will use automatically decodeIfPresent for it.

Yeah! It will fail if the property is declared Non-Optional. For Optional it won't fail, my mistake.

One of the advantages of Codable is that basic types (Int, String, Array...) are also Codable, so when working with generic types you dont have to worry about multiple type format.
Put that aside I think Objectmapper makes cleaner code and error handeling is much more easier.
Also with Objectmapper you can easily define default values for properties that are not present in the Json.
Thanks for your hardwork, Apple, Swift and almost every ios developer owe you big time.

I prefer ObjectMapper to Codable because ObjectMapper is easier more readable, easier to use when parsing complex data. Hope you and other contributors will continue this project for a long time. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

quetool picture quetool  路  3Comments

danyalaytekin picture danyalaytekin  路  4Comments

YunyueLin picture YunyueLin  路  3Comments

patchthecode picture patchthecode  路  3Comments

borut-t picture borut-t  路  4Comments