illescasDaniel / DynamicJson

An enjoyable way to manage json objects in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

An enjoyable way to manage json objects in Swift

License:MIT License


Languages

Language:Swift 100.0%