Skip to content

Commit e1068af

Browse files
authored
Merge pull request #72 from devlucky/feature/JSONAPIMeta
Added top level meta support for JSON API
2 parents 681146f + 2cc1193 commit e1068af

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

Source/JSONAPISerializer.swift

+22-15
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,27 @@ public protocol JSONAPIEntity: CustomSerializable, JSONAPISerializable {
7272
*/
7373
public struct JSONAPISerializer<T: JSONAPIEntity>: Serializable {
7474

75-
// Top level `data` member: the document’s “primary data”
76-
private let data: AnyObject
75+
private typealias JSONAPIConvertible = protocol<JSONAPISerializable, Serializable>
76+
77+
/// Top level [`data`](http://jsonapi.org/format/#document-top-level) member: the document’s “primary data”
78+
private let data: JSONAPIConvertible
7779

78-
// Top level `included` member: an array of resource objects that are related to the primary data and/or each other (“included resources”).
80+
/// Top level `included` member: an array of resource objects that are related to the primary data and/or each other (“included resources”).
7981
private let included: [AnyObject]?
8082

81-
// Top level `links` member: a links object related to the primary data.
82-
private let links: AnyObject?
83-
84-
private typealias JSONAPIConvertible = protocol<JSONAPISerializable, Serializable>
85-
private typealias JSONAPISerializerInit = (data: AnyObject, links: AnyObject?, included: [AnyObject]?)
83+
/// Top level [`links`](http://jsonapi.org/format/#document-links) member: a links object related to the primary data.
84+
private let links: [String: JSONAPILink]?
85+
86+
/// Top level [`meta`](http://jsonapi.org/format/#document-meta) member: used to include non-standard meta-information.
87+
private let meta: Serializable?
88+
89+
private typealias JSONAPISerializerInit = (data: JSONAPIConvertible, links: [String: JSONAPILink]?, meta: Serializable?, included: [AnyObject]?)
8690

87-
private static func commonInit(object: JSONAPIConvertible, topLevelLinks: [String: JSONAPILink]?, includeChildren: Bool) -> JSONAPISerializerInit {
91+
private static func commonInit(object: JSONAPIConvertible, topLevelLinks: [String: JSONAPILink]?, meta: Serializable?, includeChildren: Bool) -> JSONAPISerializerInit {
8892
return (
89-
data: object.serialize()!, // can't fail, JSONAPIEntity must always be serializable
90-
links: topLevelLinks.serialize(),
93+
data: object,
94+
links: topLevelLinks,
95+
meta: meta,
9196
included: object.includedRelationships(includeChildren)?.unifiedIncludedRelationships()
9297
)
9398
}
@@ -97,25 +102,27 @@ public struct JSONAPISerializer<T: JSONAPIEntity>: Serializable {
97102

98103
- parameter object: A `JSONAPIEntities`
99104
- parameter topLevelLinks: A top `JSONAPILink` optional object
105+
- parameter topLevelMeta: A meta object that will be serialized and placed in the top level of the json.
100106
- parameter includeChildren: when true it will include relationships of relationships, false by default.
101107

102108
- returns: A serializable object that serializes a `JSONAPIEntity` conforming to JSON API
103109
*/
104-
public init(_ object: T, topLevelLinks: [String: JSONAPILink]? = nil, includeChildren: Bool = false) {
105-
(data, links, included) = JSONAPISerializer.commonInit(object, topLevelLinks: topLevelLinks, includeChildren: includeChildren)
110+
public init(_ object: T, topLevelLinks: [String: JSONAPILink]? = nil, topLevelMeta: Serializable? = nil, includeChildren: Bool = false) {
111+
(data, links, meta, included) = JSONAPISerializer.commonInit(object, topLevelLinks: topLevelLinks, meta: topLevelMeta, includeChildren: includeChildren)
106112
}
107113

108114
/**
109115
Initialize a serializer with an array of `JSONAPIEntity`
110116

111117
- parameter objects: An array of `JSONAPIEntity`
112118
- parameter topLevelLinks: A top `JSONAPILink` optional object
119+
- parameter topLevelMeta: A meta object that will be serialized and placed in the top level of the json.
113120
- parameter includeChildren: when true it wll include relationships of relationships, false by default.
114121

115122
- returns: A serializable object that serializes an array of `JSONAPIEntity` conforming to JSON API
116123
*/
117-
public init(_ objects: [T], topLevelLinks: [String: JSONAPILink]? = nil, includeChildren: Bool = false) {
118-
(data, links, included) = JSONAPISerializer.commonInit(objects, topLevelLinks: topLevelLinks, includeChildren: includeChildren)
124+
public init(_ objects: [T], topLevelLinks: [String: JSONAPILink]? = nil, topLevelMeta: Serializable? = nil, includeChildren: Bool = false) {
125+
(data, links, meta, included) = JSONAPISerializer.commonInit(objects, topLevelLinks: topLevelLinks, meta: topLevelMeta, includeChildren: includeChildren)
119126
}
120127
}
121128

Tests/JSONAPITests.swift

+20
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ class JSONAPISpec: QuickSpec {
8585
let data = object["data"].arrayValue
8686
expect(data.count).toNot(equal(0))
8787
}
88+
89+
context("top level meta") {
90+
it("should be included in the top level") {
91+
let object = json(JSONAPISerializer(user, topLevelMeta: ["test": "meta"]))
92+
let meta = object["meta"].dictionary
93+
expect(meta).toNot(beNil())
94+
}
95+
96+
it("should serialize a simple dictionary") {
97+
let object = json(JSONAPISerializer(user, topLevelMeta: ["test": "meta"]))
98+
let meta = object["meta"].dictionary
99+
expect(meta?["test"]).to(equal("meta"))
100+
}
101+
102+
it("should serialize a serializable object") {
103+
let object = json(JSONAPISerializer(user, topLevelMeta: ["test": user]))
104+
let meta = object["meta"].dictionary
105+
expect(meta?["test"]?.dictionaryValue["id"]).to(equal("11"))
106+
}
107+
}
88108
}
89109

90110
describe("JSON API Entity Serialization") {

0 commit comments

Comments
 (0)