Repository navigation
Expand file tree
/
Copy pathparams.json
More file actions
1 lines (1 loc) · 2.91 KB
/
Copy pathparams.json
File metadata and controls
1 lines (1 loc) · 2.91 KB
1
{"name":"ObjectMapper","tagline":"JSON Object mapping written in Swift","body":"ObjectMapper is a framework written in Swift that makes it easy for you to convert your Model objects to and from JSON. \r\n\r\nFeatures:\r\n- Mapping JSON to objects\r\n- Mapping objects to JSON\r\n- Nested Objects (stand alone, in Arrays or in Dictionaries)\r\n- Custom transformations during mapping\r\n\r\nTo support mapping, a class just needs to implement the MapperProtocol. ObjectMapper uses the \"<=\" operator to define how each member variable maps to and from JSON.\r\n\r\n```swift\r\nclass User: MapperProtocol {\r\n\r\n var username: String?\r\n var age: Int?\r\n var weight: Double?\r\n var arr: [AnyObject]?\r\n var dict: [String : AnyObject] = [:]\r\n var friend: User?\r\n var birthday: NSDate?\r\n\r\n // MapperProtocol \r\n class func map(mapper: Mapper, object: User) {\r\n object.username <= mapper[\"username\"]\r\n object.age <= mapper[\"age\"]\r\n object.weight <= mapper[\"weight\"]\r\n object.arr <= mapper[\"arr\"]\r\n object.dict <= mapper[\"dict\"]\r\n object.friend <= mapper[\"friend\"]\r\n object.birthday <= (mapper[\"birthday\"], DateTransform<NSDate, Int>())\r\n }\r\n}\r\n```\r\n\r\nOnce your class implements MapperProtocol, the Mapper class handles everything else for you:\r\n\r\nConvert a JSON string to a model object:\r\n```swift\r\nlet user = Mapper().map(JSONString, to: User.self)\r\n```\r\n\r\nConvert a model object to a JSON string:\r\n```swift\r\n\r\nlet JSONString = Mapper().toJSONString(user)\r\n```\r\n\r\nObject mapper can handle classes composed of the following types:\r\n- Int\r\n- Bool\r\n- Double\r\n- Float\r\n- String\r\n- Array\\<AnyObject\\>\r\n- Dictionary\\<String, AnyObject\\>\r\n- Optionals of all the abovee\r\n- Object\\<T: MapperProtocol\\>\r\n- Array\\<T: MapperProtocol\\>\r\n- Dictionary\\<String, T: MapperProtocol\\>\r\n\r\nObjectMapper also supports Transforms that convert values during the mapping process. To use a transform, simply create a tuple with the mapper[\"field_name\"] and the transform of choice on the right side of the '<=' operator:\r\n```swift\r\nobject.birthday <= (mapper[\"birthday\"], DateTransform<NSDate, Int>())\r\n```\r\nThe above transform will convert the JSON Int value to an NSDate when reading JSON and will convert the NSDate to an Int when converting objects to JSON.\r\n\r\nYou can easily create your own custom transforms by subclassing and overriding the methods in the MapperTransform class:\r\n```swift\r\npublic class MapperTransform<ObjectType, JSONType> {\r\n init(){\r\n\r\n }\r\n\r\n func transformFromJSON(value: AnyObject?) -> ObjectType? {\r\n return nil\r\n }\r\n\r\n func transformToJSON(value: ObjectType?) -> JSONType? {\r\n return nil\r\n }\r\n}\r\n```\r\n\r\n","google":"","note":"Don't delete this file! It's used internally to help with page regeneration."}