Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manipulate and resend a JSON response #22

Closed
lifeisfoo opened this issue Dec 14, 2015 · 5 comments
Closed

Manipulate and resend a JSON response #22

lifeisfoo opened this issue Dec 14, 2015 · 5 comments

Comments

@lifeisfoo
Copy link
Contributor

I'm using siesta with SwiftyJSON, and I've a login call that returns some json data that I need to resend after a proper manipulation:

//api.login is a Resource
api.login.request(.POST, json: [:])
            .success() { response in
                var res = response.json
                //example manipulation
                res["cbs"][0]["input"][0]["value"].string = "00000"
                api.login.request(.POST, json: res).success(){/*...*/}
            }

But this code generate a static error:

Argument type 'JSON' does not conform to expected type 'NSJSONConvertible'

Howto manipulate response data between two (ore more) request?

@pcantrell
Copy link
Member

I assume that response.json is the typed content accessor from the example project that returns SwiftyJSON, right?

What's going on here is that at this point:

var res = response.json
//example manipulation
res["cbs"][0]["input"][0]["value"].string = "00000"

res is a SwiftyJSON.JSON object, but NSJSONConvertible wants a dictionary or array. You have two options:

  1. Convert the JSON back to an NSDictionary to pass it to Siesta:

    api.login.request(.POST, json: res.dictionary)
    
  2. If you're doing that in many places, add a Siesta request flavor that accepts JSON objects:

    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: json.dictionary) }
    

(Warning: untested code there!)

@lifeisfoo
Copy link
Contributor Author

Yes, res is a SwiftyJSON.JSON.

I've already tried with .dictionary but seems that it generates a non valid type for the method signature:

Argument type '[String : JSON]?' does not conform to expected type 'NSJSONConvertible'

@pcantrell
Copy link
Member

Ah, indeed. Here is some code that I’ve verified actually compiles:

extension Resource
    {
    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: json.object as! NSJSONConvertible) }
    }

See if that does the trick.

(Hmm. I should expose FailedRequest in the public API so you can replace that as! with an as?.)

@lifeisfoo
Copy link
Contributor Author

Yes, this is right, but there is a typo in the call: the parameter is called jsonDict, not json :).

extension Resource
    {
    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: jsonDict.object as! NSJSONConvertible) }
    }

Thank you for the help.

Do you think that could be useful to add this example in a "Cookbook" page inside Siesta docs?

@pcantrell
Copy link
Member

the parameter is called jsonDict, not json

Ah, right.

I mean to write up a cookbook for 1.0, but am trying to get API details sorted out first.

I may also add a standard SwiftyJSON extension, now that there's an Extensions folder for such things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants