<commit_before># iolcos
JSON parser/creator for Swift - for parsing, editing, creating and reconstituting of parsed data.
#Guiding Principles
The guiding principles of the parser are:

* it should be able to take any valid JSON dictionary or array
* it will enable the structure of the data to be interrogated without any foreknowledge of the values and keys contained
* JSON should remain editable and be able to be reconstituted easily
* internally the types used to manipulate the JSON are type safe and do not store AnyObject values
* the programmer implementing the parser can choose (and easily alter) the interpretation of type safety used for values
* JSON can not only be parsed but also created from scratch in a type safe manner
* structure of JSON can be further refined through adding API specific types. This can reduce the size of manipulated data for use in UITableView, etc., while still making it possible to edit and reconstitute.

#Naming
The name of this repository follows the logic of many other JSON repositories in that the name is derived from some connection to Jason, the Greek mythological hero. Iolcos was Jason's starting point in the myth of the Golden Fleece, and this JSON parser is a new starting point for the parsing of JSON in Swift.
#Capabilities and Requirements
The parser is capable of handling JSON that has been loaded as NSData and which follows an array or string : value dictionary format. (In other words standard JSON.) It is also capable of creating JSON data from scratch.

The code requires the latest Xcode 6.3 Beta installed with Swift 1.2
#How to Use
To use simply copy the contents of the Sources folder into your app or playground. It works with iOS and OS X.
##Parse JSON Data
To parse NSData simply use

`JSONParser.parseDictionary(json:NSData, restrictTypeChanges:Bool = true, anyValueIsNullable:Bool = true) -> JSONDictionary?`

or 

`JSONParser.parseArray(json:NSData, restrictTypeChanges:Bool = true, anyValueIsNullable:Bool = true)  -> JSONArray?`

Default settings do not allow changes of type but do allow values to become null. To enable changes of type change **restrictTypeChanges** to **false** and to prevent nullability of values change **anyValueIsNullable** to **false**.

These properties can be changed at any time, and of course if the JSON is parsed into a constant value then no changes will be possible anyway until the dictionary or array is copied to a variable.
##Retrieve and update values
In order to retrieve a value identify its type, e.g. `if let tName = jsonDictionary["trackName"]?.str {}` or `if let rCount = jsonDictionary["resultCount"]?.num {}`

Updates to results can be made like this `jsonDictionary["results"]?[0]?["trackName"] = "Something"`
#Creation of JSON
    
    var myJSON = JSONDictionary()
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One"
    // A string key with string value has been added
    myJSON["Fourth"] = myJSON
    // A copy of the JSONDictionary has been added creating a nest
    myJSON["Fourth"]?["Third"] = "Two"
    // a nested value is changed
    if let aStr = myJSON["Fourth"]?["Third"]?.str {
        println(aStr) // Two
    }
    // A value is extracted from the nested JSONDictionary
    myJSON.jsonData()?.writeToFile("/tmp/myJSON.json", atomically: false)
    // JSON has now been written to /tmp directory

Once the JSON has been created in this way, the file you created will contain text that looks like this:

`{"Fourth":{"Third":"Two"},"Third":"One"}`

Or similar (remember dictionaries are not ordered in the way that arrays are).
##Initialize with Dictionary<String,T>
It's also possible to initialize the JSONDictionary using a Swift Dictionary, e.g.

    let myDictionary = ["First":9,"Second":1,"Third":"Two"]
    var myJSON = JSONDictionary(dict:myDictionary)
    myJSON.jsonData()?.writeToFile("/tmp/myJSON.json", atomically: false)
    // JSON has now been written to /tmp directory

The created JSON:

`{"First":9,"Second":1,"Third":"Two"}`

Creating JSON using **iolcos** means that you can take advantage of all the associated methods and properties while forgetting about AnyObject and casting. It is also type safe.
#Type Safety
By default we cannot change type once it is set. For example:
    
    var myJSON = JSONDictionary()
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One" // "One"
    myJSON["Third"] = 1 // "One"
    myJSON["Third"] = "Two" // "Two"

But if we were to do this:
    
    var myJSON = JSONDictionary(restrictTypeChanges: false)
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One" // "One"
    myJSON["Third"] = 1 // 1
    myJSON["Third"] = "Two" // "Two"

Then type changes would be freely permitted. But this behaviour changes again if the `restrictTypeChanges` property is later set back to true:
<commit_msg>Name change to Aldwych<commit_after># Aldwych (formerly iolcos)
JSON parser/creator for Swift - for parsing, editing, creating and reconstituting of parsed data.
#Guiding Principles
The guiding principles of the parser are:

* it should be able to take any valid JSON dictionary or array
* it will enable the structure of the data to be interrogated without any foreknowledge of the values and keys contained
* JSON should remain editable and be able to be reconstituted easily
* internally the types used to manipulate the JSON are type safe and do not store AnyObject values
* the programmer implementing the parser can choose (and easily alter) the interpretation of type safety used for values
* JSON can not only be parsed but also created from scratch in a type safe manner
* structure of JSON can be further refined through adding API specific types. This can reduce the size of manipulated data for use in UITableView, etc., while still making it possible to edit and reconstitute.

#Naming
The name of this repository ~~follows the logic of many other JSON repositories in that the name is derived from some connection to Jason, the Greek mythological hero. Iolcos was Jason's starting point in the myth of the Golden Fleece, and this JSON parser is a new starting point for the parsing of JSON in Swift.~~ is randomly grabbed from a London street name. Really it makes no sense, because Aldwych is a one-way street and this parser is the opposite. It's all about working in two directions, but hey it sounded punchier than iolcos. (Note: a part of me wanted to call it Vodka &ndash; SwiftSummit 2015.) 
#Capabilities and Requirements
The parser is capable of handling JSON that has been loaded as NSData and which follows an array or string : value dictionary format. (In other words standard JSON.) It is also capable of creating JSON data from scratch.

The code requires the latest Xcode 6.3 Beta installed with Swift 1.2
#How to Use
To use simply copy the contents of the Sources folder into your app or playground. It works with iOS and OS X.
##Parse JSON Data
To parse NSData simply use

`JSONParser.parseDictionary(json:NSData, restrictTypeChanges:Bool = true, anyValueIsNullable:Bool = true) -> JSONDictionary?`

or 

`JSONParser.parseArray(json:NSData, restrictTypeChanges:Bool = true, anyValueIsNullable:Bool = true)  -> JSONArray?`

Default settings do not allow changes of type but do allow values to become null. To enable changes of type change **restrictTypeChanges** to **false** and to prevent nullability of values change **anyValueIsNullable** to **false**.

These properties can be changed at any time, and of course if the JSON is parsed into a constant value then no changes will be possible anyway until the dictionary or array is copied to a variable.
##Retrieve and update values
In order to retrieve a value identify its type, e.g. `if let tName = jsonDictionary["trackName"]?.str {}` or `if let rCount = jsonDictionary["resultCount"]?.num {}`

Updates to results can be made like this `jsonDictionary["results"]?[0]?["trackName"] = "Something"`
#Creation of JSON
    
    var myJSON = JSONDictionary()
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One"
    // A string key with string value has been added
    myJSON["Fourth"] = myJSON
    // A copy of the JSONDictionary has been added creating a nest
    myJSON["Fourth"]?["Third"] = "Two"
    // a nested value is changed
    if let aStr = myJSON["Fourth"]?["Third"]?.str {
        println(aStr) // Two
    }
    // A value is extracted from the nested JSONDictionary
    myJSON.jsonData()?.writeToFile("/tmp/myJSON.json", atomically: false)
    // JSON has now been written to /tmp directory

Once the JSON has been created in this way, the file you created will contain text that looks like this:

`{"Fourth":{"Third":"Two"},"Third":"One"}`

Or similar (remember dictionaries are not ordered in the way that arrays are).
##Initialize with Dictionary<String,T>
It's also possible to initialize the JSONDictionary using a Swift Dictionary, e.g.

    let myDictionary = ["First":9,"Second":1,"Third":"Two"]
    var myJSON = JSONDictionary(dict:myDictionary)
    myJSON.jsonData()?.writeToFile("/tmp/myJSON.json", atomically: false)
    // JSON has now been written to /tmp directory

The created JSON:

`{"First":9,"Second":1,"Third":"Two"}`

Creating JSON using **Aldwych** means that you can take advantage of all the associated methods and properties while forgetting about AnyObject and casting. It is also type safe.
#Type Safety
By default we cannot change type once it is set. For example:
    
    var myJSON = JSONDictionary()
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One" // "One"
    myJSON["Third"] = 1 // "One"
    myJSON["Third"] = "Two" // "Two"

But if we were to do this:
    
    var myJSON = JSONDictionary(restrictTypeChanges: false)
    // A JSONDictionary instance has been created
    myJSON["Third"] = "One" // "One"
    myJSON["Third"] = 1 // 1
    myJSON["Third"] = "Two" // "Two"

Then type changes would be freely permitted. But this behaviour changes again if the `restrictTypeChanges` property is later set back to true:

{'fraction_non_alphanumeric': 0.07856575288041319, 'fraction_numerical': 0.0027810885975367503, 'mean_word_length': 4.179526748971194, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3448384', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2553, 'n_words': 1375}