Skip to content

Commit

Permalink
- added toJSONString function that handles arrays of objects
Browse files Browse the repository at this point in the history
- added convenience toJSONFunctions that don't require the prettyPrint parameter
  • Loading branch information
tristanhimmelman committed Nov 3, 2015
1 parent 765c9f9 commit 33a2e63
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
52 changes: 36 additions & 16 deletions ObjectMapper/Core/Mapper.swift
Expand Up @@ -290,26 +290,46 @@ extension Mapper {
}

/// Maps an Object to a JSON string
public func toJSONString(object: N) -> String? {
return toJSONString(object, prettyPrint: false)
}

/// Maps an Object to a JSON string with option of pretty formatting
public func toJSONString(object: N, prettyPrint: Bool) -> String? {
let JSONDict = toJSON(object)

if NSJSONSerialization.isValidJSONObject(JSONDict) {
let options: NSJSONWritingOptions = prettyPrint ? .PrettyPrinted : []
let JSONData: NSData?
do {
JSONData = try NSJSONSerialization.dataWithJSONObject(JSONDict, options: options)
} catch let error {
print(error)
JSONData = nil
}

if let JSON = JSONData {
return String(data: JSON, encoding: NSUTF8StringEncoding)
}
}

return nil
return toJSONString(JSONDict, prettyPrint: prettyPrint)
}

/// Maps an array of Objects to a JSON string
public func toJSONString(array: [N]) -> String? {
return toJSONString(array, prettyPrint: false)
}

/// Maps an array of Objects to a JSON string with option of pretty formatting
public func toJSONString(array: [N], prettyPrint: Bool) -> String? {
let JSONDict = toJSONArray(array)

return toJSONString(JSONDict, prettyPrint: prettyPrint)
}

private func toJSONString(object: AnyObject, prettyPrint: Bool) -> String? {
if NSJSONSerialization.isValidJSONObject(object) {
let options: NSJSONWritingOptions = prettyPrint ? .PrettyPrinted : []
let JSONData: NSData?
do {
JSONData = try NSJSONSerialization.dataWithJSONObject(object, options: options)
} catch let error {
print(error)
JSONData = nil
}

if let JSON = JSONData {
return String(data: JSON, encoding: NSUTF8StringEncoding)
}
}
return nil
}
}

extension Mapper where N: Hashable {
Expand Down
28 changes: 28 additions & 0 deletions ObjectMapperTests/ObjectMapperTests.swift
Expand Up @@ -233,6 +233,34 @@ class ObjectMapperTests: XCTestCase {
XCTAssertEqual(user.smoker, parsedUser.smoker)
XCTAssertEqual(user.sex, parsedUser.sex)
}

func testToJSONArrayAndBack(){
let user = User()
user.username = "tristan_him"
user.identifier = "tristan_him_identifier"
user.photoCount = 0
user.age = 28
user.weight = 150
user.drinker = true
user.smoker = false
user.sex = .Female
user.arr = ["cheese", 11234]
let users = [user, user, user]

//print(JSONString)
let JSONString = Mapper().toJSONString(users)
let parsedUsers = userMapper.mapArray(JSONString)

XCTAssertNotNil(parsedUsers)
XCTAssertTrue(parsedUsers?.count == 3)
XCTAssertEqual(user.identifier, parsedUsers?[0].identifier)
XCTAssertEqual(user.photoCount, parsedUsers?[0].photoCount)
XCTAssertEqual(user.age, parsedUsers?[0].age)
XCTAssertEqual(user.weight, parsedUsers?[0].weight)
XCTAssertEqual(user.drinker, parsedUsers?[0].drinker)
XCTAssertEqual(user.smoker, parsedUsers?[0].smoker)
XCTAssertEqual(user.sex, parsedUsers?[0].sex)
}

func testUnknownPropertiesIgnored() {
let JSONString = "{\"username\":\"bob\",\"identifier\":\"bob1987\", \"foo\" : \"bar\", \"fooArr\" : [ 1, 2, 3], \"fooObj\" : { \"baz\" : \"qux\" } }"
Expand Down

0 comments on commit 33a2e63

Please sign in to comment.