Skip to content

Commit

Permalink
Merge pull request SwiftyJSON#51 from tangplin/master
Browse files Browse the repository at this point in the history
Refactor!
  • Loading branch information
aemaeth-me committed Sep 22, 2014
2 parents 11f6b94 + 127a5c3 commit de1885d
Show file tree
Hide file tree
Showing 9 changed files with 969 additions and 508 deletions.
37 changes: 16 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ But while dealing with things that naturally implicit about types such as JSON,
Take the Twitter API for example: say we want to retrieve a user's "name" value of some tweet in Swift (according to Twitter's API https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline)

```JSON

[
{
......
Expand Down Expand Up @@ -34,6 +35,7 @@ Take the Twitter API for example: say we want to retrieve a user's "name" value
"in_reply_to_screen_name": null,
},
......]

```

The code would look like this:
Expand All @@ -53,6 +55,7 @@ if let statusesArray = jsonObject as? NSArray{
}

```

It's not good.

Even if we use optional chaining, it would also cause a mess:
Expand All @@ -65,56 +68,48 @@ if let userName = (((jsonObject as? NSArray)?[0] as? NSDictionary)?["user"] as?
}

```

An unreadable mess for something like this should really be simple!

##SwiftyJSON

With SwiftyJSON all you have to do is:

```swift
let json = JSONValue(dataFromNetworking)

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string{
//Now you got your value
}

```

And don't worry about the Optional Wrapping thing, it's done for you automatically

```swift
let json = JSONValue(dataFromNetworking)

let json = JSON(data: dataFromNetworking)
if let userName = json[999999]["wrong_key"]["wrong_name"].string{
//Calm down, take it easy, the ".string" property still produces the correct Optional String type with safety
}

```

```swift
let json = JSONValue(jsonObject)

let json = JSON(object: jsonObject)
switch json["user_id"]{
case .JString(let stringValue):
case .ScalarString(let stringValue):
let id = stringValue.toInt()
case .JNumber(let numberValue):
case .ScalarNumber(let numberValue):
let id = numberValue.integerValue
default:
println("ooops!!! JSON Data is Unexpected or Broken")

```

##Error Handling
```swift
let json = JSONValue(dataFromNetworking)["some_key"]["some_wrong_key"]["wrong_name"]
if json{
//JSONValue it self confirm to Protocol "LogicValue", with JSONValue.JInvalid produce false and others produce true
}else{
println(json)
//> JSON Keypath Error: Incorrect Keypath "some_wrong_key/wrong_name"
//It always tells you where your key starts went wrong
switch json{
case .JInvalid(let error):
//An NSError containing detailed error information
}
}
```
##Integration

CocoaPods is not fully supported for Swift yet, to use this library in your project you should:

1. for Projects just drag SwiftyJSON.swift to the project tree
Expand Down
44 changes: 22 additions & 22 deletions SwiftyJSON.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
objects = {

/* Begin PBXBuildFile section */
177C1FC4195C45FB0082AF3B /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF719575C2600351305 /* SwiftyJSON.swift */; };
177C1FC5195C45FC0082AF3B /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF719575C2600351305 /* SwiftyJSON.swift */; };
2E4FEFE119575BE100351305 /* SwiftyJSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 2E4FEFE019575BE100351305 /* SwiftyJSON.h */; settings = {ATTRIBUTES = (Public, ); }; };
2E4FEFE719575BE100351305 /* SwiftyJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2E4FEFDB19575BE100351305 /* SwiftyJSON.framework */; };
2E4FEFF819575C2600351305 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF719575C2600351305 /* SwiftyJSON.swift */; };
2E4FEFFB19575C3200351305 /* SwiftyJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF919575C3200351305 /* SwiftyJSONTests.swift */; };
2E4FEFFC19575C3200351305 /* Valid.JSON in Resources */ = {isa = PBXBuildFile; fileRef = 2E4FEFFA19575C3200351305 /* Valid.JSON */; };
2E4FF00D19575CE600351305 /* SwiftyJSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2E4FF00219575CE600351305 /* SwiftyJSON.framework */; };
2E4FF01B19575D0E00351305 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF719575C2600351305 /* SwiftyJSON.swift */; };
2E4FF01C19575D1100351305 /* SwiftyJSON.h in Headers */ = {isa = PBXBuildFile; fileRef = 2E4FEFE019575BE100351305 /* SwiftyJSON.h */; settings = {ATTRIBUTES = (Public, ); }; };
2E4FF01D19575D2D00351305 /* SwiftyJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E4FEFF919575C3200351305 /* SwiftyJSONTests.swift */; };
2E4FF01E19575D9400351305 /* Valid.JSON in Resources */ = {isa = PBXBuildFile; fileRef = 2E4FEFFA19575C3200351305 /* Valid.JSON */; };
A8491E1E19CD6DAE00CCFAE6 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */; };
A885D1D219CF1EE6002FD4C3 /* SwiftyJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A885D1D119CF1EE6002FD4C3 /* SwiftyJSONTests.swift */; };
A885D1D319CF1EE6002FD4C3 /* SwiftyJSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A885D1D119CF1EE6002FD4C3 /* SwiftyJSONTests.swift */; };
A885D1D419CF223F002FD4C3 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */; };
A885D1D519CF223F002FD4C3 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */; };
A885D1D619CF2240002FD4C3 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */; };
A885D1DC19CFCFF0002FD4C3 /* SwiftyJSONTests.json in Resources */ = {isa = PBXBuildFile; fileRef = A885D1DA19CFCFF0002FD4C3 /* SwiftyJSONTests.json */; };
A885D1DD19CFCFF0002FD4C3 /* SwiftyJSONTests.json in Resources */ = {isa = PBXBuildFile; fileRef = A885D1DA19CFCFF0002FD4C3 /* SwiftyJSONTests.json */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -44,11 +44,11 @@
2E4FEFE019575BE100351305 /* SwiftyJSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftyJSON.h; sourceTree = "<group>"; };
2E4FEFE619575BE100351305 /* SwiftyJSONTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftyJSONTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2E4FEFEC19575BE100351305 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2E4FEFF719575C2600351305 /* SwiftyJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSON.swift; sourceTree = "<group>"; };
2E4FEFF919575C3200351305 /* SwiftyJSONTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSONTests.swift; sourceTree = "<group>"; };
2E4FEFFA19575C3200351305 /* Valid.JSON */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Valid.JSON; sourceTree = "<group>"; };
2E4FF00219575CE600351305 /* SwiftyJSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftyJSON.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2E4FF00C19575CE600351305 /* SwiftyJSON-OSXTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftyJSON-OSXTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSON.swift; sourceTree = "<group>"; };
A885D1D119CF1EE6002FD4C3 /* SwiftyJSONTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSONTests.swift; sourceTree = "<group>"; };
A885D1DA19CFCFF0002FD4C3 /* SwiftyJSONTests.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = SwiftyJSONTests.json; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -108,8 +108,8 @@
2E4FEFDD19575BE100351305 /* SwiftyJSON */ = {
isa = PBXGroup;
children = (
2E4FEFF719575C2600351305 /* SwiftyJSON.swift */,
2E4FEFE019575BE100351305 /* SwiftyJSON.h */,
A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */,
2E4FEFDE19575BE100351305 /* Supporting Files */,
);
path = SwiftyJSON;
Expand All @@ -126,8 +126,8 @@
2E4FEFEA19575BE100351305 /* SwiftyJSONTests */ = {
isa = PBXGroup;
children = (
2E4FEFF919575C3200351305 /* SwiftyJSONTests.swift */,
2E4FEFFA19575C3200351305 /* Valid.JSON */,
A885D1DA19CFCFF0002FD4C3 /* SwiftyJSONTests.json */,
A885D1D119CF1EE6002FD4C3 /* SwiftyJSONTests.swift */,
2E4FEFEB19575BE100351305 /* Supporting Files */,
);
path = SwiftyJSONTests;
Expand Down Expand Up @@ -291,7 +291,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FEFFC19575C3200351305 /* Valid.JSON in Resources */,
A885D1DC19CFCFF0002FD4C3 /* SwiftyJSONTests.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -306,7 +306,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FF01E19575D9400351305 /* Valid.JSON in Resources */,
A885D1DD19CFCFF0002FD4C3 /* SwiftyJSONTests.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -317,33 +317,33 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FEFF819575C2600351305 /* SwiftyJSON.swift in Sources */,
A8491E1E19CD6DAE00CCFAE6 /* SwiftyJSON.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
2E4FEFE219575BE100351305 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FEFFB19575C3200351305 /* SwiftyJSONTests.swift in Sources */,
177C1FC4195C45FB0082AF3B /* SwiftyJSON.swift in Sources */,
A885D1D219CF1EE6002FD4C3 /* SwiftyJSONTests.swift in Sources */,
A885D1D419CF223F002FD4C3 /* SwiftyJSON.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
2E4FEFFD19575CE600351305 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FF01B19575D0E00351305 /* SwiftyJSON.swift in Sources */,
A885D1D519CF223F002FD4C3 /* SwiftyJSON.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
2E4FF00819575CE600351305 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2E4FF01D19575D2D00351305 /* SwiftyJSONTests.swift in Sources */,
177C1FC5195C45FC0082AF3B /* SwiftyJSON.swift in Sources */,
A885D1D319CF1EE6002FD4C3 /* SwiftyJSONTests.swift in Sources */,
A885D1D619CF2240002FD4C3 /* SwiftyJSON.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>485B50D0-253F-4C8E-82BA-848F815FC6D6</string>
<string>3A573BC4-D26F-4B75-AEB7-A02DA780E705</string>
<key>IDESourceControlProjectName</key>
<string>SwiftyJSON</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>E0A160A5-C602-42DC-8A38-C2994D32AAAE</key>
<string>ssh://github.com/garnett/SwiftyJSON.git</string>
<key>C861FC00CEE0F6A6BE81FCFF6785FAA78C58EBB3</key>
<string>https://github.com/lingoer/SwiftyJSON.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SwiftyJSON.xcodeproj/project.xcworkspace</string>
<string>SwiftyJSON.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>E0A160A5-C602-42DC-8A38-C2994D32AAAE</key>
<key>C861FC00CEE0F6A6BE81FCFF6785FAA78C58EBB3</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>ssh://github.com/garnett/SwiftyJSON.git</string>
<string>https://github.com/lingoer/SwiftyJSON.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>E0A160A5-C602-42DC-8A38-C2994D32AAAE</string>
<string>C861FC00CEE0F6A6BE81FCFF6785FAA78C58EBB3</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>E0A160A5-C602-42DC-8A38-C2994D32AAAE</string>
<string>C861FC00CEE0F6A6BE81FCFF6785FAA78C58EBB3</string>
<key>IDESourceControlWCCName</key>
<string>SwiftyJSON</string>
</dict>
Expand Down
20 changes: 17 additions & 3 deletions SwiftyJSON/SwiftyJSON.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
//
// SwiftyJSON.h
// SwiftyJSON
//
// Created by Dzianis Lebedzeu on 6/22/14.
// Copyright (c) 2014 Ruoyu Fu, Pinglin Tang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

@import Foundation;

Expand Down
Loading

0 comments on commit de1885d

Please sign in to comment.