{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.NonNull.Instances
  (
  ) where

import ClassyPrelude

import Data.Aeson

import Data.Binary (Binary)
import qualified Data.Binary as Binary


instance ToJSON a => ToJSON (NonNull a) where
  toJSON = toJSON . toNullable

instance (FromJSON a, MonoFoldable a) => FromJSON (NonNull a) where
  parseJSON = parseJSON >=> maybe (fail "Expected non-empty structure") return . fromNullable


instance Hashable a => Hashable (NonNull a) where
  hashWithSalt s = hashWithSalt s . toNullable


instance (Binary a, MonoFoldable a) => Binary (NonNull a) where
  get = Binary.get >>= maybe (fail "Expected non-empty structure") return . fromNullable
  put = Binary.put . toNullable
