Swiftpack.co - withertech/JSONLoader as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by withertech.
withertech/JSONLoader 1.0
A simple way to load JSON from the main bundle in Swift
⭐️ 6
🕓 3 years ago
.package(url: "https://github.com/withertech/JSONLoader.git", from: "1.0")

JSONLoader

License: MIT Swift-5.1

A simple Swift library to quickly load JSON from your local bundle to a Codable object.

How to use

Simply define your struct as Codable and then when you need to map your JSON file to that object just call the loadFromBundle function. If you want to fetch JSON then parse it, you can call loadFromURL.

Example

Say you had a JSON file named people.json like this:

[
  {
    "id": 1,
    "name": "Jane"
  },
  {
    "id": 2,
    "name": "John"
  }
]

Now you need a data model like so:

struct Person: Codable {
   var id: Int
   var name: String
}

Now you all you need to do to populate a array of these is call the load method (After importing JSONLoader):

do {
    var people: [Person] = try loadFromBundle("people")
} catch let err {
    //Alert
    print(err.localizedDescription)
}

Installation

To install, add a package dependency to this repo in Xcode.

GitHub

link
Stars: 6
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

First Version
4 years ago

This is the FIRST VERSION of JSONLoader! 🎉

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics