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

时间:2018-05-09 15:40:31

标签: swift codable

我正在为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)。

1 个答案:

答案 0 :(得分:7)

有一个错误报告:

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

  

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

基本上,由于RFC-7159,123之类的值是有效的JSON,但JSONDecoder不支持它。您可以跟进错误报告,以查看此处的任何修复。

失败的地方

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

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)
相关问题