Skip to content

refactor(api): migrate ToJSON instances to use experimental Era type - #1038

Merged
newhoggy merged 1 commit into
masterfrom
newhoggy/modify-some-ToJSON-instances-to-use-IsEra
Nov 26, 2025
Merged

newhoggy merged 1 commit into
masterfrom
newhoggy/modify-some-ToJSON-instances-to-use-IsEra

Conversation

@newhoggy

@newhoggy newhoggy commented Nov 25, 2025 •

Copy link
Copy Markdown
Contributor

Changelog

- description: |
    Migrates ToJSON instances for TxOut and UTxO types to use the new experimental Era framework,
    simplifying era handling and reducing pattern matching complexity in JSON serialization.
  type:
  - refactoring    # QoL changes
  projects:
  - cardano-api

Context

This PR refactors the JSON serialization logic for TxOut and UTxO types to adopt the experimental Era framework (Cardano.Api.Experimental.Era). The change replaces the IsCardanoEra constraint with Exp.IsEra, providing a cleaner and more maintainable approach to era-specific serialization.

The refactoring significantly reduces the complexity of txOutToJsonValue by eliminating redundant pattern matches for historical eras (Byron through Babbage) that share identical serialization logic, keeping only the distinct implementations for Conway and Dijkstra eras.

How to trust this PR

The changes are purely refactoring with no functional changes to the JSON output format. Key areas to review:

  • Constraint changes: IsCardanoEra era => ToJSON becomes Exp.IsEra era => ToJSON in both TxOut and UTxO modules
  • Pattern simplification: The txOutToJsonValue function now only handles Exp.ConwayEra and Exp.DijkstraEra, removing 6 redundant case branches
  • Test updates: Tests have been updated to use ShelleyBasedEraConway instead of ShelleyBasedEraBabbage for better coverage

To verify the changes preserve existing behavior:

# Run the JSON roundtrip tests
cabal test cardano-api-test --test-options "-p Json"

# Verify all tests pass
cabal test all

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

prop_json_roundtrip_utxo :: Property
prop_json_roundtrip_utxo = H.property $ do
utxo <- forAll $ genUTxO ShelleyBasedEraBabbage
utxo <- forAll $ genUTxO ShelleyBasedEraConway

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The era should be defined and reused at the top level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Replaces IsCardanoEra constraint with Exp.IsEra in ToJSON instances
for TxOut and UTxO types, adopting the new experimental Era framework
for JSON serialization. This change simplifies the era handling by
using Exp.useEra instead of cardanoEra and reduces the pattern matching
cases in txOutToJsonValue to only handle ConwayEra and DijkstraEra.

- Update TxOut ToJSON instance to use Exp.IsEra constraint
- Update UTxO ToJSON instance to use Exp.IsEra constraint
- Refactor txOutToJsonValue to use Exp.Era type parameter
- Remove redundant pattern matches for Byron through Babbage eras
- Update test generators to use ShelleyBasedEraConway instead of Babbage

The test suite changes ensure compatibility with the new era handling
approach by testing against the Conway era, which provides better
coverage of the current serialization logic.
@newhoggy
newhoggy force-pushed the newhoggy/modify-some-ToJSON-instances-to-use-IsEra branch from 6708242 to 21408c8 Compare November 26, 2025 11:16
@newhoggy
newhoggy enabled auto-merge November 26, 2025 11:16
@newhoggy
newhoggy added this pull request to the merge queue Nov 26, 2025
Merged via the queue into master with commit 7beb3c4 Nov 26, 2025
@newhoggy
newhoggy deleted the newhoggy/modify-some-ToJSON-instances-to-use-IsEra branch November 26, 2025 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants