Coder Social home page Coder Social logo

Working with Arrays at the Root about gloss HOT 4 CLOSED

rahul0x24 avatar rahul0x24 commented on August 23, 2024
Working with Arrays at the Root

from gloss.

Comments (4)

hkellaway avatar hkellaway commented on August 23, 2024

it's not clear to me that there's a good way to handle that situation the library itself. it could be managed client-side by doing the following.

create a model with a new initializer that takes in JSON array - and creates a JSON dictionary with a custom key, which is passed to the required initializer:

struct Test: Glossy {

    let urls: [String]

    // introduce JSON array initializer that creates JSON with a custom key
    init?(jsonArray: [AnyObject]) {
        let jsonDictionary = ["myCustomJSONArrayKey" : jsonArray]

        self.init(json: jsonDictionary)
    }

    init?(json: JSON) {
        guard let urls: [String] = "myCustomJSONArrayKey" <~~ json else {
            return nil
        }

        self.urls = urls
    }

    func toJSON() -> JSON? {
        return jsonify([
            "myCustomJSONArrayKey" ~~> self.urls
            ])
    }

}

client-side, use the custom key to retrieve values:

let testJSON = [ "http://google.com", "http://facebook.com" ]
let test = Test(jsonArray: testJSON)

print(test?.urls)

// use the custom key to retrieve array
let myJSON: [String]? = test?.toJSON()?["myCustomJSONArrayKey"] as? [String]

print(myJSON)

i get its not ideal - but the designation of JSON as a dictionary is currently part of the library. thoughts on this solution?

from gloss.

rahul0x24 avatar rahul0x24 commented on August 23, 2024

The solution works but I guess this will increase more work for the developer.
JSON could be Object, Array, String, Number and NSNull.

Refer to https://github.com/thoughtbot/Argo/blob/master/Argo/Types/JSON.swift. It is handling all the scenarios of JSON.

The same issue exists with Alamofire also. The parameters are the type of [String: AnyObject] so it doesn't allow us to send post requests using Array of objects. We always have to keep a rootKey like "results" and then pass the array into it.
http://stackoverflow.com/questions/27026916/sending-json-array-via-alamofire

I can work on this with the test cases if you like. I kind of need this functionality to save myself from extra work 😋.

from gloss.

hkellaway avatar hkellaway commented on August 23, 2024

sure, if you have the time to work on it. i'd love to see what you come up with

from gloss.

rahul0x24 avatar rahul0x24 commented on August 23, 2024

Update: I was able to solve this in Reactofire itself and it seems to work without changing the Gloss Library. Regards

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.