Giter Club home page Giter Club logo

jsonparser's Introduction

JSONParser

Elegant, type-safe JSON parsing in Swift

If you're a clever programmer, then you probably use Codable types wherever you can do so. However, sometimes you need to parse some JSON data the structure of which you don't control. Previously, this was a painful operation:

let object = try (JSONSerialization.jsonObject(with: data) as! [String: Any])
guard let foo = object["foo"] as? Int, let bar = object["bar"] as? String, let array = object["array"] as? [Any] else {
	return
}
guard let number = array[0] as? Double, let word = array[1] as? String, let dictionary = array[3] as? [String: Int] else {
	return
}
guard let hello = dictionary["hello"] else {
	return
}

With JSONParser, this process is elegant and effortless:

let parser = data.dictionaryParser // `data` is an instance of `Data` from `Foundation`
let foo = parser["foo", as: Int.self]
let bar = parser["bar", as: String.self]
let number = parser[arrayAt: "array"]?[0, as: Double.self]
let word = parser[arrayAt: "array"]?[1, as: String.self]
let hello = parser[arrayAt: "array"]?[dictionaryAt: 2]?["hello", as: Int.self]

You can also iterate over heterogenous JSON arrays and dictionaries:

try data.arrayParser.iterate { (proxy) in
	if let value = proxy.get(as: Int.self) {
		print(value * 2)
	} else if let value = proxy.get(as: Bool.self) {
		print(!value)
	}
}

JSONParser can work with other providers, not just Data from Foundation. Just have your custom class or struct conform to the JSONProvider protocol. It has only one mandatory interface member:

// This `Set` extension is already provided in the JSONParser package; use it as a guide for writing your own extensions for other types
extension Set: JSONProvider {

	public var parser: ArrayJSONParser? {
		get {
			return Array(self).parser
		}
	}

}

Convenient functions and subscript accessors are automatically provided for free, requiring no manual implementation:

let array = [
	0,
	1,
	[
		"2": true
	]
] as [AnyHashable]
let set = Set(array)
try set.iterate { (proxy) in
	print(proxy.get(as: Int.self) ?? proxy.get(as: [String: Bool].self)!)
}

JSONParser builds on top of Swift's robust generic-typing system to provide an easy, intuitive API for interacting with JSON data. It has no external dependencies and relies solely on Foundation APIs, so it works on Linux and Windows in addition to the various Apple platforms.

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.