2
\$\begingroup\$

I am working on a program that would generate swift classes/ structures for JSON file contents.

The link to the application is Json2Model in case you want to contribute directly.

I do not know a lot of Swift but this is what I came up with. These classes most likely will not hold any logic they are just data.

input.json

{
    "customer":{
        "firstName": "John",
        "lastName": "Doe"
    },
    "addresses": [
        {
            "street": "Faraway Creek",
            "number": 1234,
            "city": "Never-land"
        }
    ],
    "balance": 23234.23,
    "active": true
}

I want the following generated code to be reviewed.

Input.swift

struct Input {
    var addresses: [Address]
    let balance: Double
    let active: Bool
    let customer: Customer

    init(addresses: [Address], balance: Double, active: Bool, customer: Customer) {
        self.addresses = addresses;
        self.balance = balance;
        self.active = active;
        self.customer = customer;
    }

    static func load() {
        //TODO: Needs to be implemented.
    }

}

Address.swift

struct Address {
    let number: Int
    let city: String
    let street: String

    init(number: Int, city: String, street: String) {
        self.number = number;
        self.city = city;
        self.street = street;
    }

}

Customer.swift

struct Customer {
    let firstName: String
    let lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName;
        self.lastName = lastName;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy