@@ -10,6 +10,7 @@ module Simple.JSON
10
10
, read_
11
11
, parseJSON
12
12
, undefined
13
+ , unsafeStringify
13
14
14
15
, class ReadForeign
15
16
, readImpl
@@ -49,14 +50,13 @@ import Foreign (F, Foreign, ForeignError(..), MultipleErrors, fail, isNull, isUn
49
50
import Foreign.Index (readProp )
50
51
import Foreign.Object (Object )
51
52
import Foreign.Object as Object
52
- import Global.Unsafe (unsafeStringify )
53
53
import Partial.Unsafe (unsafeCrashWith )
54
54
import Prim.Row as Row
55
- import Prim.RowList (class RowToList , Cons , Nil , kind RowList )
55
+ import Prim.RowList (class RowToList , Cons , Nil , RowList )
56
56
import Record (get )
57
57
import Record.Builder (Builder )
58
58
import Record.Builder as Builder
59
- import Type.Prelude (RLProxy (..))
59
+ import Type.Prelude (Proxy (..))
60
60
61
61
-- | An alias for the Either result of decoding
62
62
type E a = Either MultipleErrors a
@@ -84,12 +84,19 @@ readJSON_ :: forall a
84
84
-> Maybe a
85
85
readJSON_ = hush <<< readJSON
86
86
87
+ -- | Uses the global JSON object to turn anything into a string. Careful! Trying
88
+ -- | to serialize functions returns undefined
89
+ foreign import _unsafeStringify :: forall a . a -> String
90
+
91
+ unsafeStringify :: forall a . a -> String
92
+ unsafeStringify = _unsafeStringify
93
+
87
94
-- | Write a JSON string from a type `a`.
88
95
writeJSON :: forall a
89
96
. WriteForeign a
90
97
=> a
91
98
-> String
92
- writeJSON = unsafeStringify <<< writeImpl
99
+ writeJSON = _unsafeStringify <<< writeImpl
93
100
94
101
write :: forall a
95
102
. WriteForeign a
@@ -199,12 +206,12 @@ instance readRecord ::
199
206
) => ReadForeign (Record fields ) where
200
207
readImpl o = flip Builder .build {} <$> getFields fieldListP o
201
208
where
202
- fieldListP = RLProxy :: RLProxy fieldList
209
+ fieldListP = Proxy :: Proxy fieldList
203
210
204
211
-- | A class for reading foreign values from properties
205
- class ReadForeignFields (xs :: RowList ) (from :: # Type ) (to :: # Type )
212
+ class ReadForeignFields (xs :: RowList Type ) (from :: Row Type ) (to :: Row Type )
206
213
| xs -> from to where
207
- getFields :: RLProxy xs
214
+ getFields :: Proxy xs
208
215
-> Foreign
209
216
-> F (Builder (Record from ) (Record to ))
210
217
@@ -222,7 +229,7 @@ instance readFieldsCons ::
222
229
pure $ Builder .insert nameP value
223
230
rest = getFields tailP obj
224
231
nameP = SProxy :: SProxy name
225
- tailP = RLProxy :: RLProxy tail
232
+ tailP = Proxy :: Proxy tail
226
233
name = reflectSymbol nameP
227
234
withExcept' = withExcept <<< map $ ErrorAtProperty name
228
235
@@ -246,11 +253,11 @@ instance readForeignVariant ::
246
253
( RowToList variants rl
247
254
, ReadForeignVariant rl variants
248
255
) => ReadForeign (Variant variants ) where
249
- readImpl o = readVariantImpl (RLProxy :: RLProxy rl ) o
256
+ readImpl o = readVariantImpl (Proxy :: Proxy rl ) o
250
257
251
- class ReadForeignVariant (xs :: RowList ) (row :: # Type )
258
+ class ReadForeignVariant (xs :: RowList Type ) (row :: Row Type )
252
259
| xs -> row where
253
- readVariantImpl :: RLProxy xs
260
+ readVariantImpl :: Proxy xs
254
261
-> Foreign
255
262
-> F (Variant row )
256
263
@@ -272,7 +279,7 @@ instance readVariantCons ::
272
279
pure $ inj namep value
273
280
else
274
281
(fail <<< ForeignError $ " Did not match variant tag " <> name)
275
- <|> readVariantImpl (RLProxy :: RLProxy tail ) o
282
+ <|> readVariantImpl (Proxy :: Proxy tail ) o
276
283
where
277
284
namep = SProxy :: SProxy name
278
285
name = reflectSymbol namep
@@ -318,10 +325,10 @@ instance recordWriteForeign ::
318
325
) => WriteForeign (Record row ) where
319
326
writeImpl rec = unsafeToForeign $ Builder .build steps {}
320
327
where
321
- rlp = RLProxy :: RLProxy rl
328
+ rlp = Proxy :: Proxy rl
322
329
steps = writeImplFields rlp rec
323
330
324
- class WriteForeignFields (rl :: RowList ) row (from :: # Type ) (to :: # Type )
331
+ class WriteForeignFields (rl :: RowList Type ) row (from :: Row Type ) (to :: Row Type )
325
332
| rl -> row from to where
326
333
writeImplFields :: forall g . g rl -> Record row -> Builder (Record from ) (Record to )
327
334
@@ -337,7 +344,7 @@ instance consWriteForeignFields ::
337
344
where
338
345
namep = SProxy :: SProxy name
339
346
value = writeImpl $ get namep rec
340
- tailp = RLProxy :: RLProxy tail
347
+ tailp = Proxy :: Proxy tail
341
348
rest = writeImplFields tailp rec
342
349
result = Builder .insert namep value <<< rest
343
350
instance nilWriteForeignFields ::
@@ -348,9 +355,9 @@ instance writeForeignVariant ::
348
355
( RowToList row rl
349
356
, WriteForeignVariant rl row
350
357
) => WriteForeign (Variant row ) where
351
- writeImpl variant = writeVariantImpl (RLProxy :: RLProxy rl ) variant
358
+ writeImpl variant = writeVariantImpl (Proxy :: Proxy rl ) variant
352
359
353
- class WriteForeignVariant (rl :: RowList ) (row :: # Type )
360
+ class WriteForeignVariant (rl :: RowList Type ) (row :: Row Type )
354
361
| rl -> row where
355
362
writeVariantImpl :: forall g . g rl -> Variant row -> Foreign
356
363
@@ -370,7 +377,7 @@ instance consWriteForeignVariant ::
370
377
on
371
378
namep
372
379
writeVariant
373
- (writeVariantImpl (RLProxy :: RLProxy tail ))
380
+ (writeVariantImpl (Proxy :: Proxy tail ))
374
381
variant
375
382
where
376
383
namep = SProxy :: SProxy name
0 commit comments