-
Notifications
You must be signed in to change notification settings - Fork 176
Add ToJSON/FromJSON instances for EraTxAuxData #5853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koslambrou
wants to merge
1
commit into
koslambrou/erascript-json
Choose a base branch
from
koslambrou/eratxauxdata-json
base: koslambrou/erascript-json
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| {-# LANGUAGE BangPatterns #-} | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DeriveAnyClass #-} | ||
| {-# LANGUAGE DeriveGeneric #-} | ||
|
|
@@ -64,11 +65,18 @@ import Cardano.Ledger.MemoBytes ( | |
| ) | ||
| import Cardano.Ledger.MemoBytes.Internal (mkMemoBytesShort) | ||
| import qualified Codec.Serialise as Cborg (Serialise (..)) | ||
| import Control.Applicative (asum) | ||
| import Control.DeepSeq (NFData) | ||
| import Data.Aeson (ToJSON (..), Value (Null)) | ||
| import Control.Monad ((<$!>)) | ||
| import Data.Aeson (FromJSON (..), ToJSON (..), Value (Null), object, withObject, (.:), (.=)) | ||
| import qualified Data.Aeson as Aeson | ||
| import Data.Aeson.Types (Parser) | ||
| import qualified Data.ByteString.Base16 as BS16 | ||
| import Data.ByteString.Short (ShortByteString, fromShort, toShort) | ||
| import Data.Coerce (coerce) | ||
| import Data.MemPack | ||
| import Data.Text.Encoding (encodeUtf8) | ||
| import qualified Data.Text.Encoding as Text (decodeUtf8) | ||
| import Data.Typeable (Typeable) | ||
| import GHC.Generics (Generic) | ||
| import NoThunks.Class (NoThunks) | ||
|
|
@@ -92,6 +100,12 @@ instance Typeable era => DecCBOR (PlutusData era) where | |
| instance Typeable era => DecCBOR (Annotator (PlutusData era)) where | ||
| decCBOR = pure <$> fromPlainDecoder Cborg.decode | ||
|
|
||
| instance ToJSON (PlutusData era) where | ||
| toJSON (PlutusData d) = plutusDataToJson d | ||
|
|
||
| instance FromJSON (PlutusData era) where | ||
| parseJSON v = PlutusData <$> plutusDataFromJson v | ||
|
|
||
| newtype Data era = MkData (MemoBytes (PlutusData era)) | ||
| deriving (Eq, Generic) | ||
| deriving newtype (SafeToHash, ToCBOR, NFData, DecCBOR) | ||
|
|
@@ -117,6 +131,56 @@ instance HashAnnotated (Data era) EraIndependentData where | |
|
|
||
| instance Typeable era => NoThunks (Data era) | ||
|
|
||
| instance ToJSON (Data era) where | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These instance implementations come from cardano-api analogous
koslambrou marked this conversation as resolved.
|
||
| toJSON = toJSON . getMemoRawType | ||
|
|
||
| instance Era era => FromJSON (Data era) where | ||
| parseJSON v = mkMemoizedEra @era <$> parseJSON v | ||
|
|
||
| plutusDataToJson :: PV1.Data -> Aeson.Value | ||
| plutusDataToJson = \case | ||
| PV1.Constr n fields -> | ||
| object ["constructor" .= n, "fields" .= map plutusDataToJson fields] | ||
| PV1.Map kvs -> | ||
| object | ||
| [ "map" | ||
| .= [ object ["k" .= plutusDataToJson k, "v" .= plutusDataToJson v] | ||
| | (k, v) <- kvs | ||
| ] | ||
| ] | ||
| PV1.List elems -> | ||
| object ["list" .= map plutusDataToJson elems] | ||
| PV1.I n -> | ||
| object ["int" .= n] | ||
| PV1.B bs -> | ||
| object ["bytes" .= Text.decodeUtf8 (BS16.encode bs)] | ||
|
|
||
| plutusDataFromJson :: Aeson.Value -> Parser PV1.Data | ||
| plutusDataFromJson = withObject "Data" $ \o -> | ||
| asum | ||
| [ do | ||
| !n <- o .: "constructor" | ||
| !fields <- o .: "fields" >>= mapM plutusDataFromJson | ||
| pure $ PV1.Constr n fields | ||
| , do | ||
| !kvs <- | ||
| o .: "map" | ||
| >>= mapM | ||
| ( withObject "MapEntry" $ \kv -> do | ||
| !k <- kv .: "k" >>= plutusDataFromJson | ||
| !v <- kv .: "v" >>= plutusDataFromJson | ||
| pure (k, v) | ||
| ) | ||
| pure $ PV1.Map kvs | ||
| , PV1.List <$!> (o .: "list" >>= mapM plutusDataFromJson) | ||
| , PV1.I <$!> o .: "int" | ||
| , PV1.B <$!> (o .: "bytes" >>= parsePlutusByteStringData) | ||
| ] | ||
| where | ||
| parsePlutusByteStringData t = case BS16.decode (encodeUtf8 t) of | ||
| Left e -> fail $ "bytes: invalid hex: " <> e | ||
| Right bs -> pure bs | ||
|
|
||
| pattern Data :: forall era. Era era => PV1.Data -> Data era | ||
| pattern Data p <- (getMemoRawType -> PlutusData p) | ||
| where | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These instance implementations come from cardano-api's analogous datatype
TxMetadata