Latest 2.1.1
Homepage https://github.com/YutoMizutani/JSONtoCodable
License MIT
Platforms ios 8.0, osx 10.10, watchos 4.0, tvos 11.0, requires ARC
Authors

JSONtoCodable

Build Status
CocoaPods
MIT License
CocoaPods
Carthage compatible
codecov

JSONtoCodable is a generating tool from Raw JSON to Codable (Swift4) text written in Swift4.

Qiita: JSONからCodable化されたstructを自動生成するツールを作った話 – Qiita

demo_macos.png

TL;DR

From JSON,

{
    "user": {
        "Name": "Yuto Mizutani"
    },
    "lib": {
        "lib-name": "JSONtoCodable",
        "year": 2018,
        "version": "1.0.2",
        "released": "2018-09-22"
    },
    "text": "Hello, world!!"
}

to Codable.

public struct Result: Codable {
    public let user: User
    public let lib: Lib
    public let text: String

    public struct User: Codable {
        public let name: String

        private enum CodingKeys: String, CodingKey {
            case name = "Name"
        }
    }

    public struct Lib: Codable {
        public let libName: String
        public let year: Int
        public let version: String
        public let released: String

        private enum CodingKeys: String, CodingKey {
            case libName = "lib-name"
            case year
            case version
            case released
        }
    }
}

macOS App

demo_macos.png

jc (CLI App)

Installation

$ brew tap YutoMizutani/jc
$ brew install jc

Usage example

$ curl https://httpbin.org/get | jc

or generate .swift file,

$ curl https://httpbin.org/get | jc > Result.swift

Help command

$ jc -h

Screen shot

demo_cli.png

Support formats

  • Type
    • String
    • Bool
    • Int
    • Double
    • struct(s)
    • Optional
  • Array
    • Start array
    • Muptiple array
    • Arrayed objects
    • Optional array
  • Number of nested array and objects
    • Infinity
  • Number of spaces in entered JSON
    • 0 to infinity

Translations

JSON Value Swift Type
"text" String
true Bool
-10 Int
1.0 Double
null <Foo>?
(the others) Any

Usage

import JSONtoCodable

let json: String = """
{
    "Hello": "Hello, world!!"
}
"""

let jsonToCodable = JSONtoCodable()
let codable = try? jsonToCodable.generate(json)

print(codable)
/*
struct Result: Codable {
    let hello: String

    private enum CodingKeys: String, CodingKey {
        case hello = "Hello"
    }
}
*/

Config

let config = Config()
config.name = "Result" // struct Result: Codable {}
config.accessModifier = AccessModifier.public // public struct
config.caseType = (variable: CaseType.camel, struct: CaseType.pascal)
config.lineType = LineType.lineFeed
config.indentType = IndentType.space(4)

See more: Config.swift

Develop to use installation

Cocoapods

Add this to your Podfile:

pod 'JSONtoCodable'

and

$ pod install

Carthage

Add this to your Cartfile:

github "YutoMizutani/JSONtoCodable"

and

$ carthage update

License

JSONtoCodable is available under the MIT license.

Latest podspec

{
    "name": "JSONtoCodable",
    "version": "2.1.1",
    "summary": "A generating tool from Raw JSON to Codable",
    "description": "A generating tool from Raw JSON to Codable.",
    "homepage": "https://github.com/YutoMizutani/JSONtoCodable",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Yuto Mizutani": "[email protected]"
    },
    "source": {
        "git": "https://github.com/YutoMizutani/JSONtoCodable.git",
        "tag": "2.1.1"
    },
    "social_media_url": "https://twitter.com/EXPENSIVE_MAN",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "4.0",
        "tvos": "11.0"
    },
    "requires_arc": true,
    "source_files": "Sources/**/*.swift",
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This