Swiftpack.co - illescasDaniel/DynamicJson as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by illescasDaniel.
illescasDaniel/DynamicJson 1.0.1
An enjoyable way to manage json objects in Swift
⭐️ 3
🕓 3 years ago
.package(url: "https://github.com/illescasDaniel/DynamicJson.git", from: "1.0.1")

DynamicJson

Swift version Version license

An enjoyable way to manage json objects.

Examples:

let json = """
{
  "name": "Daniel",
  "age": 22,
  "favouriteFoods": ["pizza"]
}
"""

// Get a json from a raw string, from data or from an object (like a dictionary)
let danielJson = Json(rawJsonString: json)

// Get stuff
print(danielJson.name.string ?? "")
print(danielJson.favouriteFoods[0].string ?? "")

// You can also change the values inside
danielJson.favouriteFoods = ["hamburger", "you"]
print(danielJson.favouriteFoods.array ?? [])
print(danielJson)

Easily convert to a Codable object.

struct Person: Codable {
  let name: String
  let age: UInt
  var favouriteFoods: [String]
}

let daniel: Person? = danielJson.decoded()
print(daniel ?? "nope")

Fast Json traversal with NSDictionary#value(forKeyPath: String).

let otherJson = """
{
  "fullName": {
    "firstName": "Daniel",
    "lastName": "Illescas",
    "parent": {
      "fullName": {
        "firstName": "Peter",
        "lastName": "Illescas"
      }
    }
  },
  "age": 22
}
"""
let me = Json(rawJsonString: otherJson)
print(me[\.fullName.parent.fullName.firstName].string ?? "nope")
// or
print(me["fullName.parent.fullName.firstName"] ?? "nope")

GitHub

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

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