tristanhimmelman /ObjectMapper
Simple JSON Object mapping written in Swift
★ 9,119⑂ 1,024👁 210⑃ 338● 811▤ 60
+0
日增长
9,119
stars
1,024
forks
210
watchers
338
prs
811
issues
60
releases
README
ObjectMapper
============
ObjectMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from JSON.
- Features
- The Basics
- Mapping Nested Objects
- Custom Transformations
- Subclassing
- Generic Objects
- Mapping Context
- ObjectMapper + Alamofire
- ObjectMapper + Realm
- Projects using ObjectMapper
- To Do
- Contributing
- Installation
Features:
- Mapping JSON to objects
- Mapping objects to JSON
- Nested Objects (stand alone, in arrays or in dictionaries)
- Custom transformations during mapping
- Struct support
- Immutable support
The Basics
To support mapping, a class or struct just needs to implement the Mappable protocol which includes the following functions:
swift
init?(map: Map)
mutating func mapping(map: Map)
ObjectMapper uses the ().map(JSONString: JSONString)
// Create JSON String from Model
let JSONString = Mapper().toJSONString(user, prettyPrint: true)
ObjectMapper can map classes composed of the following types:
- Int
- Bool
- Double
- Float
- String
- RawRepresentable (Enums)
- Array
- Dictionary
- Object
- Array
- Array
- Set
- Dictionary
- Dictionary
- Optionals of all the above
- Implicitly Unwrapped Optionals of the above
Mappable Protocol
mutating func mapping(map: Map)
This function is where all mapping definitions should go. When parsing JSON, this function is executed after successful object creation. When generating JSON, it is the only function that is called on the object.
init?(map: Map)
This failable initializer is used by ObjectMapper for object creation. It can be used by developers to validate JSON prior to object serialization. Returning nil within the function will prevent the mapping from occuring. You can inspect the JSON stored within the Map object to do your validation:
swift
required init?(map: Map){
// check if a required "name" property exists within the JSON.
if map.JSON["name"] == nil {
return nil
}
}
StaticMappable Protocol
StaticMappable is an alternative to Mappable. It provides developers with a static function that is used by ObjectMapper for object initialization instead of init?(map: Map).
Note: StaticMappable, like Mappable, is a sub protocol of BaseMappable which is where the mapping(map: Map) function is defined.
static func objectForMapping(map: Map) - BaseMappable?
ObjectMapper uses this function to get objects to use for mapping. Developers should return an instance of an object that conforms to BaseMappable in this function. This function can also be used to:
- validate JSON prior to object serialization
- provide an existing cached object to be used for mapping
- return an object of another type (which also conforms to BaseMappable) to be used for mapping. For instance, you may inspect the JSON to infer the type of object that should be used for mapping (see examples in ClassClusterTests.swift)
If you need to implement ObjectMapper in an extension, you will need to adopt this protocTop issues
#961Getting issues for binary infix operator '<-' for swift4.1, Xcode 9.317 comments · 2018-04-19#1082Xcode 11.4 Swift Package Manager can't import ObjectMapper12 comments · 2020-02-13#1089ObjectMapper causes build error in Xcode 11.4 (11E146)9 comments · 2020-03-27#1137Cannot install via SPM, error: Invalid manifest7 comments · 2024-03-13#1130Privacy manifest5 comments · 2023-11-22#1121XCODE 12.0 Could not find module 'ObjectMapper' for target 'arm64-apple-ios-simulator';5 comments · 2021-08-11#1109Module compiled with Swift 5.2.4 cannot be imported by the Swift 5.3 compiler5 comments · 2020-09-21#1140// swift-tools-version:5.0 doesn't work under Xcode 154 comments · 2024-03-31
Latest release
4.4.3 4.4.3 2024-05-02What's Changed Update Package.swift to swift-tools-version 5.5 by @kaulex99 in https://github.com/tristanhimmelman/ObjectMapper/pull/1148 New Contributors @kaulex99 made their first contribution in https://github.com/tristanhimmelman/ObjectMapper/pull/1148 Full Changelog: https://github.com/tristanhimmelman/ObjectMapper/compare/4.4.2...4.4.3