Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #53 from remko/master
Browse files Browse the repository at this point in the history
Fix conversion of String->Int dictionary
  • Loading branch information
endocrimes authored Dec 5, 2016
2 parents 828d8ac + 267c7f0 commit 8331252
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/Jay/NativeTypeConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ struct NativeTypeConverter {
func parseNSArray(_ array: NSArray) throws -> JSON? {
return try self.convertArray(array.map { $0 as Any })
}

func parseArray(_ array: [Any]) throws -> JSON? {
return try self.convertArray(array)
}

func parseNSDictionary(_ dict: NSDictionary) throws -> JSON? {
var dOut = [String: Any]()
Expand All @@ -88,6 +92,10 @@ struct NativeTypeConverter {
}
return try self.dictionaryToJayType(dOut)
}

func parseDictionary(_ dict: [String: Any]) throws -> JSON? {
return try self.dictionaryToJayType(dict)
}

func arrayToJayType(_ maybeArray: Any) throws -> JSON? {

Expand Down Expand Up @@ -126,13 +134,31 @@ struct NativeTypeConverter {
guard let json = js else { return .null }
if json is NSNull { return .null }

if let nativeDict = json as? [String: Any] {
guard let dict = try self.parseDictionary(nativeDict) else {
throw JayError.unsupportedType(nativeDict)
}
return dict
}

// On OS X, this check also succeeds for [:] dicts, so this check needs
// to come after the previous one.
if let nsdict = json as? NSDictionary {
guard let dict = try self.parseNSDictionary(nsdict) else {
throw JayError.unsupportedType(nsdict)
}
return dict
}

if let nativeArray = json as? [Any] {
guard let array = try self.parseArray(nativeArray) else {
throw JayError.unsupportedType(nativeArray)
}
return array
}

// On OS X, this check also succeeds for [] arrays, so this check needs
// to come after the previous one.
if let nsarray = json as? NSArray {
guard let array = try self.parseNSArray(nsarray) else {
throw JayError.unsupportedType(nsarray)
Expand Down
7 changes: 7 additions & 0 deletions Tests/JayTests/FormattingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extension FormattingTests {
("testNSDictionary_Empty", testNSDictionary_Empty),
("testNSDictionary_Simple", testNSDictionary_Simple),
("testObject_Simple", testObject_Simple),
("testObject_StringInt", testObject_StringInt),
("testObject_Normal", testObject_Normal),
("testObject_Nested", testObject_Nested),
("testObject_AllTypes", testObject_AllTypes),
Expand Down Expand Up @@ -70,6 +71,12 @@ class FormattingTests: XCTestCase {
let data = try! Jay().dataFromJson(any: json)
XCTAssertEqual(data, "{\"hello\":\"world\"}".chars())
}

func testObject_StringInt() {
let json = ["hello": 42]
let data = try! Jay().dataFromJson(any: json)
XCTAssertEqual(String(bytes: data, encoding: String.Encoding.utf8)!, "{\"hello\":42}")
}

func testObject_Normal() {
let json: JSON = [
Expand Down

0 comments on commit 8331252

Please sign in to comment.