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

Fix conversion of String->Int dictionary #53

Merged
merged 2 commits into from
Dec 5, 2016
Merged
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
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