Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGJSON wrapper? #239

Open
michalrus opened this issue Dec 11, 2017 · 1 comment
Open

PGJSON wrapper? #239

michalrus opened this issue Dec 11, 2017 · 1 comment

Comments

@michalrus
Copy link

michalrus commented Dec 11, 2017

Wouldn’t that be useful to have in core?

-- |Instances to simplify (de-)serializing JSONB from/to DB.
newtype PGJSON a = PGJSON a deriving (Eq, Show)

instance (FromJSON a, Typeable a) => FromField (PGJSON a) where
  fromField a b = PGJSON <$> fromJSONField a b

instance (ToJSON a) => ToField (PGJSON a) where
  toField (PGJSON a) = toJSONField a

Then one can simply use them ad hoc:

do
  [Only (PGJSON xs)] <- query_ c "select jsonb_build_object('key', 'value')"
  assert (xs == Map.fromList [("key", "value")])

… and similarly, without manually defining To-/FromField instances for all used types.

@michalrus
Copy link
Author

(A bit similar to how you can use the PGArray wrapper.)

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

No branches or pull requests

1 participant