deriveJsonNoPrefix: Derive ToJSON/FromJSON instances in a more prefix-friendly manner.

[ apache, data, library ] [ Propose Tags ] [ Report a vulnerability ]

Modules

[Index]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1
Change log ChangeLog.md
Dependencies aeson, base (>=4.7 && <5), template-haskell [details]
License Apache-2.0
Copyright 2018 Yuji Yamamoto
Author Yuji Yamamoto
Maintainer whosekiteneverfly@gmail.com
Revised Revision 1 made by Bodigrim at 2024-05-06T14:27:21Z
Category Data
Home page https://gitlab.com/igrep/deriveJsonNoPrefix
Source repo head: git clone https://gitlab.com/igrep/deriveJsonNoPrefix
Uploaded by igrep at 2018-07-17T08:29:35Z
Distributions LTSHaskell:0.1.0.1, Stackage:0.1.0.1
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1771 total (20 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-07-17 [all 1 reports]

Readme for deriveJsonNoPrefix-0.1.0.1

[back to package description]

deriveJsonNoPrefix

Template Haskell macros to derive ToJSON/FromJSON instances in a more prefix-friendly manner.

Example

Suppose you want to create a JSON like this:

{
    "id": "ID STRING",
    "max": 0.789,
    "min": 0.123
}

You'd want to define a record type to derive the instance of ToJSON (and possibly FromJSON) automatically:

{-# LANGUAGE TemplateHaskell #-}

import Data.Aeson.TH

data SomeRecord = SomeRecord
  { id :: String
  , max :: Double
  , min :: Double
  } deriving (Eq, Show)

$(deriveToJSON ''SomeRecord)

But you shouldn't define such a record because both id, max, and min are predefined functions of Prelude!!

As a workaround, we frequently prefix the record labels with their type name:

data SomeRecord = SomeRecord
  { someRecordId :: String
  , someRecordMax :: Double
  , someRecordMin :: Double
  } deriving (Eq, Show)

Then deriveToJSON with a modified option:

deriveToJSON Json.defaultOptions { fieldLabelModifier = firstLower . drop (length "SomeRecord") } ''SomeRecord

That's almost exactly what deriveToJsonNoTypeNamePrefix does!!
deriveToJsonNoTypeNamePrefix is essentially defined as:

deriveToJsonNoTypeNamePrefix :: Name -> Q [Dec]
deriveToJsonNoTypeNamePrefix deriver name =
  deriveToJSON Json.defaultOptions { fieldLabelModifier = dropPrefix name } name

So now, you don't have reimplement the fieldLabelModifier anymore!

import Data.Aeson.DeriveNoPrefix

$(deriveJsonNoTypeNamePrefix ''SomeRecord)

Other libraries which would solve the same problem

  • extensible.
  • And other libraries providing extensible records with ToJSON / FromJSON instances.

So use this package all of them are too heavy in learning cost / dependency footprint / etc.