title: How to create JSON from Model in ios Swift 3
body: <p>I have created model class for the user as below.</p>
  
  <pre><code>public class SignUpUser {
      public var fullName : String?
      public var id : Int?
      public var city : String?
      public var email : String?
      public var address : String?
      public var lastName : String?
      public var countryCode : String?
      public var firstName : String?
      public var zipCode : Int?
      public var contactNumber : Int?
      public var sex : String?
      public var dob : String?
      public var signupType : String?
      public var verified : String?
      public var emailTokenExpiration : String?
      public var updatedAt : String?
      public var createdAt : String?
  
  /**
      Returns an array of models based on given dictionary.
  
      Sample usage:
      let user_list = User.modelsFromDictionaryArray(someDictionaryArrayFromJSON)
  
      - parameter array:  NSArray from JSON dictionary.
  
      - returns: Array of User Instances.
  */
      public class func modelsFromDictionaryArray(array:NSArray) -&gt; [SignUpUser]
      {
          var models:[SignUpUser] = []
          for item in array
          {
              models.append(SignUpUser(dictionary: item as! NSDictionary)!)
          }
          return models
      }
  
  /**
      Constructs the object based on the given dictionary.
  
      Sample usage:
      let user = User(someDictionaryFromJSON)
  
      - parameter dictionary:  NSDictionary from JSON.
  
      - returns: User Instance.
  
  */
  
      init?() {}
  
  
      required public init?(dictionary: NSDictionary) {
          fullName = dictionary["fullName"] as? String
          id = dictionary["id"] as? Int
          city = dictionary["city"] as? String
          email = dictionary["email"] as? String
          address = dictionary["address"] as? String
          lastName = dictionary["lastName"] as? String
          countryCode = dictionary["countryCode"] as? String
          firstName = dictionary["firstName"] as? String
          zipCode = dictionary["zipCode"] as? Int
          contactNumber = dictionary["contactNumber"] as? Int
          sex = dictionary["sex"] as? String
          dob = dictionary["dob"] as? String
          signupType = dictionary["signupType"] as? String
          verified = dictionary["verified"] as? String
          emailTokenExpiration = dictionary["emailTokenExpiration"] as? String
          updatedAt = dictionary["updatedAt"] as? String
          createdAt = dictionary["createdAt"] as? String
      }
  
  
  /**
      Returns the dictionary representation for the current instance.
  
      - returns: NSDictionary.
  */
      public func dictionaryRepresentation() -&gt; NSDictionary {
  
          let dictionary = NSMutableDictionary()
  
          dictionary.setValue(self.fullName, forKey: "fullName")
          dictionary.setValue(self.id, forKey: "id")
          dictionary.setValue(self.city, forKey: "city")
          dictionary.setValue(self.email, forKey: "email")
          dictionary.setValue(self.address, forKey: "address")
          dictionary.setValue(self.lastName, forKey: "lastName")
          dictionary.setValue(self.countryCode, forKey: "countryCode")
          dictionary.setValue(self.firstName, forKey: "firstName")
          dictionary.setValue(self.zipCode, forKey: "zipCode")
          dictionary.setValue(self.contactNumber, forKey: "contactNumber")
          dictionary.setValue(self.sex, forKey: "sex")
          dictionary.setValue(self.dob, forKey: "dob")
          dictionary.setValue(self.signupType, forKey: "signupType")
          dictionary.setValue(self.verified, forKey: "verified")
          dictionary.setValue(self.emailTokenExpiration, forKey: "emailTokenExpiration")
          dictionary.setValue(self.updatedAt, forKey: "updatedAt")
          dictionary.setValue(self.createdAt, forKey: "createdAt")
  
          return dictionary
      }
  
  }
  </code></pre>
  
  <p>I am trying to conver the object to JSON with following way but getting error saying <strong>"invalid top-level type in json write"</strong></p>
  
  <pre><code>let signUpuser =  SignUpUser()
          signUpuser?.fullName = "Teswt"
          signUpuser?.id = 1
          signUpuser?.city = "Test"
          signUpuser?.email = "Test"
          signUpuser?.address = "Test"
          signUpuser?.lastName = "Test"
          signUpuser?.countryCode = "Test"
          signUpuser?.firstName = "Test"
          signUpuser?.zipCode = 380004
          signUpuser?.contactNumber = 12345
          signUpuser?.sex = "Test"
          signUpuser?.dob = "Test"
          signUpuser?.signupType = "Test"
          signUpuser?.verified = "Test"
          signUpuser?.emailTokenExpiration = "Test"
          signUpuser?.updatedAt = "Test"
          signUpuser?.createdAt = "Test"
  
  if let jsonData = try? JSONSerialization.data(withJSONObject: signUpuser, options: []) {
              let theJSONText = String(data: jsonData, encoding: .utf8)
              AppLog.debug(tag: TAG, msg: theJSONText!)
          }
  </code></pre>
  
  <p>In Android using Google's gson library we can easily convert JSON to Object and vice versa but in iOS it seems bit difficult.</p>
  
  <p>I also tried to wrap the SignupUser object inside other class object but no luck.</p>
  
  <p><strong>"Wrapping inside other class..."</strong></p>
  
  <pre><code>let wrapperObject = JSONServerRequest(data: signUpuser)
          if let jsonData = try? JSONSerialization.data(withJSONObject: wrapperObject, options: []) {
              let theJSONText = String(data: jsonData, encoding: .utf8)
              AppLog.debug(tag: TAG, msg: theJSONText!)
          }
  </code></pre>
  
  <p>I don't wish to do this with Dictionary as I have to write the keys every time, I prefer to work with objects so If anyone has any clue please kindly guide me.</p>