Code Monkey home page Code Monkey logo

baby's Introduction

Baby

Create models from a JSON file, even a Baby can do it.

Description

Baby can infer property's type from json such as String, Int, Double, URL and Date.

Baby can handle nested json, it will generate nested models.

Baby supports Codable from Swift 4.

Example

JSON:

{
    "id": 42,
    "name": "nixzhu",
    "twitter": {
        "profile_url": "https://twitter.com/nixzhu",
        "created_at": "2009-05-12T10:25:43.511Z"
    }
}

Swift code with Codable:

struct User: Codable {
    let id: Int
    let name: String
    struct Twitter: Codable {
        let profileURL: URL
        let createdAt: Date
        private enum CodingKeys: String, CodingKey {
            case profileURL = "profile_url"
            case createdAt = "created_at"
        }
    }
    let twitter: Twitter
}

Note that there use Property Map profile_url: profileURL to change the property name (Automatically generated will be profileUrl).

Swift code without Codable:

struct User {
    let id: Int
    let name: String
    struct Twitter {
        let profileURL: URL
        let createdAt: Date
        init(profileURL: URL, createdAt: Date) {
            self.profileURL = profileURL
            self.createdAt = createdAt
        }
        init?(json: [String: Any]) {
            guard let profileURLString = json["profile_url"] as? String else { return nil }
            guard let profileURL = URL(string: profileURLString) else { return nil }
            guard let createdAtString = json["created_at"] as? String else { return nil }
            guard let createdAt = DateFormatter.iso8601.date(from: createdAtString) else { return nil }
            self.init(profileURL: profileURL, createdAt: createdAt)
        }
    }
    let twitter: Twitter
    init(id: Int, name: String, twitter: Twitter) {
        self.id = id
        self.name = name
        self.twitter = twitter
    }
    init?(json: [String: Any]) {
        guard let id = json["id"] as? Int else { return nil }
        guard let name = json["name"] as? String else { return nil }
        guard let twitterJSONDictionary = json["twitter"] as? [String: Any] else { return nil }
        guard let twitter = Twitter(json: twitterJSONDictionary) else { return nil }
        self.init(id: id, name: name, twitter: twitter)
    }
}

You may need a DateFormatter extension:

extension DateFormatter {

    static let iso8601: DateFormatter = {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone(abbreviation: "UTC")
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
        return formatter
    }()
}

Baby can also handle array root json, it will automatically merge properties for objects in array.

Installation

Build

$ bash install.sh

Run

$ baby -i JSONFilePath

Help

$ baby --help

Try Baby's web interface SharedBaby if you like.

Or, buy the CuteBaby on the Mac App Store.

Contact

You can find me on Twitter or Weibo.

Donation

BitCoin: 1EcDVcsQPeg7QVGdCFkQpHXKQMjPJmbixz

License

MIT

baby's People

Contributors

markiv avatar nixzhu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.