haskell / haskell/aeson

Add `withTypeableObject` that extracts name of the object from the type

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

Description

Usually when I have custom data type like this one:

```haskell
data Foo = Foo
{ fooBar :: Int
, fooBaz :: Double
}
```

I write `FromJSON` instance like this:

```haskell
instance FromJSON Foo where
parseJSON = withObject "Foo" $ \o -> ...
```

I need to specify name of the type to get good error messages. But if type name changes, I need to not forget to update this function. Also, it's not really convenient to write name of type each type. I propose to add the following function to the `aeson` library:

```haskell
typeName :: forall a . Typeable a => Text
typeName = show $ typeRep $ Proxy @a

withTypeableObject :: forall a . Typeable a => (Object -> Parser a) -> Value -> Parser a
withTypeableObject = withObject (typeName @a)
```

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.