LiteJSONConvertible
LiteJSONConvertible - A minimalistic approach to JSON parsing
Install / Use
/learn @andrea-prearo/LiteJSONConvertibleREADME
#LiteJSONConvertible
A minimalistic approach to JSON parsing
Features
- Gereric protocol with associated type
- Generic functions
- Functional composition
Requirements
- iOS 8.0 or newer
- Swift 2.2 or newer
- CocoaPods 0.38 or newer
Installation
Add the following line to your Podfile to be able to install LiteJSONConvertible using CocoaPods:
pod 'LiteJSONConvertible', '~> 0.0.5'
You also need to make sure you're opting into using frameworks and targetin iOS8 or newer:
use_frameworks!
platform :ios, '8.0'
Usage
Array of primitive types
String
["item1", "item2", "item3", "item4"]
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) {
let items = json.map(String.decode)
}
Bool
[true, false, true, false]
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSON] {
let items = json.map(Bool.decode)
}
Int
[1001, 1002, 1003, 1004]
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSON] {
let items = json.map(Int.decode)
}
Float
[1001.1, 1002.2, 1003.3, 1004.4]
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSON] {
let items = json.map(Float.decode)
}
Double
[1001.1, 1002.2, 1003.3, 1004.4]
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSON] {
let items = json.map(Double.decode)
}
Complex JSON content
Given a specific JSON content
{
"locations": [{
"label": "Home",
"data": {
"address": "6925 Felicity Coves",
"city": "East Davin",
"state": "Washington",
"country": "USA",
"zipCode": "22998-1456"
}
},
{
"label": "Work",
"data": {
"address": "0506 Gretchen River",
"city": "Huntington Beach",
"state": "Connecticut",
"country": "USA",
"zipCode": "61182-9561"
}
}]
}
define the appropriate classes/structs to parse it:
import LiteJSONConvertible
struct LocationData {
let address: String?
let city: String?
let state: String?
let country: String?
let zipCode: String?
init(address: String?,
city: String?,
state: String?,
country: String?,
zipCode: String?) {
self.address = address
self.city = city
self.state = state
self.country = country
self.zipCode = zipCode
}
}
extension LocationData: JSONDecodable {
static func decode(json: JSON) -> LocationData? {
return LocationData(
address: json <| "address",
city: json <| "city",
state: json <| "state",
country: json <| "country",
zipCode: json <| "zipCode")
}
}
struct Location {
let label: String?
let data: LocationData?
init(label: String?,
data: LocationData?) {
self.label = label
self.data = data
}
}
extension Location: JSONDecodable {
static func decode(json: JSON) -> Location? {
return Location(
label: json <| "label",
data: json <| "data" >>> LocationData.decode)
}
}
Then, simply retrieve the JSON content as NSData (Web Service or file) and parse it:
// Retrieve the JSON content as NSData
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) {
let locations = json <|| "locations" >>> Location.decode
}
Notes
This library is just an experiment on minimalistic JSON parsing using generic protocols. The basic ideas behind this approach are borrowed from the following articles:
- Parsing JSON in Swift
- Efficient JSON in Swift with Functional Concepts and Generics
- Real World JSON Parsing with Swift
Medium write-up
Related Skills
node-connect
350.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
