@@ -72,22 +72,27 @@ public protocol JSONAPIEntity: CustomSerializable, JSONAPISerializable {
72
72
*/
73
73
public struct JSONAPISerializer < T: JSONAPIEntity > : Serializable {
74
74
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
77
79
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”).
79
81
private let included : [ AnyObject ] ?
80
82
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 ] ? )
86
90
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 {
88
92
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,
91
96
included: object. includedRelationships ( includeChildren) ? . unifiedIncludedRelationships ( )
92
97
)
93
98
}
@@ -97,25 +102,27 @@ public struct JSONAPISerializer<T: JSONAPIEntity>: Serializable {
97
102
98
103
- parameter object: A `JSONAPIEntities`
99
104
- 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.
100
106
- parameter includeChildren: when true it will include relationships of relationships, false by default.
101
107
102
108
- returns: A serializable object that serializes a `JSONAPIEntity` conforming to JSON API
103
109
*/
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)
106
112
}
107
113
108
114
/**
109
115
Initialize a serializer with an array of `JSONAPIEntity`
110
116
111
117
- parameter objects: An array of `JSONAPIEntity`
112
118
- 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.
113
120
- parameter includeChildren: when true it wll include relationships of relationships, false by default.
114
121
115
122
- returns: A serializable object that serializes an array of `JSONAPIEntity` conforming to JSON API
116
123
*/
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)
119
126
}
120
127
}
121
128
0 commit comments