-
Notifications
You must be signed in to change notification settings - Fork 3
JSON
The most common use for data extraction is when working with data from services. Usually, it comes in the form of JSON data.
Outlaw has a lightweight wrapper to JSONSerialization called JSON that adds the syntax sugar you already enjoy.
For validating an Array, Dictionary or Set to see if it can be serialized, its as easy as:
let isValid = JSON.isValid(collection)Outlaw has supplied a protocol JSONCollection that makes it even easier:
let isValid = collection.isValidJson()For serializing an Array, Dictionary or Set to Data, its as easy as:
let data = try! JSON.data(from: collection)Using the JSONCollection protocol, we can simplify it to:
let data = collection.jsonData()Using similar syntax for extracting data, you can parse your JSON using:
let jsonDictionary: [String: String] = try JSON.value(from: data)
let jsonArray: [String] = try JSON.value(from: data)What if your root data isn't an Array or Dictionary? No problem! Outlaw can parse fragments of any type that conforms to Value as well:
let string: String = try JSON.value(from: data)What about JSON from other sources that aren't Data?
No worries! Outlaw provides methods to parse JSON from InputStream, String and URL.