AesonBson (empty) → 0.2.0
raw patch · 5 files changed
+138/−0 lines, 5 filesdep +aesondep +attoparsecdep +basesetup-changed
Dependencies added: aeson, attoparsec, base, bson, unordered-containers, vector
Files
- AesonBson.cabal +38/−0
- Data/AesonBson.hs +73/−0
- Data/AesonBson/Instances.hs +17/−0
- LICENSE +7/−0
- Setup.hs +3/−0
+ AesonBson.cabal view
@@ -0,0 +1,38 @@+name: AesonBson+version: 0.2.0+license: OtherLicense+license-file: LICENSE+copyright: CC0+author: Niklas Hambüchen <mail@nh2.me> & Andras Slemmer <0slemi0@gmail.com>+maintainer: Niklas Hambüchen <mail@nh2.me>+category: Data+build-type: Simple+synopsis: Python-to-Haskell function calls+stability: experimental+tested-With: GHC==7.4.2+cabal-version: >= 1.10+homepage: https://github.com/nh2/AesonBson+bug-Reports: https://github.com/nh2/AesonBson/issues+synopsis: Mapping between Aeson's JSON and Bson objects.+description: This package lets you convert between Aeson's JSON and Bson objects.+++source-repository head+ type: git+ location: git://github.com/nh2/AesonBson.git+++library+ exposed-modules:+ Data.AesonBson+ Data.AesonBson.Instances+ build-depends:+ base < 5,+ aeson >= 0.3,+ attoparsec >= 0.10,+ bson >= 0.2,+ unordered-containers >= 0.1.3.0,+ vector >= 0.7.1+ hs-source-dirs: .+ default-language: Haskell2010+ ghc-options: -Wall
+ Data/AesonBson.hs view
@@ -0,0 +1,73 @@+{-# LANGUAGE OverloadedStrings #-}++-- | Convert JSON to BSON and the other way around.+--+-- Note that BSON has more data types than JSON,+-- so some BSON to JSON conversions are not bijective and somewhat arbitrary.+--+-- This means that for some BSON objects:+--+-- >bsonify . aesonify /= id+-- >bsonifyValue . aesonifyValue /= id+--+-- We tried to choose sensible translations on those cases.+module Data.AesonBson (+ aesonify, aesonifyValue,+ bsonify, bsonifyValue+) where++-- TODO Document the arbitrary choices in the Haddock.++import Data.Bson as BSON+import Data.Aeson.Types as AESON+import qualified Data.Attoparsec.Number as Atto+import Data.Monoid+import qualified Data.HashMap.Strict as HashMap (fromList, toList)+import qualified Data.Vector as Vector (fromList, toList)+import Numeric (showHex)+++-- | Converts a JSON value to BSON.+bsonifyValue :: AESON.Value -> BSON.Value+bsonifyValue (Object obj) = Doc $ bsonify 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 { Atto.I int -> Int64 $ fromIntegral int+ ; Atto.D float -> Float float }+bsonifyValue (AESON.Bool b) = BSON.Bool b+bsonifyValue (AESON.Null) = BSON.Null+++-- | Converts a BSON value to JSON.+aesonifyValue :: BSON.Value -> AESON.Value+aesonifyValue (Float f) = toJSON f+aesonifyValue (BSON.String s) = toJSON s+aesonifyValue (Doc doc) = Object $ aesonify doc+aesonifyValue (BSON.Array list) = AESON.Array . Vector.fromList $ map aesonifyValue 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 $ mconcat ["/", pattern, "/", mods]+aesonifyValue (JavaScr (Javascript env code)) = object [ "environment" .= aesonify env+ , "code" .= 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)}+++-- | Converts an AESON object to a BSON document.+bsonify :: AESON.Object -> BSON.Document+bsonify = map (\(t, v) -> t := bsonifyValue v) . HashMap.toList++-- | Converts a BSON document to an AESON object.+aesonify :: BSON.Document -> AESON.Object+aesonify = HashMap.fromList . map (\(l := v) -> (l, aesonifyValue v))
+ Data/AesonBson/Instances.hs view
@@ -0,0 +1,17 @@+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++-- | Provides @ToJSON@ instances for BSON @Value@s and @Document@s.+module Data.AesonBson.Instances where++import Data.Bson as BSON+import Data.Aeson.Types as AESON++import Data.AesonBson+++instance ToJSON BSON.Value where+ toJSON = aesonifyValue++instance ToJSON BSON.Document where+ toJSON = Object . aesonify
+ 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/>.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain