A-gent-0.11.0.18: src/Agent/Data/JSON.hs
{-# OPTIONS_GHC -Wall -Werror #-}
{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
-- |
-- Copyright : (c) 2026 SPISE MISU ApS
-- License : SSPL-1.0 OR AGPL-3.0-only
-- Maintainer : SPISE MISU <mail+hackage@spisemisu.com>
-- Stability : experimental
--------------------------------------------------------------------------------
module Agent.Data.JSON
( -- * Infer JSON from Data
Data
-- * JSON Encoder/Decoder
, DecodeError(InvalidJSON, DiffSchema)
, encode
, decode
)
where
--------------------------------------------------------------------------------
import Data.Data ( Data )
import Internal.GaloisInc.Text.JSON.Generic
( Result (Error, Ok)
, encodeJSON
, fromJSON
)
import Internal.GaloisInc.Text.JSON.String
( readJSValue
, runGetJSON
)
--------------------------------------------------------------------------------
data DecodeError
= InvalidJSON
| DiffSchema
--------------------------------------------------------------------------------
encode
:: Data a
=> a
-> String
encode = encodeJSON
decode
:: Data a
=> String
-> Either DecodeError a
decode json =
case runGetJSON readJSValue json of
Left _ -> Left InvalidJSON
Right v ->
case fromJSON v of
Error _ -> Left DiffSchema
Ok a -> Right a