title: How to make the RealmSwift RealmOptional compatible with Swift Codable?
body: <p>Im facing an issue where I can't make the RealmOptional compatible with swift new Codable feature with json decoder.</p>
  
  <p>Cosider the following Realm object.</p>
  
  <pre><code>class School: Object, Codable {
  
      @objc dynamic var id: Int64 = 0
  
      @objc dynamic var name: String?
      var numberOfStudents = RealmOptional&lt;Int64&gt;()
      var classes = List&lt;Class&gt;()
  
      enum CodingKeys: String, CodingKey {
         case id
         case name
         case numberOfStudents
         case classes
      }
  }
  
  class Class: Object, Codable {
      var name: String?
      var numberOfStudents = RealmOptional&lt;Int64&gt;()
  }
  </code></pre>
  
  <p>Here we can declare the class as Codable because I wrote an extension for RealmOptinal with the help of <a href="https://gist.github.com/mishagray/3ee82a3a82f357bfbf8ff3b3d9eca5cd" rel="noreferrer">this gist</a>. But the problem is when the decoder decodes the json.</p>
  
  <p>Consider this json</p>
  
  <pre><code>let jsonData = """
  [
      "id": 1234,
      "name": "Shreesha",
      "numberOfStudents": nil,
      "classes": {
         "name": "Class V",
         "numberOfStudents": 12
      }
  ]
  """.data(using: .utf8)!
  </code></pre>
  
  <p>In this json all the data are passed and this decodes perfectly with the code.</p>
  
  <pre><code>let decoder = JSONDecoder()
  
  let decoded = try! decoder.decode(School.self, from: jsonData)
  </code></pre>
  
  <p>But if I remove the <code>numberOfStudents</code> key from the json data which supposed to be a RealmOptional object it will throw an error and it will not decode because RealmOptional is not a swift optional so the decoder thinks that there should be a key in the json data. In <code>JSONDecoder</code> it doesn't try to decode if the key is not there in the json and the property is declared as optional. It simply skips to other keys.</p>
  
  <p>Until now I didn't override the initialiser because we had all the supporting extensions for <code>RealmOptional</code> Realm <code>Lists</code> etc. But now I have to override the <code>init(from decoder: Decoder)</code> to decode it manually and the Realm model has more than <strong>50</strong> properties in it (You know what I mean). </p>
  
  <p>If we override the initialiser I feel there is not point in using <code>JSONDecoder</code> because there is more manual work than using JSONDecoder.</p>
  
  <pre><code>required convenience init(from decoder: Decoder) throws {
      self.init()
      let container = try decoder.container(keyedBy: CodingKeys.self)
  
      id = try container.decodeIfPresent(Int64.self, forKey: .id) ?? 0
      name = try container.decodeIfPresent(String?.self, forKey: .name) ?? ""
      numberOfStudents = try container.decodeIfPresent(RealmOptional&lt;Int64&gt;.self, forKey: .numberOfStudents) ?? RealmOptional&lt;Int64&gt;()
  
      let classesArray = try container.decode([Class].self, forKey: .classes)
      classes.append(objectsIn: classesArray)
  }
  </code></pre>
  
  <p>So can someone suggest me the alternate solution to make the RealmOptional compatible with <code>JSONDecoder</code> so that we don't have to override the initialisers.</p>