Latest 0.1.2
Homepage https://github.com/borchero/WebParsing
License MIT
Platforms ios 9.0
Authors

WebParsing

WebParsing is a framework written entirely in Swift for working with JSON and XML on a very high abstraction level.

Documentation

Full documentation available at docs/index.html when downloading or online here. The documentation was auto-generated using jazzy.

Dependency on CoreUtility was removed in the last commit.

WebParsing is now available for CocoaPods! Simply include 'WebParsing' in your podfile.

How to use WebParsing

JSON can be initialized using several initializers:

// Reading JSON from URLs
let url: URL = // some URL
let json = WPJson(readingFrom: url)

// Reading JSON from data
let data: Data = // some data
let json = WPJson(reading: data)

// Reading JSON from string
let jsonString: String = // some string
let json = WPJson(reading: jsonString)

Suppose, we have a file like the following as Data:

{
    "people": [
        {
            "prename": "John",
            "surname": "Smith",
            "age": 45,
            "gender": "male"
        }
    ]
}

Reading it becomes very easy:

let json = WPJson(reading: data)
print(json["people"][0]["prename"].string) // prints 'John'

But it can in fact be done even easier when using the WPEncodable protocol. A struct wrapping a person could look like the following:

struct Person: WPEncodable {
    let prename: String
    let surname: String
    let gender: String
    let age: Int

    init(coder decoder: WPDecoder) {
        prename = decoder.decode(forKey: "prename")
        surname = decoder.decode(forKey: "surname")
        gender = decoder.decode(forKey: "gender")
        age = decoder.decode(forKey: "age")
    }

    func encode(with encoder: WPEncoder) {
        encoder.encode(prename, forKey: "prename")
        encoder.encode(surname, forKey: "surname")
        encoder.encode(gender, forKey: "gender")
        encoder.encode(age, forKey: "age")
    }
}

Taking the json from the previous code snippet, we can extract the values even simpler.

let people: [Person] = WPJsonDecoder.decode(json["people"])
let john = people[0]
print(john.prename) // prints 'John'

But what if the API we receive our JSON from suddenly changes into XML? And now returns a file like the following (omitting the header):

<root>
    <people gender="male">
        <prename>John</prename>
        <surname>Smith</surname>
        <age>45</age>
    </people>
</root>

When we have the xml file as xmlData, we can do pretty much the same we did before.

let xml = WPXml(reading: xmlData, infersTypes: true)

The infersTypes parameter is true by default: In XML, age would normally be interpreted as String, however, checking this option makes the parser interpret it as number.
The next thing we notice: We don’t have an array anymore. And internally, no array is actually stored. However, we should be able to use the same code for 1 object in the array as for * objects. And in fact, index 0 can be accessed always since the XML Parser can’t know if it should be an array semantically.
Reading the prename is therefore simple:

print(xml["people"][0]["prename"].string) // prints 'John'

We can also use almost the same Person class: We have to replace:

// encoder.encode(gender, forKey: "gender") by
encoder.encodeAttribute(gender, forKey: "gender")
// and gender = decoder.decode(forKey: "gender") by
gender = decoder.decodeAttribute(forKey: "gender")

Then we can use:

let people: [Person] = WPXmlDecoder.decode(xml["people"])
let john = people[0]
print(john.gender) // prints 'male'

Latest podspec

{
    "name": "CoreStructures",
    "version": "0.1.2",
    "summary": "CoreStructures is a framework to provide data structures missing in Swift's standard library.",
    "description": "CoreStructures is a framework to provide data structures missing in Swift's standard library.n                        It will be extended in the near future.",
    "homepage": "https://github.com/borchero/WebParsing",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "borchero": "[email protected]"
    },
    "source": {
        "git": "https://github.com/borchero/CoreStructures.git",
        "tag": "0.1.2"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "CoreStructures/**/*",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This