JSONEncoder 不允许将类型编码为原始值

问题描述 投票:0回答:2

我正在为具有可能关联值的

Codable
类型开发
enum
的实现。由于这些对于每种情况都是独特的,所以我认为我可以在编码期间不使用密钥输出它们,然后简单地看看在解码时可以得到什么,以恢复正确的情况。

这是一个非常精简、人为的示例,演示了一种动态类型值:

enum MyValueError : Error { case invalidEncoding }

enum MyValue {
    case bool(Bool)
    case float(Float)
    case integer(Int)
    case string(String)
}

extension MyValue : Codable {
    init(from theDecoder:Decoder) throws {
        let theEncodedValue = try theDecoder.singleValueContainer()

        if let theValue = try? theEncodedValue.decode(Bool.self) {
            self = .bool(theValue)
        } else if let theValue = try? theEncodedValue.decode(Float.self) {
            self = .float(theValue)
        } else if let theValue = try? theEncodedValue.decode(Int.self) {
            self = .integer(theValue)
        } else if let theValue = try? theEncodedValue.decode(String.self) {
            self = .string(theValue)
        } else { throw MyValueError.invalidEncoding }
    }

    func encode(to theEncoder:Encoder) throws {
        var theEncodedValue = theEncoder.singleValueContainer()
        switch self {
        case .bool(let theValue):
            try theEncodedValue.encode(theValue)
        case .float(let theValue):
            try theEncodedValue.encode(theValue)
        case .integer(let theValue):
            try theEncodedValue.encode(theValue)
        case .string(let theValue):
            try theEncodedValue.encode(theValue)
        }
    }
}

let theEncodedValue = try! JSONEncoder().encode(MyValue.integer(123456))
let theEncodedString = String(data: theEncodedValue, encoding: .utf8)
let theDecodedValue = try! JSONDecoder().decode(MyValue.self, from: theEncodedValue)

但是,这在编码阶段给了我一个错误,如下所示:

 "Top-level MyValue encoded as number JSON fragment."

问题似乎是,无论出于何种原因,

JSONEncoder
都不允许将不是可识别原语的顶级类型编码为单个原语值。如果我将
singleValueContainer()
更改为
unkeyedContainer()
那么它工作得很好,当然,结果
JSON
是一个数组,而不是单个值,或者我可以使用带键的容器,但这会生成一个带有增加了密钥的开销。

我在这里尝试用单值容器做的事情是不可能的吗?如果没有,我可以使用一些解决方法吗?

我的目标是以最小的开销制作我的类型

Codable
,而不仅仅是
JSON
(解决方案应该支持任何有效的
Encoder
/
Decoder
)。

swift codable
2个回答
13
投票

有一个错误报告:

https://bugs.swift.org/browse/SR-6163

SR-6163:JSONDecoder 无法解码 RFC 7159 JSON

基本上,自 RFC-7159 以来,像

123
这样的值是有效的 JSON,但
JSONDecoder
不支持它。您可以跟进错误报告,以查看未来的任何修复。 [该错误已从 iOS 13 开始修复。]

#失败的地方#

它在以下代码行中失败,您可以在其中看到如果对象不是数组也不是字典,它将失败:

https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/JSONSerialization.swift#L120

open class JSONSerialization : NSObject {
        //...

        // top level object must be an Swift.Array or Swift.Dictionary
        guard obj is [Any?] || obj is [String: Any?] else {
            return false
        }

        //...
} 

#解决方法#

您可以使用

JSONSerialization
,并带有选项:
.allowFragments
:

let jsonText = "123"
let data = Data(jsonText.utf8)

do {
    let myString = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
    print(myString)
}
catch {
    print(error)
}

编码为键值对

最后,您还可以让 JSON 对象如下所示:

{ "integer": 123456 }

{ "string": "potatoe" }

为此,您需要执行以下操作:

import Foundation 

enum MyValue {
    case integer(Int)
    case string(String)
}

extension MyValue: Codable {
    
    enum CodingError: Error { 
        case decoding(String) 
    }
    
    enum CodableKeys: String, CodingKey { 
        case integer
        case string 
    }

    init(from decoder: Decoder) throws {

        let values = try decoder.container(keyedBy: CodableKeys.self)

        if let integer = try? values.decode(Int.self, forKey: .integer) {
            self = .integer(integer)
            return
        }

        if let string = try? values.decode(String.self, forKey: .string) {
            self = .string(string)
            return
        }

        throw CodingError.decoding("Decoding Failed")
    }


    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodableKeys.self)

        switch self {
            case let .integer(i):
            try container.encode(i, forKey: .integer)
            case let .string(s):
            try container.encode(s, forKey: .string)
        }
    }

}

let theEncodedValue = try! JSONEncoder().encode(MyValue.integer(123456))
let theEncodedString = String(data: theEncodedValue, encoding: .utf8)
print(theEncodedString!) // { "integer": 123456 }
let theDecodedValue = try! JSONDecoder().decode(MyValue.self, from: theEncodedValue)

0
投票

除了上面显示字典或键值容器的答案之外,当您不想在 json 中包含

.integer
.string
时,还有一个单值容器。

enum MyValue {
    case integer(Int)
    case string(String)

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .string(let value):
            try container.encode(value)
        case .integer(let value):
            try container.encode(value)
    }
}

上面允许允许整数或字符串

{ 
    "test1": 42,
    "test2": "hello"
}

另一种选择是,如果您想要一个数组而不是字典或单个值,请使用

unkeyedContainer()

enum MyValue {
    case integer(Int)
    case string(String)

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        switch self {
        case .string(let value):
            try container.encode(value)
        case .integer(let value):
            try container.encode(value)
    }
}

输出如下:

 { 
    "test1": [42],
    "test2": ["hello"]
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.