Skip to content
This repository was archived by the owner on May 20, 2021. It is now read-only.

Commit 4352752

Browse files
committed
Add Wrap() functions for encoding JSON arrays
This commit introduces two new `Wrap()` overloads that return either an array of JSON dictionaries, or JSON-based NSData that has a root array object - by passing in an array of objects.
1 parent e12d84f commit 4352752

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Wrap.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,33 @@ public func Wrap<T>(object: T, dateFormatter: NSDateFormatter? = nil) throws ->
7474
}
7575

7676
/**
77-
* Alternative `Wrap()` implementation that returns JSON-encoded NSData
77+
* Alternative `Wrap()` overload that returns JSON-based `NSData`
7878
*
7979
* See the documentation for the dictionary-based `Wrap()` function for more information
80-
* and customization options.
8180
*/
8281
public func Wrap<T>(object: T, writingOptions: NSJSONWritingOptions? = nil, dateFormatter: NSDateFormatter? = nil) throws -> NSData {
8382
return try Wrapper(dateFormatter: dateFormatter).wrap(object, writingOptions: writingOptions ?? [])
8483
}
8584

85+
/**
86+
* Alternative `Wrap()` overload that encodes an array of objects into an array of dictionaries
87+
*
88+
* See the documentation for the dictionary-based `Wrap()` function for more information
89+
*/
90+
public func Wrap<T>(objects: [T], dateFormatter: NSDateFormatter? = nil) throws -> [WrappedDictionary] {
91+
return try objects.map({ try Wrap($0) })
92+
}
93+
94+
/**
95+
* Alternative `Wrap()` overload that encodes an array of objects into JSON-based `NSData`
96+
*
97+
* See the documentation for the dictionary-based `Wrap()` function for more information
98+
*/
99+
public func Wrap<T>(objects: [T], writingOptions: NSJSONWritingOptions? = nil, dateFormatter: NSDateFormatter? = nil) throws -> NSData {
100+
let dictionaries: [WrappedDictionary] = try Wrap(objects)
101+
return try NSJSONSerialization.dataWithJSONObject(dictionaries, options: writingOptions ?? [])
102+
}
103+
86104
/**
87105
* Protocol providing the main customization point for Wrap
88106
*

WrapTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,24 @@ class WrapTests: XCTestCase {
659659
XCTFail(error.toString())
660660
}
661661
}
662+
663+
func testWrappingArray() {
664+
struct Model {
665+
let string: String
666+
}
667+
668+
do {
669+
let models = [Model(string: "A"), Model(string: "B"), Model(string: "C")]
670+
let wrapped: [WrappedDictionary] = try Wrap(models)
671+
XCTAssertEqual(wrapped.count, 3)
672+
673+
try VerifyDictionary(wrapped[0], againstDictionary: ["string" : "A"])
674+
try VerifyDictionary(wrapped[1], againstDictionary: ["string" : "B"])
675+
try VerifyDictionary(wrapped[2], againstDictionary: ["string" : "C"])
676+
} catch {
677+
XCTFail(error.toString())
678+
}
679+
}
662680
}
663681

664682
// MARK: - Mocks

0 commit comments

Comments
 (0)