Giter Club home page Giter Club logo

Comments (7)

renetik avatar renetik commented on June 9, 2024

Data as string looks like this:
"["https:\/\/img.motofotky.cz\/upload\/images\/forum\/2020\/12\/516270_upload.jpg"]"

I am able to parse it perfectly like this:

    func jsonValue() -> Any? {
        if let data = data(using: .utf8) {
            return try? JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .allowFragments])
        }
        return nil
    }

And I get swift array with one string obviously. So I believe parsing in you case is more strict what is in this case not useful at all as we developers sometimes don't have control over server responses or it is so complicated it's best to avoid...

from networking.

renetik avatar renetik commented on June 9, 2024

So I modified your library but, I just made small change...

public class JSONResponse: Response {
    let json: JSON
    public let body: Any?

    public var dictionaryBody: [String: Any] {
        return json.dictionary
    }

    public var arrayBody: [[String: Any]] {
        return json.array
    }

    public var data: Data {
        switch json {
        case let .array(value, _):
            return value
        case let .dictionary(value, _):
            return value
        case .none:
            return Data()
        }
    }

    init(json: JSON, response: HTTPURLResponse, body: Any? = nil) {
        self.json = json
        self.body = body
        super.init(response: response)
    }
}
        if let finalError = returnedError {
            self = .failure(FailureJSONResponse(json: json, response: response, error: finalError))
        } else {
            self = .success(SuccessJSONResponse(json: json, response: response, body: body))
        }

But I don't believe you want to push this up. Maybe better fix that parser please.

from networking.

mkll avatar mkll commented on June 9, 2024

Data as string looks like this:
"["https://img.motofotky.cz/upload/images/forum/2020/12/516270_upload.jpg"]"

@rene-dohan, Hi, could you post the output of the curl request to the API endpoint here? Just the copy/paste from terminal window. It seems that there is no need to make changes to the library code, you just need to change the parsing method that you use. But this needs to be clarified.

from networking.

mkll avatar mkll commented on June 9, 2024

@rene-dohan, I looked more closely and, as I understand the essence of the problem, you need access to the raw data, but the library does not allow this.

But the library allows. Look at this:

networking.get("/endpoint") { result in
	switch result {
	case .success(let response):
		let jsonData = response.data
	
		do {
			let array = try JSONDecoder().decode([String].self, from: jsonData)
			print(array as Any)
		} catch {
			print(error.localizedDescription)
		}
	case .failure(let response):
		let json = response.dictionaryBody
		print(json)
	}
}

This snippet does just what you need. The result is an array of string values.

from networking.

renetik avatar renetik commented on June 9, 2024

No becase I see empty data there :) look at your source code please ;)

public var data: Data {
switch json {
case let .array(value, _):
return value
case let .dictionary(value, _):
return value
case .none:
return Data()
}
}

So basically you return empty Data(), I don't know why , what should I do with it ? :) well its probably because you think .none means there was not data in body but as I say youre json parsing failed and in such situation you BLOCKED ACCESS to VALID response body.

I already successfully modified your lib as I told you, to get to body of response but you should be aware of this issue.. It could be quite good library if you make it more flexible, or just make that parsing to return that array :)

from networking.

mkll avatar mkll commented on June 9, 2024

@rene-dohan I am not the author of this library. I just wanted to help. :)

So basically you return empty Data(), I don't know why

In order not to return nil, of course.

from networking.

mkll avatar mkll commented on June 9, 2024

@rene-dohan I conducted a series of experiments and ended up with the fact that we really do not have access to raw data. So you are right.

Could you make a pull request?

from networking.

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.