A JSONDecoder
extension to allow decoding JSON fragments.
Simply pass allowFragments: true
to a JSONDecoder.decode(_:from:)
call, and you’ll be able to decode JSON fragments:
import Foundation
import JSONFragmentDecoding
let data = Data("10".utf8)
let decoded = try JSONDecoder().decode(Int.self, from: data, allowFragments: true)
print(decoded) // 10
Honestly it’s so lightweight that you could just drop /Sources/JSONFragmentDecoding/JSONFragmentDecoding.swift
into your project and be on your way.
Add the following dependancy to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/hamishknight/JSON-Fragment-Decoding.git", from: "0.1.0")
],
and then add the dependancy to any targets that need to use it:
targets: [
.target(
name: "SomeTarget",
dependencies: ["JSON-Fragment-Decoding"]),
]
Add the following to your Cartfile
:
github "hamishknight/JSON-Fragment-Decoding" ~> 0.1