Skip to content

πŸ§‘πŸΌβ€πŸ’» An Xcode Source Editor extension for ObjectMapper user to convert JSON into Swift code.

License

Notifications You must be signed in to change notification settings

wgy6055/MappingCoder

Repository files navigation

Platform Platform GitHub release (latest by date) GitHub license Swift version GitHub all releases

English | δΈ­ζ–‡

πŸ§‘πŸΌβ€πŸ’» An Xcode Source Editor extension for ObjectMapper user to convert JSON into Swift code.

⏬ Download

Features

  • Convert JSON to Mappable
  • Convert JSON to ImmutableMappable
  • Auto Complete Mapping Methods
    • Custom mapping with attribute @map
  • Support converting nested type
  • Use lower camel case for property names
  • Use Int64 for property named xxx(ID|Id|id)

Install

  • macOS 10.15+
  • Drag MappingCoder.app into Applications 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, selecting MappingCoder to activate it.

Uninstall

Moving MappingCoder.app to Trash.

Killing Xcode if there is a pop up showing "some of its extensions are in use".

Usage

In Xcode, selecting JSON or Class/Struct Declaration. And choosing Editor > MappingCoder > ... to use.

Examples

Convert JSON to Mappable

Convert JSON to ImmutableMappable

Auto Complete Mapping Methods

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.

Use Lower Camel Case & Int64

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"]
    }
}

Settings

In Xcode, choosing Editor > MappingCoder > Settings... to open Settings.

Inspired By

License

MIT