• Stars
    star
    219
  • Rank 181,133 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

Get Ducky in the Mac App Store.

Contact

You can find me on Twitter.

License

MIT

More Repositories

1

dev-blog

翻译、开发心得或学习笔记
3,938
star
2

MonkeyKing

MonkeyKing helps you to post messages to Chinese Social Networks.
Swift
2,753
star
3

Proposer

Make permission request easier.
Swift
855
star
4

Coolie

Coolie(苦力) helps you to create models (& their constructors) from a JSON file.
Swift
507
star
5

KeyboardMan

KeyboardMan helps you to make keyboard animation.
Swift
351
star
6

Sensei

Sensei is a Mac app based on OpenAI API.
Swift
274
star
7

Wormhole

A more elegant way for message passing between iOS apps and extensions.
Swift
172
star
8

Bible-Assistant

Bible Assistant App
Objective-C
168
star
9

Ruler

Size matters, you need a ruler.
Swift
159
star
10

Navi

Focus on avatar caching.
Swift
115
star
11

AudioBot

AudioBot helps you do audio record & playback.
Swift
38
star
12

Redstone

Redstone has a State Machine
Swift
37
star
13

SharedBaby

Baby's web interface.
Swift
29
star
14

nixBoard

一块简易的围棋棋盘
JavaScript
25
star
15

HelloMou

The translation(Chinese) of Markdown Editor Mou's Hello.md
21
star
16

GentleTouch

Touch yourself, with WATCH.
Swift
19
star
17

Ananda

JSON model decoding based on yyjson.
Swift
16
star
18

AdaptiveChartDemo

Generate chart image from simple data, for WATCH
Swift
15
star
19

computation

Understanding Computation: From Simple Machines to Impossible Programs
Swift
14
star
20

AppClip

Create Web Clip for App.
Swift
14
star
21

rushGo

围棋急速入门,关于围棋的介绍,使对围棋感兴趣的人对围棋的规则有个快速了解。
12
star
22

nixhttpd

简易的http服务器以及测试环境
C
11
star
23

KeypathObserver

KeypathObserver helps you do KVO observing.
Swift
11
star
24

nixGoBoard

在iOS上实现的棋盘应用,可用于在生活中遇到某个围棋爱好者时,将iPad当做棋盘来一局,仅此而已。
Objective-C
11
star
25

SuperPreview

SuperPreview for Image Preview
Swift
8
star
26

AutoReview

AutoReview has a nice logic for show prompt of app review.
Swift
8
star
27

PropertyListenerDemo

Listen, they're changed!
Swift
7
star
28

MentionInUITextViewDemo

Detect mention in UITextView
Swift
7
star
29

TextureDemo

Swift
5
star
30

algorithm-playgrounds

Try some algorithms
Swift
5
star
31

AutoLayoutInUIScrollView

AutoLayout in UIScrollView Demo
Swift
5
star
32

StateMachineDemo

Simple State Machine Demo
Swift
5
star
33

coolie-cli

Coolie's command-line interface
Swift
5
star
34

WorkerBee

WorkerBee is a toolkit
Swift
4
star
35

CenterTwoViewsUseAutoLayout

AutoLayout Tip 1
Objective-C
4
star
36

42

The answer to life, the universe and everything.
3
star
37

NotificationCenterDemo

Swift
3
star
38

sgf2asy

转换sgf格式的围棋棋谱为asy矢量格式的脚本
Python
2
star
39

Girl

A HTML Parser
Swift
2
star
40

nixzhu.github.io

HTML
1
star
41

code-reading

Notes from code reading
1
star