Project

jsoncop

0.01
No commit activity in last 3 years
No release in over 3 years
A light-weight JSON to model method generator.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.0

Runtime

< 5.0, >= 3
< 2.0, >= 1.0.0
~> 1.2
< 2.0, >= 1.2.0
 Project Readme

JSONCop

License Gem Swift

JSONCop makes it easy to write a simple model layer for your Cocoa and Cocoa Touch application.

JSONCop scans all the methods and variables like JSONKeyPathByPropertyKey xxxJSONTransformer in all the structs which declare //@jsoncop before struct declaration line. And it won't change your original project structure.

let json: [String: Any] = [
    "id": 1,
    "name": "Draven",
    "createdAt": NSTimeIntervalSince1970
]
let person = Person.parse(json: json)

Usage

  1. Run cop install in a project root folder
  2. Add //@jsoncop just before model definition line
$ sudo gem install jsoncop --verbose
$ cop install
//@jsoncop
struct Person {
    let id: Int
    let name: String
}

Then, each time build action is triggered, JSONCop would generate several parsing methods in swift files.

All the code between // MARK: - JSONCop-Start and // JSONCop-End and will be replaced when re-run cop generate in current project folder. Other codes will remain unchanged. Please don't write any codes in this area.

extension Person {
    static func parse(json: Any) -> Person? {
        guard let json = json as? [String: Any] else { return nil }
        guard let id = json["id"] as? Int,
            let name = json["name"] as? String,
            let projects = (json["projects"] as? [[String: Any]]).flatMap(projectsJSONTransformer) else { return nil }
        return Person(id: id, name: name, projects: projects)
    }
    static func parses(jsons: Any) -> [Person] {
        guard let jsons = jsons as? [[String: Any]] else { return [] }
        return jsons.flatMap(parse)
    }
}

Checkout JSONCopExample for more information.

Customize

  • JSON key to attribute customisation

    struct Person {
        let id: Int
        let name: String
    
        static func JSONKeyPathByPropertyKey() -> [String: String] {
            return ["name": "nickname"]
        }
    }
  • Value transformer customisation

    struct Person {
        let id: Int
        let name: String
        let gender: Gender
        let projects: [Project]
    
        enum Gender: Int {
            case male = 0
            case female = 1
        }
    
        static func genderJSONTransformer(value: Int) -> Gender? {
            return Gender(rawValue: value)
        }
    
        static func projectsJSONTransformer(value: [[String: Any]]) -> [Project] {
            return value.flatMap(Project.parse)
        }
    }
  • Nested JSON value extraction

    static func JSONKeyPathByPropertyKey() -> [String: String] {
        return ["currentProjectName": "project.name"]
    }

Installation

sudo gem install jsoncop --verbose

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/draveness/jsoncop. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.