在Swift中解码带有特殊字符的URL

4

我使用的一种API提供了URL链接,这些链接可能包含特殊字符,比如说"http://es.dbpedia.org/resource/Análisis_de_datos"(其中包含á字母)。

这是一个绝对有效的URL,然而,如果可解码类中包含一个可选的URL?变量,这个变量就无法被解码。

我可以将我的类中的URL?更改为String?,并使用类似于URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)的计算属性,但也许有更优雅的解决方案。

在Playground中重新生成:

struct Container: Encodable {
    let url: String
}

struct Response: Decodable {
    let url: URL?
}

let container = Container(url: "http://es.dbpedia.org/resource/Análisis_de_datos")

let encoder = JSONEncoder()
let encodedData = try encoder.encode(container)
    
let decoder = JSONDecoder()
let response = try? decoder.decode(Response.self, from: encodedData)
// response == nil, as it can't be decoded.

let url = response?.url 

你考虑过自定义解码吗? - Sweeper
是的,我有。但是这个类本身有大约20个变量和12个嵌套结构体。这不值得。 - Alexey Chekanov
2个回答

4
有多种方法可以解决这个问题,但我认为使用属性包装器可能是最优雅的:
@propertyWrapper
struct URLPercentEncoding {
   var wrappedValue: URL
}

extension URLPercentEncoding: Decodable {
   public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
        
      if let str = try? container.decode(String.self),
         let encoded = str.addingPercentEncoding(
                              withAllowedCharacters: .urlFragmentAllowed),
         let url = URL(string: encoded) {

         self.wrappedValue = url

      } else {
         throw DecodingError.dataCorrupted(
            .init(codingPath: container.codingPath, debugDescription: "Corrupted url"))
      }
   }
}

这样使用时,模型的消费者无需了解其内部细节:

struct Response: Decodable {
    @URLPercentEncoding let url: URL
}

2
你可以扩展KeyedDecodingContainer并实现自己的URL解码方法:
extension KeyedDecodingContainer {
    func decode(_ type: URL.Type, forKey key: K) throws -> URL {
        let string = try decode(String.self, forKey: key)
        guard let url = URL(string: string.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
        else {
            throw DecodingError.dataCorrupted(.init(codingPath: codingPath, debugDescription: "The stringvalue for the key \(key) couldn't be converted into a URL value: \(string)"))
        }
        return url
    }
    // decoding an optional URL
    func decodeIfPresent(_ type: URL.Type, forKey key: K) throws -> URL? {
        try URL(string: decode(String.self, forKey: key).addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) ?? "")
    }
}

struct Container: Encodable {
    let url: String
}

struct Response: Decodable {
    let url: URL
}

let container = Container(url: "http://es.dbpedia.org/resource/Análisis_de_datos")

do {
    let encodedData = try encoder.encode(container)
    print(String(data: encodedData, encoding: .utf8))
    let decoder = JSONDecoder()
    let response = try decoder.decode(Response.self, from: encodedData)
    print(response)
} catch {
    print(error)
}

这将打印出:

Optional("{"url":"http:\/\/es.dbpedia.org\/resource\/Análisis_de_datos"}")

响应(url: http://es.dbpedia.org/resource/An%C3%A1lisis_de_datos)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接