Skip to content

lijuncode/Baby

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Baby

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

Description

Baby can detect 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 Decodable:

struct User: Decodable {
  let id: Int?
  let name: String?
  struct Twitter: Decodable {
    let profileUrl: URL?
    let createdAt: Date?
    private enum CodingKeys: String, CodingKeyPath {
      case profileUrl = "profile_url"
      case createdAt = "created_at"
    }
    static func decode(_ ext: Extractor) throws -> struct {
      return try struct(
        profileUrl: try? URLTransformer.apply(ext <| CodingKeys.profileUrl.keyPath),
        createdAt: try? dateTransformer.apply(ext <| CodingKeys.createdAt.keyPath)
      )
    }
  }
  let twitter: Twitter?
  private enum CodingKeys: String, CodingKeyPath {
    case id
    case name
    case twitter
  }
  static func decode(_ ext: Extractor) throws -> struct {
    return try struct(
      id: ext <|? CodingKeys.id.keyPath,
      name: ext <|? CodingKeys.name.keyPath,
      twitter: ext <|? CodingKeys.twitter.keyPath
    )
  }
}

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).

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

Or, try Baby's web interface SharedBaby.

Contact

You can find me on Twitter or Weibo.

Donation

BitCoin: 1EcDVcsQPeg7QVGdCFkQpHXKQMjPJmbixz

License

MIT

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 99.7%
  • Shell 0.3%