Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Xcode
#
.build/
build/
*.pbxuser
!default.pbxuser
Expand Down
16 changes: 8 additions & 8 deletions JsonSerializer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
807EB9581C6EFEC900BAA720 /* JsonParser+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "JsonParser+Foundation.swift"; sourceTree = "<group>"; };
80E3F7D41C602ACC007E1A94 /* Json.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Json.swift; path = Source/Json.swift; sourceTree = SOURCE_ROOT; };
80E3F7D51C602ACC007E1A94 /* JsonParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JsonParser.swift; path = Source/JsonParser.swift; sourceTree = SOURCE_ROOT; };
80E3F7D71C602ACC007E1A94 /* JsonSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JsonSerializer.swift; path = Source/JsonSerializer.swift; sourceTree = SOURCE_ROOT; };
807EB9581C6EFEC900BAA720 /* JsonParser+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = "JsonParser+Foundation.swift"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
80E3F7D41C602ACC007E1A94 /* Json.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = Json.swift; path = Source/Json.swift; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
80E3F7D51C602ACC007E1A94 /* JsonParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = JsonParser.swift; path = Source/JsonParser.swift; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
80E3F7D71C602ACC007E1A94 /* JsonSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = JsonSerializer.swift; path = Source/JsonSerializer.swift; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
80E3F7D81C602ACC007E1A94 /* ParseError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ParseError.swift; path = Source/ParseError.swift; sourceTree = SOURCE_ROOT; };
80E3F7D91C602ACC007E1A94 /* StringUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StringUtils.swift; path = Source/StringUtils.swift; sourceTree = SOURCE_ROOT; };
E434DF561C5A1D4C00A95287 /* PureJsonSerializer.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PureJsonSerializer.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -81,14 +81,14 @@
FA06004F19C10B7900B4A852 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA06005619C10B7900B4A852 /* JsonSerializerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JsonSerializerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
FA06005919C10B7900B4A852 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA06005A19C10B7900B4A852 /* JsonParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonParserTests.swift; sourceTree = "<group>"; };
FA06005A19C10B7900B4A852 /* JsonParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = JsonParserTests.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
FA06006719C10CBC00B4A852 /* tweets.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = tweets.json; sourceTree = "<group>"; };
FA06006919C1EC0900B4A852 /* ParseErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseErrorTests.swift; sourceTree = "<group>"; };
FA3AF20919C9CA5C00F9B28A /* SwiftFeed.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftFeed.app; sourceTree = BUILT_PRODUCTS_DIR; };
FA3AF20C19C9CA5C00F9B28A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA3AF20D19C9CA5C00F9B28A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
FA3AF20F19C9CA5C00F9B28A /* MasterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MasterViewController.swift; sourceTree = "<group>"; };
FA3AF21119C9CA5C00F9B28A /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = "<group>"; };
FA3AF20F19C9CA5C00F9B28A /* MasterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = MasterViewController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
FA3AF21119C9CA5C00F9B28A /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = DetailViewController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
FA3AF21419C9CA5C00F9B28A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
FA3AF21619C9CA5C00F9B28A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
FA3AF21919C9CA5C00F9B28A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
Expand All @@ -100,7 +100,7 @@
FA3AF29D19CBA4AB00F9B28A /* ApiClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiClient.swift; sourceTree = "<group>"; };
FA5A20E319CE58B8003CEF99 /* OverlayIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OverlayIndicator.swift; sourceTree = "<group>"; };
FAF14E8319C6834A0070F09F /* StringUtilsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringUtilsTests.swift; sourceTree = "<group>"; };
FAF14E8519C696BD0070F09F /* JsonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JsonTests.swift; sourceTree = "<group>"; };
FAF14E8519C696BD0070F09F /* JsonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = JsonTests.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down
22 changes: 11 additions & 11 deletions JsonSerializer/JsonParser+Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import Foundation
public extension Json {
var anyValue: AnyObject {
switch self {
case .ObjectValue(let ob):
case .object(let ob):
var mapped: [String : AnyObject] = [:]
ob.forEach { key, val in
mapped[key] = val.anyValue
}
return mapped
case .ArrayValue(let array):
case .array(let array):
return array.map { $0.anyValue }
case .BooleanValue(let bool):
case .bool(let bool):
return bool
case .NumberValue(let number):
case .number(let number):
return number
case .StringValue(let string):
case .string(let string):
return string
case .NullValue:
case .null:
return NSNull()
}
}
Expand All @@ -45,19 +45,19 @@ extension Json {
// If we're coming from foundation, it will be an `NSNumber`.
//This represents double, integer, and boolean.
case let number as Double:
return .NumberValue(number)
return .number(number)
case let string as String:
return .StringValue(string)
return .string(string)
case let object as [String : AnyObject]:
return from(object)
case let array as [AnyObject]:
return .ArrayValue(array.map(from))
return .array(array.map(from))
case _ as NSNull:
return .NullValue
return .null
default:
fatalError("Unsupported foundation type")
}
return .NullValue
return .null
}

public static func from(_ any: [String : AnyObject]) -> Json {
Expand Down
37 changes: 18 additions & 19 deletions JsonSerializerTests/JsonParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
import Foundation

class JsonDeserializerTests: XCTestCase {

Expand Down Expand Up @@ -58,13 +59,13 @@ class JsonDeserializerTests: XCTestCase {

func testStringWithMyltiBytes() {
let json = try! Json.deserialize("[\"こんにちは\"]")
XCTAssertEqual(json[0]!.stringValue, "こんにちは")
XCTAssertEqual(json[0]!.string, "こんにちは")
XCTAssertEqual(json.description, "[\"こんにちは\"]")
}

func testStringWithMyltiUnicodeScalars() {
let json = try! Json.deserialize("[\"江戸前🍣\"]")
XCTAssertEqual(json[0]!.stringValue!, "江戸前🍣")
XCTAssertEqual(json[0]!.string!, "江戸前🍣")
XCTAssertEqual(json[0]!.description, "\"江戸前🍣\"")
XCTAssertEqual(json.description, "[\"江戸前🍣\"]")
}
Expand All @@ -81,63 +82,61 @@ class JsonDeserializerTests: XCTestCase {

func testNumberOfExponent() {
let json = try! Json.deserialize("[1e2, 1e-2, 3.14e+01]")
XCTAssertEqual(json[0]!.intValue, 100)
XCTAssertEqual(json[1]!.doubleValue, 0.01)
XCTAssertEqual("\(json[2]!.doubleValue!)", "31.4")
XCTAssertEqual(json[0]!.int, 100)
XCTAssertEqual(json[1]!.double, 0.01)
XCTAssertEqual("\(json[2]!.double!)", "31.4")
}

func testUnicodeEscapeSequences() {
let json = try! Json.deserialize("[\"\\u003c \\u003e\"]")
XCTAssertEqual(json[0]!.stringValue!, "< >")
XCTAssertEqual(json[0]!.string!, "< >")
}

func testUnicodeEscapeSequencesWith32bitsUnicodeScalar() {
let json = try! Json.deserialize("[\"\\u0001\\uF363\"]")
XCTAssertEqual(json[0]!.stringValue, "\u{0001F363}")
XCTAssertEqual(json[0]!.string, "\u{0001F363}")
}

func testUnicodeEscapeSequencesWithTwo16bitsUnicodeScalar() {
let json = try! Json.deserialize("[\"\\u00015\\uF363\"]")
XCTAssertEqual(json[0]!.stringValue, "\u{0001}5\u{F363}")
XCTAssertEqual(json[0]!.string, "\u{0001}5\u{F363}")
}

func testTwitterJson() {
let json = try! Json.deserialize(complexJsonExample("tweets"))
XCTAssertEqual(json["statuses"]![0]!["id_str"]!.stringValue, "250075927172759552")
XCTAssertEqual(json["statuses"]![0]!["id_str"]!.string, "250075927172759552")
}

func testStackexchangeJson() {
let json = try! Json.deserialize(complexJsonExample("stackoverflow-items"))
XCTAssertEqual(json["items"]![0]!["view_count"]!.intValue, 18711)
XCTAssertEqual(json["items"]![0]!["view_count"]!.int, 18711)
}


func testPerformanceExampleWithNSData() {
let jsonSource = complexJsonExample("tweets")

self.measureBlock {
self.measure {
let _ = try! Json.deserialize(jsonSource)
}
}

func testPerformanceExampleWithString() {
let jsonSource = String(data: complexJsonExample("tweets"), encoding: NSUTF8StringEncoding)!

self.measureBlock {
self.measure {
let _ = try! Json.deserialize(jsonSource)
}
}

func testPerformanceExampleInJSONSerialization() {
let jsonSource = complexJsonExample("tweets")
self.measureBlock {
let _: AnyObject? = try! NSJSONSerialization
.JSONObjectWithData(jsonSource, options: .MutableContainers)
self.measure {
let _: AnyObject? = try! NSJSONSerialization.jsonObject(with: jsonSource,
options: .mutableContainers)
}
}

func complexJsonExample(name: String) -> NSData {
let bundle = NSBundle(forClass: self.dynamicType)
func complexJsonExample(_ name: String) -> NSData {
let bundle = NSBundle(for: self.dynamicType)
let path = bundle.pathForResource(name, ofType: "json")!
return NSData(contentsOfFile: path)!
}
Expand Down
24 changes: 12 additions & 12 deletions JsonSerializerTests/JsonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class JsonTests: XCTestCase {
let json = try Json.deserialize("[\"foo bar\", true, false]")
XCTAssertEqual(json.description, "[\"foo bar\",true,false]")

XCTAssertEqual(json[0]!.stringValue!, "foo bar")
XCTAssertEqual(json[1]!.boolValue!, true)
XCTAssertEqual(json[2]!.boolValue!, false)
XCTAssertEqual(json[0]!.string!, "foo bar")
XCTAssertEqual(json[1]!.bool!, true)
XCTAssertEqual(json[2]!.bool!, false)

XCTAssertEqual(json[3]?.stringValue, nil, "out of range")
XCTAssertEqual(json[3]?[0]?.stringValue, nil, "no such item")
XCTAssertEqual(json["no such property"]?.stringValue, nil, "no such property")
XCTAssertEqual(json["no"]?["such"]?["property"]?.stringValue, nil, "no such properties")
XCTAssertEqual(json[3]?.string, nil, "out of range")
XCTAssertEqual(json[3]?[0]?.string, nil, "no such item")
XCTAssertEqual(json["no such property"]?.string, nil, "no such property")
XCTAssertEqual(json["no"]?["such"]?["property"]?.string, nil, "no such properties")
} catch {
XCTFail("\(error)")
}
Expand All @@ -31,7 +31,7 @@ class JsonTests: XCTestCase {

func testConvertFromNilLiteral() {
let value: Json = nil
XCTAssertEqual(value, Json.NullValue)
XCTAssertEqual(value, Json.null)
}

func testConvertFromBooleanLiteral() {
Expand Down Expand Up @@ -117,7 +117,7 @@ class JsonTests: XCTestCase {
XCTAssertNotEqual(e, o0)
}

func testBooleanValueEquality() {
func testboolEquality() {
XCTAssertEqual(b0, b0)
XCTAssertEqual(b1, b1)
XCTAssertNotEqual(b0, e)
Expand All @@ -138,7 +138,7 @@ class JsonTests: XCTestCase {
XCTAssertNotEqual(n0, o0)
}

func testStringValueEquality() {
func teststringEquality() {
XCTAssertEqual(s0, s0)
XCTAssertEqual(s1, s1)
XCTAssertNotEqual(s0, e)
Expand All @@ -149,7 +149,7 @@ class JsonTests: XCTestCase {
XCTAssertNotEqual(s0, o0)
}

func testArrayValueEquality() {
func testarrayEquality() {
XCTAssertEqual(a0, a0)
XCTAssertEqual(a1, a1)
XCTAssertNotEqual(a0, e)
Expand All @@ -160,7 +160,7 @@ class JsonTests: XCTestCase {
XCTAssertNotEqual(a0, o0)
}

func testObjectValueEquality() {
func testobjectEquality() {
XCTAssertEqual(o0, o0)
XCTAssertEqual(o1, o1)
XCTAssertNotEqual(o0, e)
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PackageDescription

let package = Package(
name: "PureJsonSerializer"
name: "PureJson"
)
Loading