haskell / haskell/aeson

Add a data type and `FromJSON` instance that retains the original JSON

Open
#747 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
1.3k
Forks
336
Avg merge
3d 7h
Merged PRs (30d)
4

Description

With many APIs the Haskell types and the real world get out of sync. Updating the types is time consuming and often requires human interaction (because of separate packages, maintainers). For example, the GitHub API has lots of nooks that aren't reflected in the quite-rich (and appreciated) github package. For these cases I find it helpful to have access to the raw JSON both for debugging and for supporting functionality until the next release.

To restate, having a type decoded from an Aeson Value does not negate the utility of having the original Aeson Value around. This is a common situation and to support it I propose we add a type and `FromJSON` instance of:

```
module M (RetainedJSON, retainedJSON, decodedJSON) where
data RetainedJSON a = RetainedJSON
{ retainedJSON :: Value
, decodedJSON :: a
}

instance FromJSON a => FromJSON (RetainedJSON a) where
parseJSON x = RetainedJSON x <$> parseJSON x
```

And either omit the `ToJSON` instance or use the admittedly-controversial instance of:

```
instance ToJSON (RetainedJSON a) where
toJSON = pure . retainedJSON
```

I'm checking for strong opinions against this proposal before I make a PR. Anyone have thoughts?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.