@@ -37,18 +37,19 @@ import Data.Codec (BasicCodec, Codec, GCodec(..), basicCodec, bihoistGCodec, dec
37
37
import Data.Codec (decode , encode , (~), (<~<)) as Exports
38
38
import Data.Either (Either (..), note )
39
39
import Data.Generic.Rep (class Generic )
40
- import Data.Generic.Rep.Show (genericShow )
41
40
import Data.Int as I
42
41
import Data.List ((:))
43
42
import Data.List as L
44
43
import Data.Maybe (Maybe (..), maybe , fromJust )
45
44
import Data.Profunctor.Star (Star (..))
46
- import Data.StrMap as SM
47
45
import Data.String as S
46
+ import Data.String.CodeUnits as SCU
48
47
import Data.Symbol (class IsSymbol , SProxy , reflectSymbol )
49
48
import Data.Traversable (traverse )
50
49
import Data.Tuple (Tuple (..))
50
+ import Foreign.Object as FO
51
51
import Partial.Unsafe (unsafePartial )
52
+ import Prim.Row as Row
52
53
import Unsafe.Coerce (unsafeCoerce )
53
54
54
55
-- | Codec type for `Json` values.
@@ -68,7 +69,13 @@ derive instance ordJsonDecodeError ∷ Ord JsonDecodeError
68
69
derive instance genericJsonDecodeError ∷ Generic JsonDecodeError _
69
70
70
71
instance showJsonDecodeError ∷ Show JsonDecodeError where
71
- show err = genericShow err
72
+ show = case _ of
73
+ TypeMismatch s -> " (TypeMismatch " <> show s <> " )"
74
+ UnexpectedValue j -> " (UnexpectedValue " <> J .stringify j <> " )"
75
+ AtIndex i e -> " (AtIndex " <> show i <> " " <> show e <> " )"
76
+ AtKey k e -> " (AtKey " <> show k <> " " <> show e <> " )"
77
+ Named s e -> " (Named " <> show s <> " " <> show e <> " )"
78
+ MissingValue -> " MissingValue"
72
79
73
80
-- | Prints a `JsonDecodeError` as a somewhat readable error message.
74
81
printJsonDecodeError ∷ JsonDecodeError → String
@@ -85,11 +92,11 @@ printJsonDecodeError err =
85
92
86
93
-- | The "identity codec" for `Json` values.
87
94
json ∷ JsonCodec J.Json
88
- json = basicCodec pure id
95
+ json = basicCodec pure identity
89
96
90
97
-- | A codec for `null` values in `Json`.
91
- null ∷ JsonCodec J.JNull
92
- null = jsonPrimCodec " Null" J .toNull J .fromNull
98
+ null ∷ JsonCodec Unit
99
+ null = jsonPrimCodec " Null" J .toNull (const J .jsonNull)
93
100
94
101
-- | A codec for `Boolean` values in `Json`.
95
102
boolean ∷ JsonCodec Boolean
@@ -107,22 +114,26 @@ int = jsonPrimCodec "Int" (I.fromNumber <=< J.toNumber) (J.fromNumber <<< I.toNu
107
114
string ∷ JsonCodec String
108
115
string = jsonPrimCodec " String" J .toString J .fromString
109
116
117
+ -- | A codec for `Codepoint` values in `Json`.
118
+ codePoint ∷ JsonCodec S.CodePoint
119
+ codePoint = jsonPrimCodec " CodePoint" (S .codePointAt 0 <=< J .toString) (J .fromString <<< S .singleton)
120
+
110
121
-- | A codec for `Char` values in `Json`.
111
122
char ∷ JsonCodec Char
112
- char = jsonPrimCodec " Char" (S .toChar <=< J .toString) (J .fromString <<< S .singleton)
123
+ char = jsonPrimCodec " Char" (SCU .toChar <=< J .toString) (J .fromString <<< SCU .singleton)
113
124
114
125
-- | A codec for `Void` values.
115
126
void ∷ JsonCodec Void
116
127
void = jsonPrimCodec " Void" (const Nothing ) absurd
117
128
118
- -- | A codec for a `JArray ` values in `Json`. This does not decode the values
129
+ -- | A codec for `Array Json ` values in `Json`. This does not decode the values
119
130
-- | of the array, for that use `array` for a general array decoder, or
120
131
-- | `indexedArray` with `index` to decode fixed length array encodings.
121
- jarray ∷ JsonCodec J.JArray
132
+ jarray ∷ JsonCodec ( Array J.Json )
122
133
jarray = jsonPrimCodec " Array" J .toArray J .fromArray
123
134
124
135
-- | A codec for `JObject` values in `Json`.
125
- jobject ∷ JsonCodec J.JObject
136
+ jobject ∷ JsonCodec ( FO.Object J.Json )
126
137
jobject = jsonPrimCodec " Object" J .toObject J .fromObject
127
138
128
139
-- | A codec for `Array` values.
@@ -144,7 +155,7 @@ array codec = GCodec dec enc
144
155
type JIndexedCodec a =
145
156
Codec
146
157
(Either JsonDecodeError )
147
- J.JArray
158
+ ( Array J.Json )
148
159
(L.List J.Json )
149
160
a a
150
161
@@ -181,27 +192,27 @@ index ix codec = GCodec dec enc
181
192
type JPropCodec a =
182
193
Codec
183
194
(Either JsonDecodeError )
184
- J.JObject
185
- (L.List J.JAssoc )
195
+ ( FO.Object J.Json )
196
+ (L.List ( Tuple String J.Json ) )
186
197
a a
187
198
188
199
-- | A codec for objects that are encoded with specific properties.
189
200
object ∷ ∀ a . String → JPropCodec a → JsonCodec a
190
201
object name =
191
202
bihoistGCodec
192
203
(\r → ReaderT (BF .lmap (Named name) <<< runReaderT r <=< decode jobject))
193
- (mapWriter (BF .rmap (J .fromObject <<< SM .fromFoldable)))
204
+ (mapWriter (BF .rmap (J .fromObject <<< FO .fromFoldable)))
194
205
195
206
-- | A codec for a property of an object.
196
207
prop ∷ ∀ a . String → JsonCodec a → JPropCodec a
197
208
prop key codec = GCodec dec enc
198
209
where
199
- dec ∷ ReaderT J.JObject (Either JsonDecodeError ) a
210
+ dec ∷ ReaderT ( FO.Object J.Json ) (Either JsonDecodeError ) a
200
211
dec = ReaderT \obj →
201
- BF .lmap (AtKey key) case SM .lookup key obj of
212
+ BF .lmap (AtKey key) case FO .lookup key obj of
202
213
Just val → decode codec val
203
214
Nothing → Left MissingValue
204
- enc ∷ Star (Writer (L.List J.JAssoc )) a a
215
+ enc ∷ Star (Writer (L.List ( Tuple String J.Json ) )) a a
205
216
enc = Star \val → writer $ Tuple val (pure (Tuple key (encode codec val)))
206
217
207
218
-- | The starting value for a object-record codec. Used with `recordProp` it
@@ -223,32 +234,32 @@ record = GCodec (pure {}) (Star \val → writer (Tuple val L.Nil))
223
234
recordProp
224
235
∷ ∀ p a r r'
225
236
. IsSymbol p
226
- ⇒ RowCons p a r r'
237
+ ⇒ Row.Cons p a r r'
227
238
⇒ SProxy p
228
239
→ JsonCodec a
229
240
→ JPropCodec (Record r )
230
241
→ JPropCodec (Record r' )
231
242
recordProp p codecA codecR =
232
243
let key = reflectSymbol p in GCodec (dec' key) (enc' key)
233
244
where
234
- dec' ∷ String → ReaderT J.JObject (Either JsonDecodeError ) (Record r' )
245
+ dec' ∷ String → ReaderT ( FO.Object J.Json ) (Either JsonDecodeError ) (Record r' )
235
246
dec' key = ReaderT \obj → do
236
247
r ← decode codecR obj
237
- a ← BF .lmap (AtKey key) case SM .lookup key obj of
248
+ a ← BF .lmap (AtKey key) case FO .lookup key obj of
238
249
Just val → decode codecA val
239
250
Nothing → Left MissingValue
240
251
pure $ unsafeSet key a r
241
- enc' ∷ String → Star (Writer (L.List J.JAssoc )) (Record r' ) (Record r' )
252
+ enc' ∷ String → Star (Writer (L.List ( Tuple String J.Json ) )) (Record r' ) (Record r' )
242
253
enc' key = Star \val →
243
254
writer $ Tuple val
244
255
$ Tuple key (encode codecA (unsafeGet key val))
245
256
: encode codecR (unsafeForget val)
246
257
unsafeForget ∷ Record r' → Record r
247
258
unsafeForget = unsafeCoerce
248
259
unsafeSet ∷ String → a → Record r → Record r'
249
- unsafeSet key a = unsafeCoerce <<< SM .insert key a <<< unsafeCoerce
260
+ unsafeSet key a = unsafeCoerce <<< FO .insert key a <<< unsafeCoerce
250
261
unsafeGet ∷ String → Record r' → a
251
- unsafeGet s = unsafePartial fromJust <<< SM .lookup s <<< unsafeCoerce
262
+ unsafeGet s = unsafePartial fromJust <<< FO .lookup s <<< unsafeCoerce
252
263
253
264
jsonPrimCodec
254
265
∷ ∀ a
0 commit comments