aeson-t (empty) → 0.0.0
raw patch · 6 files changed
+157/−0 lines, 6 filesdep +aesondep +aeson-qqdep +aeson-tsetup-changed
Dependencies added: aeson, aeson-qq, aeson-t, base, bytestring, hspec2, text, unordered-containers, vector
Files
- Data/Aeson/Transform.hs +86/−0
- Data/Aeson/Transform/Internal.hs +3/−0
- LICENSE +20/−0
- Setup.lhs +3/−0
- aeson-t.cabal +44/−0
- test/Spec.hs +1/−0
+ Data/Aeson/Transform.hs view
@@ -0,0 +1,86 @@+module Data.Aeson.Transform+ (+ -- * Example usage+ -- $use++ -- * Transformation builder+ Builder(..)+ -- * Executing transformation+ , transform+ ) where++import Data.Aeson as A+import Data.Text+import qualified Data.Vector as Vec+import qualified Data.HashMap.Strict as H++-- | Transformations are specified by creating 'Builder' instances.+-- Builders specify how to navigate through input JSON and construct+-- output at various nodes in the tree.+--+data Builder =+ Id+ | At Text Builder+ | Attr Text+ | Attrs [Text]+ | Map Builder+ | Index Int Builder+ | Obj (H.HashMap Text Builder)++-- | Generates new Aeson 'Value' guided by a 'Builder'+--+transform :: Builder -> Value -> Value+transform (At k b) (A.Object o) = transform b $ o H.! k+transform (At _ _) _ = error "Expecting object (At)"+transform (Index i b) (Array a) = transform b $ a Vec.! i+transform (Index _ _) _ = error "Expecting array (Index)"+transform (Map b) (Array a) = Array $ Vec.map (transform b) a+transform (Map _) _ = error "Expecting array (Map)"+transform (Attr k) (A.Object o) = o H.! k+transform (Attr _) _ = error "Expecting object (Attr)"+transform (Attrs ks) (A.Object o) = A.Object $ H.filterWithKey (const . (`elem` ks)) o+transform (Attrs _) _ = error "Expecting object (Attrs)"+transform (Obj fs) x = A.Object $ H.map (`transform` x) fs+transform Id x = x++-- $use+--+-- Filter unwanted attributes from an object+--+-- > Attrs ["nice", "good"]+-- >+-- > -- { bad: 3, good: 1, nice: 500, evil: -3 }+-- > -- => { good: 1, nice: 500 }+--+-- Grab value+--+-- > Attr "foo"+-- >+-- > -- { foo: 2 } => 2+--+-- Dig deeper+--+-- > At "foo" $ Attr "bar"+-- >+-- > -- { foo: { bar: 3 }} => 3+--+-- Map stuff+--+-- > Map $ Attr "foo"+-- >+-- > -- [{ foo:1, foo:2 }] => [1, 2]+--+-- Extract indices+--+-- > Map $ Index 0 Id+-- >+-- > -- [[1,2], [3,4]] => [1, 3]+--+-- Create object+--+-- > Obj $ fromList [+-- > ("first", Index 0 Id)+-- > , ("second", Index 1 Id)+-- > ]+-- >+-- > -- ["hi", "bye"] => { first:"hi", second:"bye" }
+ Data/Aeson/Transform/Internal.hs view
@@ -0,0 +1,3 @@+module Data.Aeson.Transform.Internal+ (+ ) where
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Joe Nelson++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.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ aeson-t.cabal view
@@ -0,0 +1,44 @@+Name: aeson-t+Version: 0.0.0+Author: Joe Nelson <cred+github@begriffs.com>+Maintainer: Joe Nelson <cred+github@begriffs.com>+Category: Data+License: MIT+License-File: LICENSE+Synopsis: Transform JSON+Description:+ Provides a DSL to succinctly transform one JSON document to another.+Cabal-Version: >= 1.10+Build-Type: Simple++Library+ Default-Language: Haskell2010+ GHC-Options: -Wall+ Exposed-Modules: Data.Aeson.Transform+ Other-Modules: Data.Aeson.Transform.Internal+ Build-Depends: base >= 4 && < 5+ , aeson+ , unordered-containers+ , text+ , bytestring+ , vector++Test-Suite spec+ Type: exitcode-stdio-1.0+ Default-Language: Haskell2010+ Hs-Source-Dirs: test+ Ghc-Options: -Wall+ Main-Is: Spec.hs+ Build-Depends: base+ , aeson-t+ , hspec2+ , aeson+ , unordered-containers+ , text+ , bytestring+ , vector+ , aeson-qq++Source-Repository head+ Type: git+ Location: git://github.com:begriffs/aeson-t.git
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}