Giter VIP home page Giter VIP logo

optjson's Introduction

#OptJSON

A simple and safe approach to work with JSON objects hierarchies in Swift

Because of the strict static typing working with dynamic data structures (like JSON) is pain in Swift:

let jsonObject = [
  "name": "John",
  "age": 32,
  "phoneNumbers" = [
    [
      "type": "home",
      "number": "646 555-4567"
    ]
  ]
]

// thanks to runtime bridging we can downcast to Foundation types
if let person = nsJSON as? NSDictionary {
  if let phones = person["phoneNumbers"] as? NSArray {
    if let phone = phones[0] as? NSDictionary {
      if let number = phone["number"] as? String {
        assert(number == "646 555-4567")
      }
    }
  }
}

// alternativley – using optional downcasting and optional chaining
let maybeNumber = (((jsonObject as? NSDictionary)?["phoneNumbers"] as? NSArray)?[0] as? NSDictionary)?["number"] as? NSString
assert(maybeNumber == "646 555-4567")

With OptJSON traversing JSON hierarchy is easy:

let maybeNumber = JSON(jsonObject)?[key:"phoneNumbers"]?[index:0]?[key:"number"] as? NSString
XCTAssert(maybeNumber == "646 555-4567")

OptJSON works by introducing universal protocol JSONValue, which enables JSON-specific subscripts to access dictionaries and arrays elements. This protocol is then adopted by all JSON-related Foundation types.

OptJSON enables:

  • compact and readable code
  • no excessive downcasting
  • easy subscripts chaining to go deep in a single expression
  • safe runtime – you'll get nil if your chain doesn't match actual JSON hierarchy
  • minimal CPU/memory overhead
  • work with native JSON object representation - no extra conversion needed

optjson's People

Contributors

odnoletkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.