@@ -30,18 +30,18 @@ module Data.Codec.Argonaut
3030import Prelude
3131
3232import Control.Monad.Reader (ReaderT (..), runReaderT )
33- import Control.Monad.Writer (Writer , writer , mapWriter )
33+ import Control.Monad.Writer (Writer , mapWriter , writer )
3434import Data.Argonaut.Core as J
3535import Data.Array as A
3636import Data.Bifunctor as BF
3737import Data.Codec (BasicCodec , Codec , GCodec (..), basicCodec , bihoistGCodec , decode , encode )
38- import Data.Codec (decode , encode , (~ ), (<~< ), (>~> )) as Exports
38+ import Data.Codec (decode , encode , (<~< ), (>~> ), (~ )) as Exports
3939import Data.Either (Either (..), note )
4040import Data.Generic.Rep (class Generic )
4141import Data.Int as I
4242import Data.List ((:))
4343import Data.List as L
44- import Data.Maybe (Maybe (..), maybe , fromJust )
44+ import Data.Maybe (Maybe (..), fromJust , maybe )
4545import Data.Profunctor.Star (Star (..))
4646import Data.String as S
4747import Data.String.CodeUnits as SCU
@@ -72,25 +72,25 @@ derive instance genericJsonDecodeError ∷ Generic JsonDecodeError _
7272
7373instance showJsonDecodeError ∷ Show JsonDecodeError where
7474 show = case _ of
75- TypeMismatch s -> " (TypeMismatch " <> show s <> " )"
76- UnexpectedValue j -> " (UnexpectedValue " <> J .stringify j <> " )"
77- AtIndex i e -> " (AtIndex " <> show i <> " " <> show e <> " )"
78- AtKey k e -> " (AtKey " <> show k <> " " <> show e <> " )"
79- Named s e -> " (Named " <> show s <> " " <> show e <> " )"
80- MissingValue -> " MissingValue"
75+ TypeMismatch s → " (TypeMismatch " <> show s <> " )"
76+ UnexpectedValue j → " (UnexpectedValue " <> J .stringify j <> " )"
77+ AtIndex i e → " (AtIndex " <> show i <> " " <> show e <> " )"
78+ AtKey k e → " (AtKey " <> show k <> " " <> show e <> " )"
79+ Named s e → " (Named " <> show s <> " " <> show e <> " )"
80+ MissingValue → " MissingValue"
8181
8282-- | Prints a `JsonDecodeError` as a somewhat readable error message.
8383printJsonDecodeError ∷ JsonDecodeError → String
8484printJsonDecodeError err =
8585 " An error occurred while decoding a JSON value:\n " <> go err
8686 where
87- go = case _ of
88- TypeMismatch ty → " Expected value of type '" <> ty <> " '."
89- UnexpectedValue val → " Unexpected value " <> J .stringify val <> " ."
90- AtIndex ix inner → " At array index " <> show ix <> " :\n " <> go inner
91- AtKey key inner → " At object key " <> key <> " :\n " <> go inner
92- Named name inner → " Under '" <> name <> " ':\n " <> go inner
93- MissingValue → " No value was found."
87+ go = case _ of
88+ TypeMismatch ty → " Expected value of type '" <> ty <> " '."
89+ UnexpectedValue val → " Unexpected value " <> J .stringify val <> " ."
90+ AtIndex ix inner → " At array index " <> show ix <> " :\n " <> go inner
91+ AtKey key inner → " At object key " <> key <> " :\n " <> go inner
92+ Named name inner → " Under '" <> name <> " ':\n " <> go inner
93+ MissingValue → " No value was found."
9494
9595-- | The "identity codec" for `Json` values.
9696json ∷ JsonCodec J.Json
@@ -162,7 +162,8 @@ type JIndexedCodec a =
162162 (Either JsonDecodeError )
163163 (Array J.Json )
164164 (L.List J.Json )
165- a a
165+ a
166+ a
166167
167168-- | A codec for types that are encoded as an array with a specific layout.
168169-- |
@@ -203,7 +204,8 @@ type JPropCodec a =
203204 (Either JsonDecodeError )
204205 (FO.Object J.Json )
205206 (L.List (Tuple String J.Json ))
206- a a
207+ a
208+ a
207209
208210-- | A codec for objects that are encoded with specific properties.
209211-- |
@@ -224,6 +226,7 @@ prop key codec = GCodec dec enc
224226 BF .lmap (AtKey key) case FO .lookup key obj of
225227 Just val → decode codec val
226228 Nothing → Left MissingValue
229+
227230 enc ∷ Star (Writer (L.List (Tuple String J.Json ))) a a
228231 enc = Star \val → writer $ Tuple val (pure (Tuple key (encode codec val)))
229232
@@ -264,28 +267,32 @@ recordProp
264267recordProp p codecA codecR =
265268 let key = reflectSymbol p in GCodec (dec' key) (enc' key)
266269 where
267- dec' ∷ String → ReaderT (FO.Object J.Json ) (Either JsonDecodeError ) (Record r' )
268- dec' key = ReaderT \obj → do
269- r ← decode codecR obj
270- a ← BF .lmap (AtKey key) case FO .lookup key obj of
271- Just val → decode codecA val
272- Nothing → Left MissingValue
273- pure $ unsafeSet key a r
274- enc' ∷ String → Star (Writer (L.List (Tuple String J.Json ))) (Record r' ) (Record r' )
275- enc' key = Star \val →
276- writer $ Tuple val
277- $ Tuple key (encode codecA (unsafeGet key val))
278- : encode codecR (unsafeForget val)
279- unsafeForget ∷ Record r' → Record r
280- unsafeForget = unsafeCoerce
281- unsafeSet ∷ String → a → Record r → Record r'
282- unsafeSet key a = unsafeCoerce <<< FO .insert key a <<< unsafeCoerce
283- unsafeGet ∷ String → Record r' → a
284- unsafeGet s = unsafePartial fromJust <<< FO .lookup s <<< unsafeCoerce
270+ dec' ∷ String → ReaderT (FO.Object J.Json ) (Either JsonDecodeError ) (Record r' )
271+ dec' key = ReaderT \obj → do
272+ r ← decode codecR obj
273+ a ← BF .lmap (AtKey key) case FO .lookup key obj of
274+ Just val → decode codecA val
275+ Nothing → Left MissingValue
276+ pure $ unsafeSet key a r
277+
278+ enc' ∷ String → Star (Writer (L.List (Tuple String J.Json ))) (Record r' ) (Record r' )
279+ enc' key = Star \val →
280+ writer $ Tuple val
281+ $ Tuple key (encode codecA (unsafeGet key val))
282+ : encode codecR (unsafeForget val)
283+
284+ unsafeForget ∷ Record r' → Record r
285+ unsafeForget = unsafeCoerce
286+
287+ unsafeSet ∷ String → a → Record r → Record r'
288+ unsafeSet key a = unsafeCoerce <<< FO .insert key a <<< unsafeCoerce
289+
290+ unsafeGet ∷ String → Record r' → a
291+ unsafeGet s = unsafePartial fromJust <<< FO .lookup s <<< unsafeCoerce
285292
286293jsonPrimCodec
287294 ∷ ∀ a
288- . String
295+ . String
289296 → (J.Json → Maybe a )
290297 → (a → J.Json )
291298 → JsonCodec a
0 commit comments