deriveToJSON: Allow injecting extra fields via Options (for e.g. JSON-LD)
- Dominant language
- Haskell
- Stars
- 1.3k
- Forks
- 336
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 4
Description
JSON-LD specifies a number of [descriptive tags](http://www.w3.org/TR/json-ld/#syntax-tokens-and-keywords) which are intended to be injected as metadata into JSON objects ([nice example here](https://developers.google.com/schemas/formats/json-ld)). A lot of it is stuff there's no reason to store in the local data representation (effectively type info, really). Beyond that, the names of the tags are supposed to start with `@`, which kills clever work-arounds involving `fieldLabelModifier`.
Unless I've missed something (very possible), there's no good way to reflect this in derived `aeson` instances. I believe a simple "good enough" solution would be to expand `Options` with something like `unionObject`:
```
data Foo = Foo { bar :: String }
fooLD = object ["@type" .= ("Foo"::String)]
$(deriveJSON defaultOptions{unionObject = fooLD} ''Foo)
> encode $ Foo "baz"
"{\"@type\":\"Foo\",\"bar\":\"baz\"}"
```
There are some cases like the `@language` tag where this is insufficient, but it seems like an easy and safe (in terms of not getting in the way of better stuff later) solution to a lot of problems.
Contributor guide
Assessment
This issue has not been assessed yet.