vinyl-json (empty) → 0.1.0.0
raw patch · 6 files changed
+174/−0 lines, 6 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, hlint, hscolour, template-haskell, text, vinyl
Files
- Data/Vinyl/JSON.hs +46/−0
- Data/Vinyl/JSON/Examples.hs +41/−0
- LICENSE +22/−0
- Setup.hs +2/−0
- test-suite/HLint.hs +16/−0
- vinyl-json.cabal +47/−0
+ Data/Vinyl/JSON.hs view
@@ -0,0 +1,46 @@+{-# LANGUAGE DataKinds+ , TypeOperators+ , OverloadedStrings+ , FlexibleInstances+ , ScopedTypeVariables+ , KindSignatures+ , FlexibleContexts+ #-}+{-| This adds and instance of FromJSON to SimpleRecords+ -+ -}+module Data.Vinyl.JSON where++import Control.Applicative+import Control.Monad++import Data.Vinyl++-- import Data.ByteString as L+import Data.Aeson++import Data.Text as T++import GHC.TypeLits+-- import Data.Proxy++instance FromJSON (PlainRec '[]) where+ parseJSON (Object v) = pure RNil+ parseJSON _ = mzero++instance (KnownSymbol sym, FromJSON a, FromJSON (PlainRec fields)) =>+ FromJSON (PlainRec ((sym ::: a) ': (fields :: [*]))) where+ parseJSON (Object v) = ((<+>) :: PlainRec '[sym ::: a] + -> PlainRec fields + -> PlainRec ((sym ::: a) ': fields))+ <$> ((field =:) <$> (v .: json_name))+ <*> rest_rec+ where field = Field :: (sym ::: a)+ json_name = T.pack $ show field+ rest_rec = parseJSON (Object v)+ -- I had to type (<+>) because I can't say the type of rest_rec,+ -- as it uses an internal type+ -- rest_rec :: Data.Aeson.Types.Internal.Parser (PlainRec (fields))+ -- but I needed to type rest_rec's result record to have it parse++
+ Data/Vinyl/JSON/Examples.hs view
@@ -0,0 +1,41 @@+{-# LANGUAGE DataKinds+ , TypeOperators+ , OverloadedStrings+ , FlexibleInstances+ , ScopedTypeVariables+ , KindSignatures+ , FlexibleContexts+ #-}++module Data.Vinyl.JSON.Examples where++import Control.Applicative+import Control.Monad++import Data.Vinyl++-- import Data.ByteString as L+import Data.Aeson++import Data.Text as T++import GHC.TypeLits++import Data.Vinyl.JSON++-- Examples of use+--{-+example_json = "{\"name\": \"jon\", \"job\":\"Code\", \"age\":42, \"things\":[1,2,3] }"++nested_json = "{\"dad\" : { \"name\" : \"bob\" } }"++parsed_example :: Maybe (PlainRec '[("name" ::: Text),+ ("job" ::: Text),+ ("age" ::: Int),+ ("things" ::: [Int])])+parsed_example = decode example_json++parsed_nested :: Maybe (PlainRec '[("dad" ::: + (PlainRec '[("name" ::: Text)]))])+parsed_nested = decode nested_json+--}
+ LICENSE view
@@ -0,0 +1,22 @@++The MIT License (MIT)++Copyright (c) 2014 Theo Belaire++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ test-suite/HLint.hs view
@@ -0,0 +1,16 @@+module Main (main) where++import Language.Haskell.HLint (hlint)+import System.Exit (exitFailure, exitSuccess)++arguments :: [String]+arguments =+ [+ , "library"+ , "test-suite"+ ]++main :: IO ()+main = do+ hints <- hlint arguments+ if null hints then exitSuccess else exitFailure
+ vinyl-json.cabal view
@@ -0,0 +1,47 @@+-- Initial vinyl-json.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: vinyl-json+version: 0.1.0.0+synopsis: Provide json instances automagically to vinyl types+description: This adds Data.Aeson FromJSON instances for Simple records+license: MIT+license-file: LICENSE+author: Theo Belaire+maintainer: theo.belaire@gmail.com+-- copyright: +category: Web+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/tbelaire/vinyl-json.git++flag documentation+ default: False++library+ if flag(documentation)+ build-depends: hscolour == 1.20.*++ exposed-modules: Data.Vinyl.JSON+ Data.Vinyl.JSON.Examples+ -- other-modules: + -- other-extensions: + build-depends: base >= 4.7 && < 5,+ aeson,+ text,+ template-haskell == 2.9.*,+ vinyl,+ bytestring+ hs-source-dirs: .+ default-language: Haskell2010++test-suite hlint+ build-depends: base, hlint == 1.8.*+ default-language: Haskell2010+ hs-source-dirs: test-suite+ main-is: HLint.hs+ type: exitcode-stdio-1.0