All Projects → nixzhu → Baby

nixzhu / Baby

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

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Baby

Autocser
AutoCSer is a high-performance RPC framework. AutoCSer 是一个以高效率为目标向导的整体开发框架。主要包括 TCP 接口服务框架、TCP 函数服务框架、远程表达式链组件、前后端一体 WEB 视图框架、ORM 内存索引缓存框架、日志流内存数据库缓存组件、消息队列组件、二进制 / JSON / XML 数据序列化 等一系列无缝集成的高性能组件。
Stars: ✭ 140 (-34.58%)
Mutual labels:  json, code-generator
Omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
Stars: ✭ 148 (-30.84%)
Mutual labels:  json, parser
Jaxon
Streaming JSON parser for Elixir
Stars: ✭ 145 (-32.24%)
Mutual labels:  json, parser
Tiny Json
The tiny-json is a versatile and easy to use json parser in C suitable for embedded systems. It is fast, robust and portable.
Stars: ✭ 127 (-40.65%)
Mutual labels:  json, parser
Csmodel
CSModel is a concise and efficient model framework for iOS/OSX, and provides nested Model to compare values and copy values.
Stars: ✭ 192 (-10.28%)
Mutual labels:  json, model
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-36.45%)
Mutual labels:  json, model
Ladybug
A powerful model framework for Swift 4
Stars: ✭ 147 (-31.31%)
Mutual labels:  json, model
Parze
A clean, efficient parser combinator
Stars: ✭ 113 (-47.2%)
Mutual labels:  parser-combinators, parser
Rapidyaml
Rapid YAML - a library to parse and emit YAML, and do it fast.
Stars: ✭ 183 (-14.49%)
Mutual labels:  json, parser
Json To Ast
JSON AST parser
Stars: ✭ 161 (-24.77%)
Mutual labels:  json, parser
Prowide Core
Model and parsers for all SWIFT MT (FIN) messages
Stars: ✭ 125 (-41.59%)
Mutual labels:  parser, model
Sdk
Library for using Grafana' structures in Go programs and client for Grafana REST API.
Stars: ✭ 193 (-9.81%)
Mutual labels:  json, parser
Alembic
⚗️ Functional JSON Parser - Linux Ready 🐧
Stars: ✭ 115 (-46.26%)
Mutual labels:  json, parser
Json Autotype
Automatic Haskell type inference from JSON input
Stars: ✭ 139 (-35.05%)
Mutual labels:  json, parser
Moyamapper
快速解析模型工具,支持RxSwift。同时支持缓存功能 【相关手册 https://MoyaMapper.github.io 】
Stars: ✭ 115 (-46.26%)
Mutual labels:  json, model
Parjs
JavaScript parser-combinator library
Stars: ✭ 145 (-32.24%)
Mutual labels:  parser-combinators, parser
Forge
Functional style JSON parsing in Kotlin
Stars: ✭ 106 (-50.47%)
Mutual labels:  json, parser
Cppcmb
A generic C++17 parser-combinator library with a natural grammar notation.
Stars: ✭ 108 (-49.53%)
Mutual labels:  parser-combinators, parser
Gelatin
Transform text files to XML, JSON, or YAML
Stars: ✭ 150 (-29.91%)
Mutual labels:  json, parser
Swiftparsec
A parser combinator library written in the Swift programming language.
Stars: ✭ 192 (-10.28%)
Mutual labels:  parser-combinators, parser

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 the CuteBaby or SmartBaby on the Mac App Store.

Contact

You can find me on Twitter.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].