Skip to content

Commit 1eff8fb

Browse files
Merge pull request tristanhimmelman#43 from corinnekrych/json.to.object.array.of.primitives
fix typo and ad unit tests
2 parents c176ed7 + 13e75cd commit 1eff8fb

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

ObjectMapper/Core/ToJSON.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ class ToJSON {
4646

4747
// Dictionaries with basic types
4848
case is Dictionary<String, Bool>.Type:
49-
dictionary[currentKey] = field as Dictionary<String,Bool>
50-
case is Dictionary<String, Bool>.Type:
51-
dictionary[currentKey] = field as Dictionary<String,Int>
52-
case is Dictionary<String, Bool>.Type:
53-
dictionary[currentKey] = field as Dictionary<String,Double>
54-
case is Dictionary<String, Bool>.Type:
55-
dictionary[currentKey] = field as Dictionary<String,Float>
49+
dictionary[currentKey] = field as Dictionary<String, Bool>
50+
case is Dictionary<String, Int>.Type:
51+
dictionary[currentKey] = field as Dictionary<String, Int>
52+
case is Dictionary<String, Double>.Type:
53+
dictionary[currentKey] = field as Dictionary<String, Double>
54+
case is Dictionary<String, Float>.Type:
55+
dictionary[currentKey] = field as Dictionary<String, Float>
5656
case is Dictionary<String, String>.Type:
57-
dictionary[currentKey] = field as Dictionary<String,String>
57+
dictionary[currentKey] = field as Dictionary<String, String>
5858
default:
5959
//println("Default")
6060
return

ObjectMapperTests/ObjectMapperTests.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,40 @@ class ObjectMapperTests: XCTestCase {
315315
XCTAssert(user2 != nil, "ISO8601DateTransform must not crash for incorrect format")
316316
XCTAssert(user2.y2kOpt == nil, "ISO8601DateTransform should return nil for incorrect format")
317317
}
318+
319+
func testJsonToObjectModelOptionalDictionnaryOfPrimitives() {
320+
var json = ["dictStringString":["string": "string"], "dictStringBool":["string": false], "dictStringInt":["string": 1], "dictStringDouble":["string": 1.1], "dictStringFloat":["string": 1.2]]
321+
322+
let mapper = Mapper<TestCollectionOfPrimitives>()
323+
let testSet = mapper.map(json)
324+
325+
XCTAssertTrue(testSet.dictStringString.count == 1)
326+
XCTAssertTrue(testSet.dictStringInt.count == 1)
327+
XCTAssertTrue(testSet.dictStringBool.count == 1)
328+
XCTAssertTrue(testSet.dictStringDouble.count == 1)
329+
XCTAssertTrue(testSet.dictStringFloat.count == 1)
330+
}
331+
332+
func testObjectToModelDictionnaryOfPrimitives() {
333+
var object = TestCollectionOfPrimitives()
334+
object.dictStringString = ["string": "string"]
335+
object.dictStringBool = ["string": false]
336+
object.dictStringInt = ["string": 1]
337+
object.dictStringDouble = ["string": 1.2]
338+
object.dictStringFloat = ["string": 1.3]
339+
340+
let json = Mapper<TestCollectionOfPrimitives>().toJSON(object)
341+
342+
XCTAssertTrue((json["dictStringString"] as [String:String]).count == 1)
343+
XCTAssertTrue((json["dictStringBool"] as [String:Bool]).count == 1)
344+
XCTAssertTrue((json["dictStringInt"] as [String:Int]).count == 1)
345+
XCTAssertTrue((json["dictStringDouble"] as [String:Double]).count == 1)
346+
XCTAssertTrue((json["dictStringFloat"] as [String:Float]).count == 1)
347+
let dict:[String: String] = json["dictStringString"] as [String:String]
348+
let value = dict["string"]! as String
349+
XCTAssertTrue(value == "string")
350+
}
351+
318352
}
319353

320354
class Response<T:Mappable>: Mappable {
@@ -444,3 +478,31 @@ class User: Mappable {
444478
return "username: \(username) \nid:\(identifier) \nage: \(age) \nphotoCount: \(photoCount) \ndrinker: \(drinker) \nsmoker: \(smoker) \narr: \(arr) \narrOptional: \(arrOptional) \ndict: \(dict) \ndictOptional: \(dictOptional) \nfriend: \(friend)\nfriends: \(friends) \nbirthday: \(birthday)\nbirthdayOpt: \(birthdayOpt) \ny2k: \(y2k) \ny2kOpt: \(y2k) \nweight: \(weight)"
445479
}
446480
}
481+
482+
class TestCollectionOfPrimitives : Mappable {
483+
var dictStringString: [String: String] = [:]
484+
var dictStringInt: [String: Int] = [:]
485+
var dictStringBool: [String: Bool] = [:]
486+
var dictStringDouble: [String: Double] = [:]
487+
var dictStringFloat: [String: Float] = [:]
488+
var arrayString: [String] = []
489+
var arrayInt: [Int] = []
490+
var arrayBool: [Bool] = []
491+
var arrayDouble: [Double] = []
492+
var arrayFloat: [Float] = []
493+
494+
required init() {}
495+
496+
func map<N>(mapper: Mapper<N>) {
497+
dictStringString <= mapper["dictStringString"]
498+
dictStringBool <= mapper["dictStringBool"]
499+
dictStringInt <= mapper["dictStringInt"]
500+
dictStringDouble <= mapper["dictStringDouble"]
501+
dictStringFloat <= mapper["dictStringFloat"]
502+
arrayString <= mapper["arrayString"]
503+
arrayInt <= mapper["arrayInt"]
504+
arrayBool <= mapper["arrayBool"]
505+
arrayDouble <= mapper["arrayDouble"]
506+
arrayFloat <= mapper["arrayFloat"]
507+
}
508+
}

0 commit comments

Comments
 (0)