packages feed

dependent-sum-aeson-orphans (empty) → 0.1.0.0

raw patch · 5 files changed

+92/−0 lines, 5 filesdep +aesondep +basedep +constraintssetup-changed

Dependencies added: aeson, base, constraints, constraints-extras, dependent-map, dependent-sum

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for dependent-sum-aeson-orphans++## 0.1.0.0  -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Obsidian Systems++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Obsidian Systems nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ dependent-sum-aeson-orphans.cabal view
@@ -0,0 +1,22 @@+name: dependent-sum-aeson-orphans+version: 0.1.0.0+license: BSD3+license-file: LICENSE+author: Obsidian Systems+maintainer: maintainer@obsidian.systems+copyright: 2018 Obsidian Systems LLC+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10+description: JSON instances for DSum and DMap++library+  exposed-modules: Data.Dependent.Sum.Orphans+  build-depends: base >=4.9 && <4.11+               , aeson >= 1.2 && < 1.3+               , dependent-map+               , dependent-sum+               , constraints+               , constraints-extras+  hs-source-dirs: src+  default-language: Haskell2010
+ src/Data/Dependent/Sum/Orphans.hs view
@@ -0,0 +1,33 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeOperators #-}+module Data.Dependent.Sum.Orphans where++import Data.Aeson+import Data.Constraint+import Data.Constraint.Forall+import Data.Constraint.Extras+import Data.Dependent.Map (DMap, GCompare)+import qualified Data.Dependent.Map as DMap+import Data.Dependent.Sum+import Data.Some (Some)+import qualified Data.Some as Some++instance (ForallF ToJSON f, Has ToJSON f, ToJSON1 g) => ToJSON (DSum f g) where+  toJSON ((f :: f a) :=> (x :: g a))+    | Dict :: Dict (ToJSON a) <- argDict f+    = toJSON (toJSON f \\ (instF :: ForallF ToJSON f :- ToJSON (f a)), toJSON1 x)++instance (ForallF ToJSON f, Has ToJSON f, ToJSON1 g) => ToJSON (DMap f g) where+    toJSON = toJSON . DMap.toList++instance (FromJSON (Some f), GCompare f, Has FromJSON f, FromJSON1 g) => FromJSON (DSum f g) where+  parseJSON x = do+    (tag, val) <- parseJSON x+    Some.This (parsedTag :: f a) <- parseJSON tag+    val' <- case argDict parsedTag of+      (Dict :: Dict (FromJSON a)) -> parseJSON1 val+    return $ parsedTag :=> val'++instance (FromJSON (Some f), GCompare f, Has FromJSON f, FromJSON1 g) => FromJSON (DMap f g) where+    parseJSON = fmap DMap.fromList . parseJSON