Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

A JSONDecoder extension to allow decoding JSON fragments

License

Notifications You must be signed in to change notification settings

hamishknight/JSON-Fragment-Decoding

Repository files navigation

JSON Fragment Decoding

A JSONDecoder extension to allow decoding JSON fragments.

Usage

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

Installation

Honestly it’s so lightweight that you could just drop /Sources/JSONFragmentDecoding/JSONFragmentDecoding.swift into your project and be on your way.

Swift PM

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"]),
]

Carthage

Add the following to your Cartfile:

github "hamishknight/JSON-Fragment-Decoding" ~> 0.1