Coder Social home page Coder Social logo

json-to-swift-converter's Introduction

JSON-to-Swift-Converter

An Xcode 9 editor extension to convert JSON format to Swift code.

Description

This is a lightweight naïve implementation for converting JSON-formatted text into Swift code. The JSON-formatted text structure and types are interpreted, and code is generated. Swift 4 Codable is supported.

There are several settings for controlling how the code is generated. By default, coding keys are declared, and properties and nested types are declared. The settings can be changed from the application that hosts the Xcode editor extension.

Separator characters (space, dash, and underscore) in JSON property names denote word boundaries, e.g., for a JSON property name "camel case", "camelCase" is generated for the corresponding Swift key name. Swift struct types are generated as substitutable with a "Type" suffix, for example, "<#CamelCaseType#>".

Example

The extension will convert the following JSON-formatted text:

{
    "currency": [
                 {
                 "symbol": "cp",
                 "coefficient": 0.01,
                 "long name": "copper piece",
                 "long name plural": "copper pieces"
                 },
                 {
                 "symbol": "sp",
                 "coefficient": 0.1,
                 "long name": "silver piece",
                 "long name plural": "silver pieces"
                 }
                ]
}

Into the following Swift implementation:

    private enum CodingKeys: String, CodingKey {
        let symbol
        let longNamePlural = "long name plural"
        let currency
        let longName = "long name"
        let coefficient
    }

    struct <#CurrencyType#>: Codable {

        let longNamePlural: String!
        let coefficient: Double!
        let symbol: String!
        let longName: String!

    }

    let currency: [<#CurrencyType#>]!

Usage

In Xcode, choose Editor > Convert JSON to Swift > Convert JSON to Swift.

Settings

In Xcode, choosing Editor > Convert JSON to Swift > Settings... opens the host application where settings that control the conversion can be changed. Settings include:

  • declaration: specify let or var for property declarations (default is let)
  • typeUnwrapping: options include explicit, optional ("?"), or required ("!") (default is required)
  • supportCodable: whether to provide and init(from:) and encode(to:) function (default is true)
  • addDefaultValue: whether to add default values, e.g., "= 0" (default is false)
  • addInitAndDictionary: whether to add an init(from:) method and dictionary: variable accessor (default is false)

Credits

This extension is loosely based on Json2Property, which in turn is based on ESJsonFormat-Xcode. This version is written entirely Swift, is expanded a bit, and has unit tests.

json-to-swift-converter's People

Contributors

mrlegowatch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

json-to-swift-converter's Issues

Add a test that invokes the Swift compiler on generated code

Although there are unit tests to validate the expected behavior of the extension, there is no test whether generated code is valid for the Swift compiler. It would be useful to have a test that validates generated code using the Swift compiler. This would require implementing wrapper struct type and substitution for embedded struct type names.

Keys should be static

Keys can't be used the way they are currently being declared, they need to be declared static.

invalid JSON format

I tried your extension but my Xcode showed me that I converted a invalid JSON
screen shot 2017-09-17 at 7 00 26 pm
Could you please help?

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.