English | δΈζ
π§πΌβπ» An Xcode Source Editor extension for ObjectMapper user to convert JSON into Swift code.
β¬ Download
- Convert JSON to
Mappable
- Convert JSON to
ImmutableMappable
- Auto Complete Mapping Methods
- Custom mapping with attribute
@map
- Custom mapping with attribute
- Support converting nested type
- Use lower camel case for property names
- Use
Int64
for property namedxxx(ID|Id|id)
- macOS 10.15+
- Drag
MappingCoder.app
intoApplications
folder. - Open it. The extension will be installed on Xcode when the app is opened for the first time.
- In
System Preferences > Extensions > Xcode Source Editor
, selectingMappingCoder
to activate it.
Moving MappingCoder.app
to Trash.
Killing Xcode if there is a pop up showing "some of its extensions are in use".
In Xcode, selecting JSON or Class/Struct Declaration. And choosing Editor > MappingCoder > ...
to use.
Sometimes, there is no need to convert whole JSON to Swift code. So you can simply define properties and use Auto Complete Mapping Methods
to generate init(map:)
& mapping(map:)
. If you want to customize the mapping, attribute @map()
is provided to determine key
& default
for each property.
Here is the declaration of @map
in Swift Style.
@map(key: String? = nil, default: Any? = nil)
You can use it like this.
// Typing your declaration with @map in line comment
struct Person: ImmutableMappable {
// @map(key: "all_skills", default: [])
let skills: [Any]
// @map(key: "user-name", default: "")
let name: String
// @map(default: [:])
let profile: [String : Any]
// @map(key: "math score")
let mathScore: Int
}
// Run Auto Complete Mapping Methods
struct Person: ImmutableMappable {
// @map(key: "all_skills", default: [])
let skills: [Any]
// @map(key: "user-name", default: "")
let name: String
// @map(default: [:])
let profile: [String : Any]
// @map(key: "math score")
let mathScore: Int
init(map: Map) throws {
skills = (try? map.value("all_skills")) ?? []
name = (try? map.value("user-name")) ?? ""
profile = (try? map.value("profile")) ?? [:]
mathScore = (try? map.value("math score")) ?? <#defaultValue#>
}
func mapping(map: Map) {
skills >>> map["all_skills"]
name >>> map["user-name"]
profile >>> map["profile"]
mathScore >>> map["math score"]
}
}
This feature is powered by SwiftSyntax.
MappingCoder
names property using lower camel case automatically. And also, defines property named xxx(ID|Id|id)
as Int64
.
//{
// "user_name": "jack",
// "user-id": 123456789
//}
class <#name#>: Mappable {
var userId: Int64 = <#defaultValue#>
var userName: String = <#defaultValue#>
required init?(map: Map) {}
func mapping(map: Map) {
userId <- map["user-id"]
userName <- map["user_name"]
}
}
In Xcode, choosing Editor > MappingCoder > Settings...
to open Settings.