/
JSON.purs
311 lines (261 loc) · 9.24 KB
/
JSON.purs
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
module Simple.JSON (
readJSON
, readJSON'
, writeJSON
, write
, read
, read'
, class ReadForeign
, readImpl
, class ReadForeignFields
, getFields
, class ReadForeignVariant
, readVariantImpl
, class WriteForeign
, writeImpl
, class WriteForeignFields
, writeImplFields
, class WriteForeignVariant
, writeVariantImpl
) where
import Prelude
import Control.Alt ((<|>))
import Control.Monad.Except (runExcept, withExcept)
import Data.Either (Either)
import Data.Foreign (F, Foreign, ForeignError(..), MultipleErrors, fail, readArray, readBoolean, readChar, readInt, readNull, readNumber, readString, toForeign)
import Data.Foreign.Index (readProp)
import Data.Foreign.Internal (readStrMap)
import Data.Foreign.JSON (parseJSON)
import Data.Foreign.NullOrUndefined (NullOrUndefined(NullOrUndefined), readNullOrUndefined, unNullOrUndefined, undefined)
import Data.Maybe (Maybe(..), maybe)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Data.Record (get)
import Data.Record.Builder (Builder)
import Data.Record.Builder as Builder
import Data.StrMap as StrMap
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Traversable (sequence, traverse)
import Data.Variant (Variant, inj, on)
import Global.Unsafe (unsafeStringify)
import Partial.Unsafe (unsafeCrashWith)
import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList)
-- | Read a JSON string to a type `a` while returning a `MultipleErrors` if the
-- | parsing failed.
readJSON :: forall a
. ReadForeign a
=> String
-> Either MultipleErrors a
readJSON = runExcept <<< (readImpl <=< parseJSON)
-- | Read a JSON string to a type `a` using `F a`. Useful with record types.
readJSON' :: forall a
. ReadForeign a
=> String
-> F a
readJSON' = readImpl <=< parseJSON
-- | Write a JSON string from a type `a`.
writeJSON :: forall a
. WriteForeign a
=> a
-> String
writeJSON = unsafeStringify <<< writeImpl
write :: forall a
. WriteForeign a
=> a
-> Foreign
write = writeImpl
read :: forall a
. ReadForeign a
=> Foreign
-> Either MultipleErrors a
read = runExcept <<< readImpl
read' :: forall a
. ReadForeign a
=> Foreign
-> F a
read' = readImpl
-- | A class for reading foreign values to a type
class ReadForeign a where
readImpl :: Foreign -> F a
instance readForeign :: ReadForeign Foreign where
readImpl = pure
instance readChar :: ReadForeign Char where
readImpl = readChar
instance readNumber :: ReadForeign Number where
readImpl = readNumber
instance readInt :: ReadForeign Int where
readImpl = readInt
instance readString :: ReadForeign String where
readImpl = readString
instance readBoolean :: ReadForeign Boolean where
readImpl = readBoolean
instance readArray :: ReadForeign a => ReadForeign (Array a) where
readImpl = traverse readImpl <=< readArray
instance readNullOrUndefined :: ReadForeign a => ReadForeign (NullOrUndefined a) where
readImpl = readNullOrUndefined readImpl
instance readMaybe :: ReadForeign a => ReadForeign (Maybe a) where
readImpl = map unNullOrUndefined <<< readImpl
instance readNullable :: ReadForeign a => ReadForeign (Nullable a) where
readImpl o = withExcept (map reformat) $
map toNullable <$> traverse readImpl =<< readNull o
where
reformat error = case error of
TypeMismatch inner other -> TypeMismatch ("Nullable " <> inner) other
_ -> error
instance readStrMap :: ReadForeign a => ReadForeign (StrMap.StrMap a) where
readImpl = sequence <<< StrMap.mapWithKey (const readImpl) <=< readStrMap
instance readRecord ::
( RowToList fields fieldList
, ReadForeignFields fieldList () fields
) => ReadForeign (Record fields) where
readImpl o = do
steps <- getFields fieldListP o
pure $ Builder.build steps {}
where
fieldListP = RLProxy :: RLProxy fieldList
-- | A class for reading foreign values from properties
class ReadForeignFields (xs :: RowList) (from :: # Type) (to :: # Type)
| xs -> from to where
getFields :: RLProxy xs
-> Foreign
-> F (Builder (Record from) (Record to))
instance readFieldsCons ::
( IsSymbol name
, ReadForeign ty
, ReadForeignFields tail from from'
, RowLacks name from'
, RowCons name ty from' to
) => ReadForeignFields (Cons name ty tail) from to where
getFields _ obj = do
value :: ty <- withExcept' $ readImpl =<< readProp name obj
rest <- getFields tailP obj
let
first :: Builder (Record from') (Record to)
first = Builder.insert nameP value
pure $ first <<< rest
where
nameP = SProxy :: SProxy name
tailP = RLProxy :: RLProxy tail
name = reflectSymbol nameP
withExcept' = withExcept <<< map $ ErrorAtProperty name
instance readFieldsNil ::
ReadForeignFields Nil () () where
getFields _ _ =
pure id
instance readForeignVariant ::
( RowToList variants rl
, ReadForeignVariant rl variants
) => ReadForeign (Variant variants) where
readImpl o = readVariantImpl (RLProxy :: RLProxy rl) o
class ReadForeignVariant (xs :: RowList) (row :: # Type)
| xs -> row where
readVariantImpl :: RLProxy xs
-> Foreign
-> F (Variant row)
instance readVariantNil ::
ReadForeignVariant Nil trash where
readVariantImpl _ _ = fail $ ForeignError "Unable to match any variant member."
instance readVariantCons ::
( IsSymbol name
, ReadForeign ty
, RowCons name ty trash row
, ReadForeignVariant tail row
) => ReadForeignVariant (Cons name ty tail) row where
readVariantImpl _ o = do
obj :: { type :: String, value :: Foreign } <- readImpl o
if obj.type == name
then do
value :: ty <- readImpl obj.value
pure $ inj namep value
else
(fail <<< ForeignError $ "Did not match variant tag " <> name)
<|> readVariantImpl (RLProxy :: RLProxy tail) o
where
namep = SProxy :: SProxy name
name = reflectSymbol namep
-- | A class for writing a value into JSON
-- | need to do this intelligently using Foreign probably, because of null and undefined whatever
class WriteForeign a where
writeImpl :: a -> Foreign
instance writeForeignForeign :: WriteForeign Foreign where
writeImpl = id
instance writeForeignString :: WriteForeign String where
writeImpl = toForeign
instance writeForeignInt :: WriteForeign Int where
writeImpl = toForeign
instance writeForeignChar :: WriteForeign Char where
writeImpl = toForeign
instance writeForeignNumber :: WriteForeign Number where
writeImpl = toForeign
instance writeForeignBoolean :: WriteForeign Boolean where
writeImpl = toForeign
instance writeForeignArray :: WriteForeign a => WriteForeign (Array a) where
writeImpl xs = toForeign $ writeImpl <$> xs
instance writeForeignNullOrUndefined :: WriteForeign a => WriteForeign (NullOrUndefined a) where
writeImpl (NullOrUndefined a) = maybe undefined writeImpl a
instance writeForeignMaybe :: WriteForeign a => WriteForeign (Maybe a) where
writeImpl (Just a) = writeImpl a
writeImpl Nothing = toForeign $ toNullable Nothing
instance writeForeignNullable :: WriteForeign a => WriteForeign (Nullable a) where
writeImpl = writeImpl <<< toMaybe
instance writeForeignStrMap :: WriteForeign a => WriteForeign (StrMap.StrMap a) where
writeImpl = toForeign <<< StrMap.mapWithKey (const writeImpl)
instance recordWriteForeign ::
( RowToList row rl
, WriteForeignFields rl row () to
) => WriteForeign (Record row) where
writeImpl rec = toForeign $ Builder.build steps {}
where
rlp = RLProxy :: RLProxy rl
steps = writeImplFields rlp rec
class WriteForeignFields (rl :: RowList) row (from :: # Type) (to :: # Type)
| rl -> row from to where
writeImplFields :: forall g. g rl -> Record row -> Builder (Record from) (Record to)
instance consWriteForeignFields ::
( IsSymbol name
, WriteForeign ty
, WriteForeignFields tail row from from'
, RowCons name ty whatever row
, RowLacks name from'
, RowCons name Foreign from' to
) => WriteForeignFields (Cons name ty tail) row from to where
writeImplFields _ rec = result
where
namep = SProxy :: SProxy name
value = writeImpl $ get namep rec
tailp = RLProxy :: RLProxy tail
rest = writeImplFields tailp rec
result = Builder.insert namep value <<< rest
instance nilWriteForeignFields ::
WriteForeignFields Nil row () () where
writeImplFields _ _ = id
instance writeForeignVariant ::
( RowToList row rl
, WriteForeignVariant rl row
) => WriteForeign (Variant row) where
writeImpl variant = writeVariantImpl (RLProxy :: RLProxy rl) variant
class WriteForeignVariant (rl :: RowList) (row :: # Type)
| rl -> row where
writeVariantImpl :: forall g. g rl -> Variant row -> Foreign
instance nilWriteForeignVariant ::
WriteForeignVariant Nil () where
writeVariantImpl _ _ =
-- a PureScript-defined variant cannot reach this path, but a JavaScript FFI one could.
unsafeCrashWith "Variant was not able to be writen row WriteForeign."
instance consWriteForeignVariant ::
( IsSymbol name
, WriteForeign ty
, RowCons name ty subRow row
, WriteForeignVariant tail subRow
) => WriteForeignVariant (Cons name ty tail) row where
writeVariantImpl _ variant = do
on
namep
writeVariant
(writeVariantImpl (RLProxy :: RLProxy tail))
variant
where
namep = SProxy :: SProxy name
writeVariant value = toForeign
{ type: reflectSymbol namep
, value: writeImpl value
}