title: Vapor 2 Model to JSON failure
body: <p>Currently have a class that has two nested vars which are struct types. When I post to the controller I have the JSON validated by mapping the post body to the object using the method below:</p>
  
  <pre><code>extension Request {
  func user() throws -&gt; User {
      guard let json = json else { throw Abort.badRequest }
      do {
          return try User(json: json)
      } catch  {
          throw Abort.init(.notAcceptable, metadata: nil, reason: "\(json.wrapped)", identifier: nil, possibleCauses: nil, suggestedFixes: nil, documentationLinks: nil, stackOverflowQuestions: nil, gitHubIssues: nil)
      }
  }
  }
  </code></pre>
  
  <p>Which calls:</p>
  
  <pre><code>extension User: JSONInitializable {
    convenience init(json: JSON) throws {
      try self.init(
          type: json.get("type"),
          person: json.get("person")
      )
    }
  }
  </code></pre>
  
  <p>The problem is that I'm getting a throw error from the Request.user() method. My Model is solid and I've checked the JSON response against the person object. Person at the moment is:</p>
  
  <pre><code>  struct Person {
     var firstName: String
     var lastName: String
     var email: String
     var password: String
     var address: Address 
  }
  
  extension Person: JSONInitializable {
   init(json: JSON) throws {
      try self.init(
          firstName: json.get("firstName"),
          lastName: json.get("lastName"),
          email: json.get("email"),
          password: json.get("password"),
          address: json.get("address")
      )
    }
  }
  </code></pre>
  
  <p>It's very basic so it should pass the mapped validation. Does this struct also need to conform to <code>Vapor Model subclass</code> for it to map correctly or is there something else wrong? </p>
  
  <p>edit: Added User</p>
  
  <pre><code>final class User: Model {
  
  let storage = Storage()
  var type: String
  var person: Person
  
  init(type: String, person: Person) {
      self.type   = type
      self.person = person
  }
  
  //MARK: Parse row from the database
  init(row: Row) throws {
      type = try row.get("type")
      person = try row.get("person")
  }
  
  //MARK: Adding a row to the database
  func makeRow() throws -&gt; Row {
      var row = Row()
      try row.set("person", person)
      try row.set("type", type)
      try row.set("country", person.address.country)
      return row
  }
  
  }
  
  extension User: JSONInitializable {
  
  // MARK: Will be the request in json format
  convenience init(json: JSON) throws {
      try self.init(
          type: json.get("type"),
          person: json.get("person")
  
      )
  }
  }
  
  extension User: Timestampable, SoftDeletable, JSONConvertible {
  
  //MARK: Convert user to json to pass back
  func makeJSON() throws -&gt; JSON {
      var json = JSON()
      try json.set("id", id)
      try json.set("type", type)
      try json.set("person", person)
      return json
  }
  
  }
  
  // Fluent DB Preparation
  extension User: Preparation {
  
  static func prepare(_ database: Database) throws {
      try database.create(self) { builder in
          builder.id()
          builder.string("person")
          builder.string("type")
          builder.string("country")
      }
  }
  
  static func revert(_ database: Database) throws {
      try database.delete(self)
  }
  }
  
  extension User: ResponseRepresentable, NodeRepresentable {}
  </code></pre>