Skip to content
forked from vdka/JSON

The fastest Native Swift JSON available

License

Notifications You must be signed in to change notification settings

robertjpayne/JSON

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON

Language

This is Not just Another Swift JSON Package. This is the Swift JSON Package. When you are transforming directly to models this framework is faster than Foundation.JSONSerialization.

Time to Parse and initialize a struct from a sample 432KB JSON file.

Foundation vdka/json
Time 149.7ms 27.03ms
LOC 71 35

Type safety can get you a long way, lets see how you can use it for your applications.

enum Currency: String { case AUD, EUR, GBP, USD }

struct Money {
  var minorUnits: Int
  var currency: Currency

}

struct Person {
  var name: String
  var age: Int
  var accountBalances: [Money]
  var petName: String?
}

extension Money: JSONConvertible {

  func encoded() -> JSON {
    return
      [
        "minorUnits": minorUnits,
        "currencyCode": currency.encoded()
    ]
  }

  init(json: JSON) throws {
    self.minorUnits = try json.get("minorUnits")
    self.currency   = try json.get("currencyCode")
  }
}


extension Person: JSONConvertible {

  func encoded() -> JSON {
    return
      [
        "name": name,
        "age": age,
        "accountBalances": accountBalances.encoded()
    ]
  }

  init(json: JSON) throws {
    self.name             = try json.get("name")
    self.age              = try json.get("age")
    self.accountBalances  = try json.get("accountBalances")
    self.petName          = try json.get("petName")
  }
}

Alternative Access Patterns

Subscripting

json["pet"]?["vets"]?[0]?["name"]?.string

json["pet"]["vets"][0]["name"].string is also valid however this one has to perform runtime type checks making it much slower. This will probably be deprecated at least until we can constrain Generic Type extensions to Specific types.

About

The fastest Native Swift JSON available

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.3%
  • Shell 1.7%