1
1
package org .zalando .jsonapi .json .circe
2
2
3
- import cats .data .Xor
4
3
import io .circe ._
5
4
import org .zalando .jsonapi .json .FieldNames
6
5
import org .zalando .jsonapi .model .JsonApiObject ._
@@ -20,23 +19,23 @@ trait CirceJsonapiDecoders {
20
19
}.toList)
21
20
)
22
21
23
- implicit val valueDecoder = Decoder .instance[Value ](_.as[Json ].map(jsonToValue))
22
+ implicit val valueDecoder = Decoder .instance[Value ](_.as[Json ].right. map(jsonToValue))
24
23
25
24
implicit val attributesDecoder = Decoder .instance[Attributes ](hcursor ⇒ {
26
- hcursor.as[Value ].flatMap {
25
+ hcursor.as[Value ].right. flatMap {
27
26
case JsObjectValue (value) ⇒
28
- Xor . Right (value)
27
+ Right (value)
29
28
case _ ⇒
30
- Xor . Left (DecodingFailure (" only an object can be decoded to Attributes" , hcursor.history))
29
+ Left (DecodingFailure (" only an object can be decoded to Attributes" , hcursor.history))
31
30
}
32
31
})
33
32
34
- implicit val attributeDecoder = Decoder .instance[Attribute ](_.as[Attributes ].map(_.head))
33
+ implicit val attributeDecoder = Decoder .instance[Attribute ](_.as[Attributes ].right. map(_.head))
35
34
36
35
implicit val linksDecoder = Decoder .instance[Links ](hcursor ⇒ {
37
- hcursor.as[Value ].flatMap {
36
+ hcursor.as[Value ].right. flatMap {
38
37
case JsObjectValue (attributes) ⇒
39
- Xor . Right (attributes.map {
38
+ Right (attributes.map {
40
39
case Attribute (FieldNames .`self`, StringValue (url)) ⇒ Links .Self (url)
41
40
case Attribute (FieldNames .`about`, StringValue (url)) ⇒
42
41
Links .About (url)
@@ -49,11 +48,11 @@ trait CirceJsonapiDecoders {
49
48
Links .Related (url)
50
49
})
51
50
case _ ⇒
52
- Xor . Left (DecodingFailure (" only an object can be decoded to Links" , hcursor.history))
51
+ Left (DecodingFailure (" only an object can be decoded to Links" , hcursor.history))
53
52
}
54
53
})
55
54
56
- def jsonToData (json : Json ): Xor [DecodingFailure , Data ] = json match {
55
+ def jsonToData (json : Json ): Either [DecodingFailure , Data ] = json match {
57
56
case json : Json if json.isArray ⇒
58
57
json.as[ResourceObjects ]
59
58
case json : Json if json.isObject ⇒
@@ -62,9 +61,9 @@ trait CirceJsonapiDecoders {
62
61
63
62
implicit val relationshipDecoder = Decoder .instance[Relationship ](hcursor ⇒ {
64
63
for {
65
- links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]]
64
+ links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]].right
66
65
// TODO: there's prolly a cleaner way here. there's a circular dependency Data -> ResourceObject(s) -> Relationship(s) -> Data that's giving circe problems
67
- data ← hcursor.downField(FieldNames .`data`).as[Option [Json ]].map(_.flatMap(jsonToData(_).toOption))
66
+ data ← hcursor.downField(FieldNames .`data`).as[Option [Json ]].right. map(_.flatMap(jsonToData(_).right. toOption)).right
68
67
} yield
69
68
Relationship (
70
69
links = links,
@@ -75,31 +74,31 @@ trait CirceJsonapiDecoders {
75
74
implicit val relationshipsDecoder = Decoder .instance[Relationships ](_.as[Map [String , Relationship ]])
76
75
77
76
implicit val jsonApiDecoder = Decoder .instance[JsonApi ](hcursor ⇒ {
78
- hcursor.as[Value ].flatMap {
77
+ hcursor.as[Value ].right. flatMap {
79
78
case JsObjectValue (attributes) ⇒
80
- Xor . Right (attributes.map {
79
+ Right (attributes.map {
81
80
case Attribute (name, value) ⇒ JsonApiProperty (name, value)
82
81
})
83
82
case _ ⇒
84
- Xor . Left (DecodingFailure (" only an object can be decoded to JsonApi" , hcursor.history))
83
+ Left (DecodingFailure (" only an object can be decoded to JsonApi" , hcursor.history))
85
84
}
86
85
})
87
86
88
87
implicit val metaDecoder = Decoder .instance[Meta ](hcursor ⇒ {
89
- hcursor.as[Value ].flatMap {
88
+ hcursor.as[Value ].right. flatMap {
90
89
case JsObjectValue (attributes) ⇒
91
- Xor . Right (attributes.map {
90
+ Right (attributes.map {
92
91
case Attribute (name, value) ⇒ name -> value
93
92
}.toMap)
94
93
case _ ⇒
95
- Xor . Left (DecodingFailure (" only an object can be decoded to Meta" , hcursor.history))
94
+ Left (DecodingFailure (" only an object can be decoded to Meta" , hcursor.history))
96
95
}
97
96
})
98
97
99
98
implicit val errorSourceDecoder = Decoder .instance[ErrorSource ](hcursor ⇒ {
100
99
for {
101
- pointer ← hcursor.downField(FieldNames .`pointer`).as[Option [String ]]
102
- parameter ← hcursor.downField(FieldNames .`parameter`).as[Option [String ]]
100
+ pointer ← hcursor.downField(FieldNames .`pointer`).as[Option [String ]].right
101
+ parameter ← hcursor.downField(FieldNames .`parameter`).as[Option [String ]].right
103
102
} yield
104
103
ErrorSource (
105
104
pointer = pointer,
@@ -109,14 +108,14 @@ trait CirceJsonapiDecoders {
109
108
110
109
implicit val errorDecoder = Decoder .instance[Error ](hcursor ⇒ {
111
110
for {
112
- id ← hcursor.downField(FieldNames .`id`).as[Option [String ]]
113
- status ← hcursor.downField(FieldNames .`status`).as[Option [String ]]
114
- code ← hcursor.downField(FieldNames .`code`).as[Option [String ]]
115
- title ← hcursor.downField(FieldNames .`title`).as[Option [String ]]
116
- detail ← hcursor.downField(FieldNames .`detail`).as[Option [String ]]
117
- links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]]
118
- meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]]
119
- source ← hcursor.downField(FieldNames .`source`).as[Option [ErrorSource ]]
111
+ id ← hcursor.downField(FieldNames .`id`).as[Option [String ]].right
112
+ status ← hcursor.downField(FieldNames .`status`).as[Option [String ]].right
113
+ code ← hcursor.downField(FieldNames .`code`).as[Option [String ]].right
114
+ title ← hcursor.downField(FieldNames .`title`).as[Option [String ]].right
115
+ detail ← hcursor.downField(FieldNames .`detail`).as[Option [String ]].right
116
+ links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]].right
117
+ meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]].right
118
+ source ← hcursor.downField(FieldNames .`source`).as[Option [ErrorSource ]].right
120
119
} yield
121
120
Error (
122
121
id = id,
@@ -132,12 +131,12 @@ trait CirceJsonapiDecoders {
132
131
133
132
implicit val resourceObjectDecoder = Decoder .instance[ResourceObject ](hcursor ⇒ {
134
133
for {
135
- id ← hcursor.downField(FieldNames .`id`).as[Option [String ]]
136
- `type` ← hcursor.downField(FieldNames .`type`).as[String ]
137
- attributes ← hcursor.downField(FieldNames .`attributes`).as[Option [Attributes ]]
138
- relationships ← hcursor.downField(FieldNames .`relationships`).as[Option [Relationships ]]
139
- links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]]
140
- meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]]
134
+ id ← hcursor.downField(FieldNames .`id`).as[Option [String ]].right
135
+ `type` ← hcursor.downField(FieldNames .`type`).as[String ].right
136
+ attributes ← hcursor.downField(FieldNames .`attributes`).as[Option [Attributes ]].right
137
+ relationships ← hcursor.downField(FieldNames .`relationships`).as[Option [Relationships ]].right
138
+ links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]].right
139
+ meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]].right
141
140
} yield
142
141
ResourceObject (
143
142
id = id,
@@ -150,20 +149,20 @@ trait CirceJsonapiDecoders {
150
149
})
151
150
152
151
implicit val resourceObjectsDecoder =
153
- Decoder .instance[ResourceObjects ](_.as[List [ResourceObject ]].map(ResourceObjects ))
152
+ Decoder .instance[ResourceObjects ](_.as[List [ResourceObject ]].right. map(ResourceObjects ))
154
153
155
- implicit val dataDecoder = Decoder .instance[Data ](_.as[Json ].flatMap(jsonToData))
154
+ implicit val dataDecoder = Decoder .instance[Data ](_.as[Json ].right. flatMap(jsonToData))
156
155
157
- implicit val includedDecoder = Decoder .instance[Included ](_.as[ResourceObjects ].map(Included .apply))
156
+ implicit val includedDecoder = Decoder .instance[Included ](_.as[ResourceObjects ].right. map(Included .apply))
158
157
159
158
implicit val rootObjectDecoder = Decoder .instance[RootObject ](hcursor ⇒ {
160
159
for {
161
- data ← hcursor.downField(FieldNames .`data`).as[Option [Data ]]
162
- links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]]
163
- errors ← hcursor.downField(FieldNames .`errors`).as[Option [Errors ]]
164
- meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]]
165
- included ← hcursor.downField(FieldNames .`included`).as[Option [Included ]]
166
- jsonapi ← hcursor.downField(FieldNames .`jsonapi`).as[Option [JsonApi ]]
160
+ data ← hcursor.downField(FieldNames .`data`).as[Option [Data ]].right
161
+ links ← hcursor.downField(FieldNames .`links`).as[Option [Links ]].right
162
+ errors ← hcursor.downField(FieldNames .`errors`).as[Option [Errors ]].right
163
+ meta ← hcursor.downField(FieldNames .`meta`).as[Option [Meta ]].right
164
+ included ← hcursor.downField(FieldNames .`included`).as[Option [Included ]].right
165
+ jsonapi ← hcursor.downField(FieldNames .`jsonapi`).as[Option [JsonApi ]].right
167
166
} yield
168
167
RootObject (
169
168
data = data,
0 commit comments