@@ -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
320354class Response < T: Mappable > : Mappable {
@@ -444,3 +478,31 @@ class User: Mappable {
444478 return " username: \( username) \n id: \( identifier) \n age: \( age) \n photoCount: \( photoCount) \n drinker: \( drinker) \n smoker: \( smoker) \n arr: \( arr) \n arrOptional: \( arrOptional) \n dict: \( dict) \n dictOptional: \( dictOptional) \n friend: \( friend) \n friends: \( friends) \n birthday: \( birthday) \n birthdayOpt: \( birthdayOpt) \n y2k: \( y2k) \n y2kOpt: \( y2k) \n weight: \( 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