bignerdranch / Freddy

A reusable framework for parsing JSON in Swift.
MIT License
1.09k stars 119 forks source link

Did support for reading a top level array of dictionaries disappear in 3.0? #231

Closed edasque closed 7 years ago

edasque commented 7 years ago

Just before 3.0 was released, https://github.com/bignerdranch/Freddy/issues/211 highlighted the use of JSON.arrayOf or JSON.array without an at: parameter but in 3.0 I can't find them.

What am I missing?

    let JSONdoc_as_string = "[\"One\",\"True\",\"Ring\"]"

    do {

      let json = try JSON(jsonString: JSONdoc_as_string)

      // let array_of_strings = try json.array...

      NSLog("Success")

      // do something with `success`

    } catch {
      // do something with the error
      NSLog("Failure")
    }
mdmathias commented 7 years ago

You can do what you're describing like so:

let JSONdoc_as_string = "[\"One\",\"True\",\"Ring\"]"

do {
    let json = try JSON(jsonString: JSONdoc_as_string)
    let stringArray = try json.getArray()
    XCTAssertTrue(stringArray == ["One", "True", "Ring"], "String arrays should match")
} catch {
    print("Failure: \(error)")
}

path is a variadic parameter:

public func getArray(at path: JSONPathType...) throws -> [JSON] {
    return try JSON.getArray(from: value(at: path))
}

This allows you to pass nothing to it, which can be used to grab something at the top level.