Coder Social home page Coder Social logo

How to model this? about gloss HOT 3 CLOSED

ed-mejia avatar ed-mejia commented on August 23, 2024
How to model this?

from gloss.

Comments (3)

hkellaway avatar hkellaway commented on August 23, 2024

i'd handle this nesting in one of two ways. either create nested Gloss models for each step - or, use guard statements within the initializer to assure the JSON has the desired values.

the two examples you provided are similar, so i'm only going to illustrate one unless there's a particular need.

Strategy 1 - Everything is a model:

struct MyObject: Decodable {

    let data: Data

    init?(json: JSON) {
        guard let data: Data = "data" <~~ json
            else { return nil }

        self.data = data
    }

}
struct Data: Decodable {

    let user: User

    init?(json: JSON) {
        guard let user: User = "user" <~~ json
            else { return nil }

        self.user = user
    }

}
struct User: Decodable {

    let id: Int
    let name: String

    init?(json: JSON) {
        guard let id: Int = "id" <~~ json,
            let name: String = "name" <~~ json
            else { return nil }

        self.id = id
        self.name = name
    }

}

Here, we see the object that represents the full JSON, MyObject, has a property that is another Gloss model, Data, which has a property that is another Gloss model, User.

Strategy 2 - Use guard statements to drill down to the model you want

struct MyObject2: Decodable {

    let user: User

    init?(json: JSON) {
        guard let dataJSON = json["data"] as? JSON, user: User = "user" <~~ dataJSON
            else {
            return nil
        }

        self.user = user
    }

}

Here, the desired property was a User model instead of a Data model, so we guaranteed with our guard statement that the nested JSON under the "data" key was present before constructing our property out of it.

Hope that helps.

from gloss.

ed-mejia avatar ed-mejia commented on August 23, 2024

Hey thanks mate!, this really put it clear to me.

Btw I came here because I read your blog http://harlankellaway.com/blog/2015/07/05/swift-json-parsing-by-example/

I ended using your solution since it seems less complicated and allows me to deal with object construction within the init?(json: JSON) which is great but it seems that the only benefit here is the use of the operator <~~ otherwise we would end just crating an object as a normal init in your model.

I was trying to implement ObjectMapper before, I must say it has really good features like nested values using dot notation, the func value(), valuerOrFail(), valueOf(default) are really awesome tools but unfortunately it doesn't work well on real world scenarios where JSON are big, diverse and more complicated that the usual "samples"

Thanks again great work 👍

from gloss.

hkellaway avatar hkellaway commented on August 23, 2024

thanks for reading the post and trying it out 👌🏼

yes, a benefit comes in taking away a lot of boilerplate you'd have to write in translating all sorts of values - numbers, strings, enums, arrays, other models - from JSON yourself and allows you to focus on an almost one to one matching of property to JSON key

from gloss.

Related Issues (20)

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.