{-# LANGUAGE DeriveAnyClass     #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia        #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Ledger.Builtins.Orphans where

import PlutusTx.Prelude qualified as PlutusTx

import Data.Aeson.Extras qualified as JSON

import Codec.Serialise (Serialise (decode, encode))
import Data.Aeson (FromJSON (parseJSON), ToJSON (toJSON))
import Data.Aeson qualified as JSON
import GHC.Generics (Generic)
import PlutusCore.Data
import PlutusTx qualified as PlutusTx
import PlutusTx.Builtins.Internal (BuiltinData (..))

instance ToJSON PlutusTx.BuiltinByteString where
    toJSON :: BuiltinByteString -> Value
toJSON = Text -> Value
JSON.String (Text -> Value)
-> (BuiltinByteString -> Text) -> BuiltinByteString -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
JSON.encodeByteString (ByteString -> Text)
-> (BuiltinByteString -> ByteString) -> BuiltinByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinByteString -> ByteString
forall arep a. FromBuiltin arep a => arep -> a
PlutusTx.fromBuiltin

instance FromJSON PlutusTx.BuiltinByteString where
    parseJSON :: Value -> Parser BuiltinByteString
parseJSON Value
v = ByteString -> BuiltinByteString
forall a arep. ToBuiltin a arep => a -> arep
PlutusTx.toBuiltin (ByteString -> BuiltinByteString)
-> Parser ByteString -> Parser BuiltinByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser ByteString
JSON.decodeByteString Value
v

instance ToJSON PlutusTx.BuiltinData where
  toJSON :: BuiltinData -> Value
toJSON = Data -> Value
forall a. ToJSON a => a -> Value
toJSON (Data -> Value) -> (BuiltinData -> Data) -> BuiltinData -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinData -> Data
PlutusTx.builtinDataToData

instance FromJSON PlutusTx.BuiltinData where
  parseJSON :: Value -> Parser BuiltinData
parseJSON Value
v = Value -> Parser Data
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v Parser Data -> (Data -> Parser BuiltinData) -> Parser BuiltinData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= BuiltinData -> Parser BuiltinData
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BuiltinData -> Parser BuiltinData)
-> (Data -> BuiltinData) -> Data -> Parser BuiltinData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Data -> BuiltinData
PlutusTx.dataToBuiltinData

instance Serialise PlutusTx.BuiltinData where
  encode :: BuiltinData -> Encoding
encode = Data -> Encoding
forall a. Serialise a => a -> Encoding
encode (Data -> Encoding)
-> (BuiltinData -> Data) -> BuiltinData -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuiltinData -> Data
PlutusTx.builtinDataToData
  decode :: Decoder s BuiltinData
decode = Data -> BuiltinData
PlutusTx.dataToBuiltinData (Data -> BuiltinData) -> Decoder s Data -> Decoder s BuiltinData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Data
forall a s. Serialise a => Decoder s a
decode

deriving stock instance Generic BuiltinData

deriving via (JSON.JSONViaSerialise Data) instance ToJSON Data
deriving via (JSON.JSONViaSerialise Data) instance FromJSON Data