JSONDecoder
的一个扩展,允许解码 JSON 片段。
只需将 allowFragments: true
传递给 JSONDecoder.decode(_:from:)
调用,即可解码 JSON 片段。
import Foundation
import JSONFragmentDecoding
let data = Data("10".utf8)
let decoded = try JSONDecoder().decode(Int.self, from: data, allowFragments: true)
print(decoded) // 10
老实说,它非常轻量,您可以直接将 /Sources/JSONFragmentDecoding/JSONFragmentDecoding.swift
拖放到您的项目中即可使用。
将以下依赖项添加到您的 Package.swift
文件中
dependencies: [
.package(url: "https://github.com/hamishknight/JSON-Fragment-Decoding.git", from: "0.1.0")
],
然后将依赖项添加到任何需要使用它的目标中
targets: [
.target(
name: "SomeTarget",
dependencies: ["JSON-Fragment-Decoding"]),
]
将以下内容添加到您的 Cartfile
github "hamishknight/JSON-Fragment-Decoding" ~> 0.1