This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
JSON.hs
315 lines (241 loc) · 11.8 KB
/
JSON.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module MusicBrainz.API.JSON
( Annotation(..)
, MaybeObject(..)
, RefObject(..)
, EligibleForCleanup(..)
, TopLevel
) where
import Control.Lens hiding ((.=))
import Data.Aeson
import Data.Set (Set)
import Data.Text (Text)
import Network.URI (URI)
import qualified Data.HashMap.Strict as HMap
import qualified Data.Map as Map
import qualified Data.Text as Text
import MusicBrainz
import MusicBrainz.Data.Recording (RecordingUse(..))
--------------------------------------------------------------------------------
instance ToJSON (MBID a) where
toJSON = toJSON . view (re mbid)
--------------------------------------------------------------------------------
instance (ToJSON a, ToJSON (Ref a)) => ToJSON (CoreEntity a) where
toJSON e = object [ "mbid" .= coreRef e
, "data" .= coreData e
, "revision" .= coreRevision e
]
--------------------------------------------------------------------------------
instance (ToJSON a, ToJSON (Ref a)) => ToJSON (Entity a) where
toJSON e = object [ "ref" .= entityRef e
, "data" .= entityData e
]
--------------------------------------------------------------------------------
instance ToJSON (Alias a) where
toJSON Alias{..} = object [ "name" .= aliasName
, "sort-name" .= aliasSortName
, "locale" .= aliasLocale
, "type" .= aliasType
, "begin-date" .= aliasBeginDate
, "end-date" .= aliasEndDate
, "ended" .= aliasEnded
, "primary-for-locale" .= aliasPrimaryForLocale
]
--------------------------------------------------------------------------------
instance ToJSON Artist where
toJSON Artist{..} = object [ "name" .= artistName
, "sort-name" .= artistSortName
, "comment" .= artistComment
, "begin-date" .= artistBeginDate
, "end-date" .= artistEndDate
, "ended" .= artistEnded
, "gender" .= artistGender
, "type" .= artistType
, "country" .= artistCountry
]
--------------------------------------------------------------------------------
instance ToJSON ArtistCreditName where
toJSON ArtistCreditName{..} = object [ "artist" .= acnArtist
, "name" .= acnName
, "join-phrase" .= acnJoinPhrase
]
--------------------------------------------------------------------------------
instance ToJSON ArtistType where
toJSON ArtistType{..} = object [ "name" .= artistTypeName ]
--------------------------------------------------------------------------------
instance ToJSON Barcode where
toJSON = toJSON . review barcode
--------------------------------------------------------------------------------
instance ToJSON CdToc where
toJSON CdToc{..} = object [ "leadout-offset" .= cdTocLeadoutOffset
, "track-offsets" .= cdTocTrackOffsets
]
--------------------------------------------------------------------------------
instance ToJSON Editor where
toJSON Editor{..} = object [ "name" .= editorName
]
--------------------------------------------------------------------------------
instance ToJSON Gender where
toJSON Gender{..} = object [ "name" .= genderName ]
--------------------------------------------------------------------------------
instance ToJSON Label where
toJSON Label{..} = object [ "name" .= labelName
, "sort-name" .= labelSortName
, "comment" .= labelComment
, "begin-date" .= labelBeginDate
, "end-date" .= labelEndDate
, "ended" .= labelEnded
, "type" .= labelType
, "label-code" .= labelCode
]
--------------------------------------------------------------------------------
instance ToJSON LinkedRelationship where
toJSON r = case r of
ArtistRelationship target rel -> go "artist" target rel
LabelRelationship target rel -> go "label" target rel
RecordingRelationship target rel -> go "recording" target rel
ReleaseRelationship target rel -> go "release" target rel
ReleaseGroupRelationship target rel -> go "release-group" target rel
UrlRelationship target rel -> go "url" target rel
WorkRelationship target rel -> go "work" target rel
where
go t target rel = object
[ "target-type" .= (t :: Text)
, "target" .= target
, "type" .= relType rel
, "begin-date" .= relBeginDate rel
, "end-date" .= relEndDate rel
, "ended" .= relEnded rel
, "attributes" .= relAttributes rel
]
--------------------------------------------------------------------------------
instance ToJSON Medium where
toJSON Medium{..} = object [ "name" .= mediumName
, "format" .= mediumFormat
, "position" .= mediumPosition
, "tracks" .= mediumTracks
, "cdtocs" .= mediumCdTocs
]
--------------------------------------------------------------------------------
instance ToJSON Recording where
toJSON Recording{..} = object [ "name" .= recordingName
, "comment" .= recordingComment
, "artist-credit" .= recordingArtistCredit
, "duration" .= recordingDuration
]
--------------------------------------------------------------------------------
instance ToJSON Release where
toJSON Release{..} = object [ "name" .= releaseName
, "comment" .= releaseComment
, "artist-credit" .= releaseArtistCredit
, "release-group" .= releaseReleaseGroup
, "date" .= releaseDate
, "country" .= releaseCountry
, "script" .= releaseScript
, "language" .= releaseLanguage
, "packaging" .= releasePackaging
, "status" .= releaseStatus
, "barcode" .= releaseBarcode
]
--------------------------------------------------------------------------------
instance ToJSON ReleaseGroup where
toJSON ReleaseGroup{..} = object
[ "name" .= releaseGroupName
, "comment" .= releaseGroupComment
, "primary-type" .= releaseGroupPrimaryType
, "artist-credit" .= releaseGroupArtistCredit
, "secondary-types" .= releaseGroupSecondaryTypes
]
--------------------------------------------------------------------------------
instance ToJSON ReleaseLabel where
toJSON ReleaseLabel{..} = object [ "label" .= releaseLabel
, "catalog-number" .= releaseCatalogNumber
]
--------------------------------------------------------------------------------
instance ToJSON (Revision a) where
toJSON r = object
[ "created-at" .= revisionCreatedAt r ]
--------------------------------------------------------------------------------
instance ToJSON Track where
toJSON Track{..} = object [ "name" .= trackName
, "recording" .= trackRecording
, "length" .= trackDuration
, "artist-credit" .= trackArtistCredit
, "number" .= trackPosition
]
--------------------------------------------------------------------------------
instance ToJSON URI where
toJSON = toJSON . show
--------------------------------------------------------------------------------
instance ToJSON Url where
toJSON Url{..} = object [ "url" .= urlUrl ]
--------------------------------------------------------------------------------
instance ToJSON Work where
toJSON Work{..} = object [ "name" .= workName
, "comment" .= workComment
, "language" .= workLanguage
, "type" .= workType
]
--------------------------------------------------------------------------------
instance ToJSON PartialDate where
toJSON date = let (y, m, d) = date ^. re partialDate
in object [ "year" .= y
, "month" .= m
, "day" .= d
]
--------------------------------------------------------------------------------
instance ToJSON RecordingUse where
toJSON RecordingUse{..} = object [ "track" .= recordingTrack
, "release" .= recordingTrackRelease
]
--------------------------------------------------------------------------------
instance (Referenceable a, ToJSON (RefSpec a)) => ToJSON (Ref a) where
toJSON = toJSON . dereference
--------------------------------------------------------------------------------
newtype RefObject a = RefObject (Ref a)
instance (Referenceable a, ToJSON (RefSpec a)) => ToJSON (RefObject a) where
toJSON (RefObject r) = object [ "ref" .= r ]
--------------------------------------------------------------------------------
newtype MaybeObject a = MaybeObject (Maybe a)
instance ToJSON a => ToJSON (MaybeObject a) where
toJSON (MaybeObject (Just a)) = toJSON a
toJSON (MaybeObject Nothing) = object []
--------------------------------------------------------------------------------
instance (ToJSON v, ToJSON (RefSpec k), Referenceable k) => ToJSON (Map.Map (Ref k) v) where
toJSON = Object . HMap.fromList . Map.toList . Map.mapKeys showRef . Map.map toJSON
where
showRef r = case toJSON r of
String s -> s
Number n -> Text.pack $ show n
_ -> error "Reference cannot be used a key"
--------------------------------------------------------------------------------
instance ToJSON ISWC where
toJSON = toJSON . view (re iswc)
--------------------------------------------------------------------------------
instance ToJSON IPI where
toJSON = toJSON . view (re ipi)
--------------------------------------------------------------------------------
newtype Annotation = Annotation Text
instance ToJSON Annotation where
toJSON (Annotation t) = object [ "annotation" .= t ]
--------------------------------------------------------------------------------
newtype EligibleForCleanup = EligibleForCleanup Bool
instance ToJSON EligibleForCleanup where
toJSON (EligibleForCleanup e) = object [ "eligible" .= e ]
--------------------------------------------------------------------------------
class ToJSON a => TopLevel a
instance (Referenceable a, ToJSON (RefSpec a)) => TopLevel (RefObject a)
instance (ToJSON (RefSpec a), ToJSON a, Referenceable a) => TopLevel (CoreEntity a)
instance (ToJSON (RefSpec a), ToJSON a, Referenceable a) => TopLevel (Entity a)
instance (ToJSON v, ToJSON (RefSpec k), Referenceable k) => TopLevel (Map.Map (Ref k) v)
instance ToJSON a => TopLevel (MaybeObject a)
instance ToJSON a => TopLevel (Set a)
instance TopLevel ()
instance TopLevel Annotation
instance TopLevel EligibleForCleanup
instance TopLevel a => TopLevel [a]
instance TopLevel RecordingUse