@@ -21,6 +21,7 @@ module Data.Codec.Argonaut
21
21
, record
22
22
, recordProp
23
23
, fix
24
+ , prismaticCodec
24
25
, module Exports
25
26
) where
26
27
@@ -33,16 +34,16 @@ import Data.Array as A
33
34
import Data.Bifunctor as BF
34
35
import Data.Codec (BasicCodec , Codec , GCodec (..), basicCodec , bihoistGCodec , decode , encode )
35
36
import Data.Codec (decode , encode , (~), (<~<)) as Exports
36
- import Data.Either (Either (..))
37
+ import Data.Either (Either (..), note )
37
38
import Data.Generic.Rep (class Generic )
38
39
import Data.Generic.Rep.Show (genericShow )
39
40
import Data.Int as I
40
41
import Data.List ((:))
41
42
import Data.List as L
42
43
import Data.Maybe (Maybe (..), maybe , fromJust )
43
44
import Data.Profunctor.Star (Star (..))
44
- import Data.String as S
45
45
import Data.StrMap as SM
46
+ import Data.String as S
46
47
import Data.Symbol (class IsSymbol , SProxy , reflectSymbol )
47
48
import Data.Traversable (traverse )
48
49
import Data.Tuple (Tuple (..))
@@ -246,3 +247,15 @@ fix f =
246
247
basicCodec
247
248
(\x → decode (f (fix f)) x)
248
249
(\x → encode (f (fix f)) x)
250
+
251
+ -- | Adapts an existing codec with a pair of functions to allow a value to be
252
+ -- | further refined. If the inner decoder fails an `UnexpectedValue` error will
253
+ -- | be raised for JSON input.
254
+ -- |
255
+ -- | This function is named as such as the pair of functions it accepts
256
+ -- | correspond with the `preview` and `view` functions of a `Prism`-style lens.
257
+ prismaticCodec ∷ ∀ a b . (a → Maybe b ) → (b → a ) → JsonCodec a → JsonCodec b
258
+ prismaticCodec f g orig =
259
+ basicCodec
260
+ (\json → note (UnexpectedValue json) <<< f =<< (decode orig json))
261
+ (encode orig <<< g)
0 commit comments