Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #177 from glennrfisher/serialize-to-string
Browse files Browse the repository at this point in the history
Add serializeString function
  • Loading branch information
mdmathias authored Sep 14, 2016
2 parents df68c72 + 834a91c commit dba5e3e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ extension JSON {

/// Unexpected JSON `value` was found that is not convertible `to` type
case ValueNotConvertible(value: JSON, to: Any.Type)

/// The JSON is not serializable to a `String`.
case StringSerializationError
}

}
Expand Down
12 changes: 12 additions & 0 deletions Sources/JSONSerializing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ extension JSON {
let obj: AnyObject = toNSJSONSerializationObject()
return try NSJSONSerialization.dataWithJSONObject(obj, options: [])
}

/// Attempt to serialize `JSON` into a `String`.
/// - returns: A `String` containing the `JSON`.
/// - throws: A `JSON.Error.StringSerializationError` or errors that arise from `NSJSONSerialization`.
/// - see: Foundation.NSJSONSerialization
public func serializeString() throws -> Swift.String {
let data = try self.serialize()
guard let json = Swift.String(data: data, encoding: NSUTF8StringEncoding) else {
throw Error.StringSerializationError
}
return json
}

/// A function to help with the serialization of `JSON`.
/// - returns: An `AnyObject` suitable for `NSJSONSerialization`'s use.
Expand Down
36 changes: 33 additions & 3 deletions Tests/JSONSerializingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,43 @@ class JSONSerializingTests: XCTestCase {
let json = JSONFromFixture("sample.JSON")
let noWhiteSpaceData = dataFromFixture("sampleNoWhiteSpace.JSON")

func testThatJSONCanBeSerialized() {
func testThatJSONCanBeSerializedToNSData() {
let data = try! json.serialize()
XCTAssertGreaterThan(data.length, 0, "There should be data.")
}

func testThatJSONCanBeSerializedToString() {
let string = try! json.serializeString()
XCTAssertGreaterThan(string.characters.count, 0, "There should be characters.")
}

func testThatJSONDataIsEqual() {
let serializedJSONData = try! json.serialize()
let noWhiteSpaceJSON = try! JSON(data: noWhiteSpaceData)
let noWhiteSpaceSerializedJSONData = try! noWhiteSpaceJSON.serialize()
XCTAssertEqual(serializedJSONData, noWhiteSpaceSerializedJSONData, "Serialized data should be equal.")
}

func testThatJSONStringIsEqual() {
let serializedJSONString = try! json.serializeString()
let noWhiteSpaceJSON = try! JSON(data: noWhiteSpaceData)
let noWhiteSpaceSerializedJSONString = try! noWhiteSpaceJSON.serializeString()
XCTAssertEqual(serializedJSONString, noWhiteSpaceSerializedJSONString, "Serialized string should be equal.")
}

func testThatJSONSerializationMakesEqualJSON() {
func testThatJSONDataSerializationMakesEqualJSON() {
let serializedJSONData = try! json.serialize()
let serialJSON = try! JSON(data: serializedJSONData)
XCTAssert(json == serialJSON, "The JSON values should be equal.")
}

func testThatJSONStringSerializationMakesEqualJSON() {
let serializedJSONString = try! json.serializeString()
let serialJSON = try! JSON(jsonString: serializedJSONString)
XCTAssert(json == serialJSON, "The JSON values should be equal.")
}

func testThatJSONSerializationHandlesBoolsCorrectly() {
func testThatJSONDataSerializationHandlesBoolsCorrectly() {
let json = JSON.Dictionary([
"foo": .Bool(true),
"bar": .Bool(false),
Expand All @@ -36,6 +54,18 @@ class JSONSerializingTests: XCTestCase {
let deserialized = JSON.Dictionary(deserializedResult)
XCTAssertEqual(json, deserialized, "Serialize/Deserialize succeed with Bools")
}

func testThatJSONStringSerializationHandlesBoolsCorrectly() {
let json = JSON.Dictionary([
"foo": .Bool(true),
"bar": .Bool(false),
"baz": .Int(123),
])
let string = try! json.serializeString()
let deserializedResult = try! JSON(jsonString: string).dictionary()
let deserialized = JSON.Dictionary(deserializedResult)
XCTAssertEqual(json, deserialized, "Serialize/Deserialize succeed with Bools")
}
}


Expand Down

0 comments on commit dba5e3e

Please sign in to comment.