Skip to content

PGJSON wrapper? #239

Open
Open
@michalrus

Description

@michalrus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions