Skip to content

Commit

Permalink
Add methods to get raw NSData and String. Fix #70
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTangPL committed Oct 15, 2014
1 parent 093256d commit 5ef814e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/SwiftyJSON.swift
Expand Up @@ -393,6 +393,18 @@ extension JSON: RawRepresentable {
public var rawValue: AnyObject {
return self.object
}

public func rawData(options opt: NSJSONWritingOptions = .PrettyPrinted, error: NSErrorPointer = nil) -> NSData? {
return NSJSONSerialization.dataWithJSONObject(self.object, options: opt, error:error)
}

public func rawString(encoding: UInt = NSUTF8StringEncoding, options opt: NSJSONWritingOptions = .PrettyPrinted, error: NSErrorPointer = nil) -> String? {
if let data = self.rawData(options: opt, error:error) {
return NSString(data: data, encoding: encoding)
} else {
return nil
}
}
}

//MARK: - Printable, DebugPrintable
Expand Down
4 changes: 4 additions & 0 deletions SwiftyJSON.xcodeproj/project.pbxproj
Expand Up @@ -14,6 +14,7 @@
A819C49F19E2EE5B00ADCC3D /* SubscriptTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A819C49E19E2EE5B00ADCC3D /* SubscriptTests.swift */; };
A819C4A119E37FC600ADCC3D /* PrintableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A819C4A019E37FC600ADCC3D /* PrintableTests.swift */; };
A8491E1E19CD6DAE00CCFAE6 /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */; };
A863BE2819EED46F0092A41F /* RawTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A863BE2719EED46F0092A41F /* RawTests.swift */; };
A86BAA0E19EBC32B009EAAEB /* PerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */; };
A87080E419E3C2A600CDE086 /* SequenceTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87080E319E3C2A600CDE086 /* SequenceTypeTests.swift */; };
A87080E619E3DF7800CDE086 /* ComparableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A87080E519E3DF7800CDE086 /* ComparableTests.swift */; };
Expand Down Expand Up @@ -46,6 +47,7 @@
A819C4A019E37FC600ADCC3D /* PrintableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrintableTests.swift; sourceTree = "<group>"; };
A82A1C0D19D922DC009A653D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A8491E1D19CD6DAE00CCFAE6 /* SwiftyJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSON.swift; sourceTree = "<group>"; };
A863BE2719EED46F0092A41F /* RawTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawTests.swift; sourceTree = "<group>"; };
A86BAA0D19EBC32B009EAAEB /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = "<group>"; };
A87080E319E3C2A600CDE086 /* SequenceTypeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SequenceTypeTests.swift; sourceTree = "<group>"; };
A87080E519E3DF7800CDE086 /* ComparableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComparableTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -126,6 +128,7 @@
A87080E519E3DF7800CDE086 /* ComparableTests.swift */,
A87080E919E43C0700CDE086 /* StringTests.swift */,
A87080E719E439DA00CDE086 /* NumberTests.swift */,
A863BE2719EED46F0092A41F /* RawTests.swift */,
A8B66C8B19E51D6500540692 /* DictionaryTests.swift */,
A8B66C8D19E52F4200540692 /* ArrayTests.swift */,
2E4FEFEB19575BE100351305 /* Supporting Files */,
Expand Down Expand Up @@ -262,6 +265,7 @@
A86BAA0E19EBC32B009EAAEB /* PerformanceTests.swift in Sources */,
A819C49919E1B10300ADCC3D /* RawRepresentableTests.swift in Sources */,
A819C49F19E2EE5B00ADCC3D /* SubscriptTests.swift in Sources */,
A863BE2819EED46F0092A41F /* RawTests.swift in Sources */,
A885D1D219CF1EE6002FD4C3 /* BaseTests.swift in Sources */,
A8B66C8E19E52F4200540692 /* ArrayTests.swift in Sources */,
A8B66C8C19E51D6500540692 /* DictionaryTests.swift in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions Tests/PerformanceTests.swift
Expand Up @@ -85,4 +85,17 @@ class PerformanceTests: XCTestCase {
}
}
}

func testRawStringMethodPerformance() {
let json = JSON(data:testData)
self.measureBlock() {
for _ in 1...100 {
autoreleasepool{
var string = json.rawString()
XCTAssertTrue(string != nil)
}
}
}
}

}
54 changes: 54 additions & 0 deletions Tests/RawTests.swift
@@ -0,0 +1,54 @@
// RawTests.swift
//
// Copyright (c) 2014 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 UIKit
import XCTest
import SwiftyJSON

class RawTests: XCTestCase {

func testArray() {
let json:JSON = [1, "2", 3.12, NSNull(), true, ["name": "Jack"]]
let data = json.rawData()
let string = json.rawString()
XCTAssertTrue (data != nil)
XCTAssertTrue (string!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0)
println(string!)
}

func testDictionary() {
let json:JSON = ["number":111111.23456789, "name":"Jack", "list":[1,2,3,4], "bool":false, "null":NSNull()]
let data = json.rawData()
let string = json.rawString()
XCTAssertTrue (data != nil)
XCTAssertTrue (string!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0)
println(string!)
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
// Put the code you want to measure the time of here.
}
}

}

0 comments on commit 5ef814e

Please sign in to comment.