将未知对象解码为Swift中的原始形式
我有Rawjson类,它曾经存储和操作未知结构的对象,无法解码。
public indirect enum RawJSON: Codable, Hashable {
case number(Double)
case string(String)
case bool(Bool)
case dictionary([String: RawJSON])
case array([RawJSON])
case `nil`
static let double = number
public init(from decoder: Decoder) throws {
let singleValueContainer = try decoder.singleValueContainer()
if let value = try? singleValueContainer.decode(Bool.self) {
self = .bool(value)
return
} else if let value = try? singleValueContainer.decode(String.self) {
self = .string(value)
return
} else if let value = try? singleValueContainer.decode(Double.self) {
self = .number(value)
return
} else if let value = try? singleValueContainer.decode([String: RawJSON].self) {
self = .dictionary(value)
return
} else if let value = try? singleValueContainer.decode([RawJSON].self) {
self = .array(value)
return
} else if singleValueContainer.decodeNil() {
self = .nil
return
}
throw DecodingError
.dataCorrupted(
DecodingError
.Context(codingPath: decoder.codingPath, debugDescription: "Could not find reasonable type to map to JSONValue")
)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .number(value): try container.encode(value)
case let .bool(value): try container.encode(value)
case let .string(value): try container.encode(value)
case let .array(value): try container.encode(value)
case let .dictionary(value): try container.encode(value)
case .nil: try container.encodeNil()
}
}
}
public extension RawJSON {
func dictionary(with value: RawJSON?, forKey key: String) -> RawJSON? {
guard case var .dictionary(content) = self else { return nil }
content[key] = value
return .dictionary(content)
}
}
public indirect enum RawJSON: Codable, Hashable {
case number(Double)
case string(String)
case bool(Bool)
case dictionary([String: RawJSON])
case array([RawJSON])
case `nil`
static let double = number
public init(from decoder: Decoder) throws {
let singleValueContainer = try decoder.singleValueContainer()
if let value = try? singleValueContainer.decode(Bool.self) {
self = .bool(value)
return
} else if let value = try? singleValueContainer.decode(String.self) {
self = .string(value)
return
} else if let value = try? singleValueContainer.decode(Double.self) {
self = .number(value)
return
} else if let value = try? singleValueContainer.decode([String: RawJSON].self) {
self = .dictionary(value)
return
} else if let value = try? singleValueContainer.decode([RawJSON].self) {
self = .array(value)
return
} else if singleValueContainer.decodeNil() {
self = .nil
return
}
throw DecodingError
.dataCorrupted(
DecodingError
.Context(codingPath: decoder.codingPath, debugDescription: "Could not find reasonable type to map to JSONValue")
)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .number(value): try container.encode(value)
case let .bool(value): try container.encode(value)
case let .string(value): try container.encode(value)
case let .array(value): try container.encode(value)
case let .dictionary(value): try container.encode(value)
case .nil: try container.encodeNil()
}
}
}
public extension RawJSON {
func dictionary(with value: RawJSON?, forKey key: String) -> RawJSON? {
guard case var .dictionary(content) = self else { return nil }
content[key] = value
return .dictionary(content)
}
}
我想要字符串的原始形式。
如何将其解码为原始字符串?
let RAWJSONArray = [RawJSON.string("A"),RawJSON.string("B"),RawJSON.string("C")]
let arrayString : [String] = []
for item in RAWJSONArray {
arrayString.append(item) <-- error No exact matches in call to instance method 'append'
}
我使用客户端从StreamChat API使用此类来客户聊天,他们存储了额外的数据并将其转换为RAWJSON类型对象并将其发送到API。
I have RAWJSON Class which Used to store and operate objects of unknown structure that's not possible to decode.
public indirect enum RawJSON: Codable, Hashable {
case number(Double)
case string(String)
case bool(Bool)
case dictionary([String: RawJSON])
case array([RawJSON])
case `nil`
static let double = number
public init(from decoder: Decoder) throws {
let singleValueContainer = try decoder.singleValueContainer()
if let value = try? singleValueContainer.decode(Bool.self) {
self = .bool(value)
return
} else if let value = try? singleValueContainer.decode(String.self) {
self = .string(value)
return
} else if let value = try? singleValueContainer.decode(Double.self) {
self = .number(value)
return
} else if let value = try? singleValueContainer.decode([String: RawJSON].self) {
self = .dictionary(value)
return
} else if let value = try? singleValueContainer.decode([RawJSON].self) {
self = .array(value)
return
} else if singleValueContainer.decodeNil() {
self = .nil
return
}
throw DecodingError
.dataCorrupted(
DecodingError
.Context(codingPath: decoder.codingPath, debugDescription: "Could not find reasonable type to map to JSONValue")
)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .number(value): try container.encode(value)
case let .bool(value): try container.encode(value)
case let .string(value): try container.encode(value)
case let .array(value): try container.encode(value)
case let .dictionary(value): try container.encode(value)
case .nil: try container.encodeNil()
}
}
}
public extension RawJSON {
func dictionary(with value: RawJSON?, forKey key: String) -> RawJSON? {
guard case var .dictionary(content) = self else { return nil }
content[key] = value
return .dictionary(content)
}
}
public indirect enum RawJSON: Codable, Hashable {
case number(Double)
case string(String)
case bool(Bool)
case dictionary([String: RawJSON])
case array([RawJSON])
case `nil`
static let double = number
public init(from decoder: Decoder) throws {
let singleValueContainer = try decoder.singleValueContainer()
if let value = try? singleValueContainer.decode(Bool.self) {
self = .bool(value)
return
} else if let value = try? singleValueContainer.decode(String.self) {
self = .string(value)
return
} else if let value = try? singleValueContainer.decode(Double.self) {
self = .number(value)
return
} else if let value = try? singleValueContainer.decode([String: RawJSON].self) {
self = .dictionary(value)
return
} else if let value = try? singleValueContainer.decode([RawJSON].self) {
self = .array(value)
return
} else if singleValueContainer.decodeNil() {
self = .nil
return
}
throw DecodingError
.dataCorrupted(
DecodingError
.Context(codingPath: decoder.codingPath, debugDescription: "Could not find reasonable type to map to JSONValue")
)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .number(value): try container.encode(value)
case let .bool(value): try container.encode(value)
case let .string(value): try container.encode(value)
case let .array(value): try container.encode(value)
case let .dictionary(value): try container.encode(value)
case .nil: try container.encodeNil()
}
}
}
public extension RawJSON {
func dictionary(with value: RawJSON?, forKey key: String) -> RawJSON? {
guard case var .dictionary(content) = self else { return nil }
content[key] = value
return .dictionary(content)
}
}
I want the array of string in its original form.
how can i decode it to original array of strings?
let RAWJSONArray = [RawJSON.string("A"),RawJSON.string("B"),RawJSON.string("C")]
let arrayString : [String] = []
for item in RAWJSONArray {
arrayString.append(item) <-- error No exact matches in call to instance method 'append'
}
I used this class from streamchat API using client to client chat they store extra data and convert it to RAWJSON type object and send it to the API.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这做您需要的吗?
也许您真的只想要琴弦?
Does this do what you need?
or perhaps you literally want the strings only?