packages feed

aeson-bson (empty) → 0.2.0

raw patch · 6 files changed

+159/−0 lines, 6 filesdep +HUnitdep +aesondep +arraysetup-changed

Dependencies added: HUnit, aeson, array, attoparsec, base, bson, bytestring, containers, test-framework, test-framework-hunit, text, unordered-containers, vector

Files

+ LICENSE view
@@ -0,0 +1,7 @@+AesonBson - Aeson to Bson and inverse bridge++Written in 2011 by Andras Slemmer <0slemi0@gmail.com> and Niklas Hambuechen <mail@nh2.me>++To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.++You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
+ README.md view
@@ -0,0 +1,5 @@+This package is a spin-off of the original package "AesonBson":++http://hackage.haskell.org/package/AesonBson-0.2.0++All the credits to the original authors for their job.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ aeson-bson.cabal view
@@ -0,0 +1,56 @@+Name: aeson-bson+Version: 0.2.0+License: OtherLicense+License-File: LICENSE+Maintainer: Niklas Hambuechen <mail@nh2.me>+Author: Niklas Hambuechen <mail@nh2.me> & Andras Slemmer <0slemi0@gmail.com>+Build-Type: Simple+Stability: experimental+Synopsis: Mapping between Aeson's JSON and Bson objects.+Description: This package lets you convert between Aeson's JSON and Bson objects.+Category: Data+Copyright: CC0+Cabal-Version: >= 1.8+Homepage:+Tested-With: GHC == 7.6.2++extra-source-files:+  README.md,+  test/Main.hs++source-repository head+  type:      git+  location:  git://github.com/adinapoli/aeson-bson.git++Library+  hs-source-dirs: src++  if impl(ghc >= 7.6.1)+    CPP-Options: -DNO_PRELUDE_CATCH++  Build-Depends:+    array -any,+    aeson >= 0.6 && < 0.8,+    attoparsec -any,+    base >= 4 && < 5,+    bson >= 0.2.1 && < 3,+    bytestring >= 0.10 && < 0.13,+    containers -any,+    text >= 0.11.1.0,+    unordered-containers >= 0.1.3.0,+    vector -any+  Exposed-Modules:+    Data.AesonBson+  Ghc-Options: -Wall -fno-warn-orphans++Test-Suite test-all++  type:  exitcode-stdio-1.0+  +  main-is:  test/Main.hs+  +  build-depends:+    base < 5,+    HUnit >= 1.2.5.1,+    test-framework >= 0.8,+    test-framework-hunit >= 0.3.0
+ src/Data/AesonBson.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}++module Data.AesonBson (+  toAeson, aesonifyValue,+  toBson, bsonifyValue+) where++import Data.Bson as BSON+import Data.Aeson.Types as AESON+import Data.Attoparsec.Number as Atto+import Data.Text as T hiding (map)+import Data.HashMap.Strict as Map (fromList, toList)+import Data.Vector as Vector (toList)+import Numeric++instance ToJSON BSON.Value where+  toJSON = aesonifyValue++instance ToJSON Document where+  toJSON = Object . toAeson++bsonifyValue :: AESON.Value -> BSON.Value+bsonifyValue (Object obj) = Doc $ toBson obj+bsonifyValue (AESON.Array array) = BSON.Array . map bsonifyValue . Vector.toList $ array+bsonifyValue (AESON.String str) = BSON.String str+bsonifyValue (Number n) = case n of { I int   -> Int64 $ fromIntegral int+                                    ; D float -> Float float }+bsonifyValue (AESON.Bool b) = BSON.Bool b+bsonifyValue (AESON.Null) = BSON.Null++aesonifyValue :: BSON.Value -> AESON.Value+aesonifyValue (Float f) = toJSON f+aesonifyValue (BSON.String s) = toJSON s+aesonifyValue (Doc doc) = toJSON doc+aesonifyValue (BSON.Array list) = toJSON list+aesonifyValue (Bin (Binary binary)) = toJSON binary+aesonifyValue (Fun (Function function)) = toJSON function+aesonifyValue (Uuid (UUID uuid)) = toJSON uuid+aesonifyValue (Md5 (MD5 md5)) = toJSON md5+aesonifyValue (UserDef (UserDefined userdef)) = toJSON userdef+aesonifyValue (ObjId (Oid w32 w64)) = toJSON $ showHex w32 (showHex w64 "")+aesonifyValue (BSON.Bool bool) = toJSON bool+aesonifyValue (UTC utc) = toJSON utc+aesonifyValue (BSON.Null) = AESON.Null+aesonifyValue (RegEx (Regex pattern mods)) = toJSON $+                                           '/' : T.unpack pattern +++                                           '/' : T.unpack mods+aesonifyValue (JavaScr (Javascript env code)) = toJSON . Map.fromList $+                                              [ (T.pack "environment", toJSON env)+                                              , (T.pack "code", toJSON code)]+aesonifyValue (Sym (Symbol sym)) = toJSON sym+aesonifyValue (Int32 int32) = toJSON int32+aesonifyValue (Int64 int64) = toJSON int64+aesonifyValue (Stamp (MongoStamp int64)) = toJSON int64+aesonifyValue (MinMax mm) = case mm of { MinKey -> toJSON (-1 :: Int)+                                       ; MaxKey -> toJSON (1 :: Int)}+++toBson :: AESON.Object -> BSON.Document+toBson = map (\(t, v) -> (t := bsonifyValue v)) . Map.toList++toAeson :: BSON.Document -> AESON.Object+toAeson = Map.fromList . map (\(l := v) -> (l, aesonifyValue v))
+ test/Main.hs view
@@ -0,0 +1,24 @@++module Main where++import Test.Framework+import Test.Framework.Providers.HUnit++import Test.HUnit hiding (Test)++import Data.List+++main :: IO ()+main = defaultMain tests++tests :: [Test]+tests = [+        testGroup "Sorting Group 2" [+                testCase "sort7" test_sort7+            ]+    ]+++test_sort7 :: Assertion+test_sort7 = sort [8, 7, 2, 5, 4, 9, 6, 1, 0, 3] @?= [0..9]