bson (empty) → 0.0.1
raw patch · 6 files changed
+883/−0 lines, 6 filesdep +basedep +binarydep +bytestringsetup-changed
Dependencies added: base, binary, bytestring, compact-string-fix, data-binary-ieee754, mtl, nano-md5, network, time, unix
Files
- Data/Bson.hs +404/−0
- Data/Bson/Binary.hs +225/−0
- Data/UString.hs +33/−0
- LICENSE +202/−0
- Setup.lhs +4/−0
- bson.cabal +15/−0
+ Data/Bson.hs view
@@ -0,0 +1,404 @@+{- | A BSON document is a JSON-like object with a standard binary encoding defined at bsonspec.org. This implements version 1.0 of that spec.++Use the GHC language extension /OverloadedStrings/ to automatically convert String literals to UString (UTF8) -}++{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances, DeriveDataTypeable, RankNTypes, OverlappingInstances, IncoherentInstances, ScopedTypeVariables #-}++module Data.Bson (+ UString,+ -- * Document+ Document, look, lookup, valueAt, at, include, exclude, merge,+ -- ** Element+ Element(..), (=:), (=?),+ Label,+ -- * Value+ Value(..), Val(..), fval, cast, typed,+ -- * Special Bson types+ Binary(..), Function(..), UUID(..), MD5(..), UserDefined(..),+ Regex(..), Javascript(..), Symbol(..), MongoStamp(..), MinMaxKey(..),+ -- ** ObjectId+ ObjectId(..), timestamp, genObjectId+) where++import Prelude hiding (lookup)+import Control.Applicative ((<$>), (<*>))+import Data.Typeable hiding (cast)+import Data.Int+import Data.Word+import Data.UString (UString, u, unpack)+import Data.Time.Clock (UTCTime)+import Data.Time.Clock.POSIX+import Data.Time.Format () -- for Show and Read instances of UTCTime+import Data.List (find, findIndex)+import Data.Bits (shift, (.|.))+import Data.ByteString.Char8 (ByteString, pack)+import Data.Digest.OpenSSL.MD5 (md5sum)+import Numeric (readHex, showHex)+import Network.BSD (getHostName)+import System.Posix.Process (getProcessID)+import System.IO.Unsafe (unsafePerformIO)+import Data.IORef+import Data.Maybe (maybeToList, mapMaybe)+import Control.Monad.Identity++roundTo :: (RealFrac a) => a -> a -> a+-- ^ Round second number to nearest multiple of first number. Eg: roundTo (1/1000) 0.12345 = 0.123+roundTo mult n = fromIntegral (round (n / mult)) * mult++-- * Document++type Document = [Element]+-- ^ A BSON document is a sequence of 'Element's++look :: (Monad m) => Label -> Document -> m Value+-- ^ Value of field in document, or fail (Nothing) if field not found+look k doc = maybe notFound (return . value) (find ((k ==) . label) doc) where+ notFound = fail $ "expected " ++ show k ++ " in " ++ show doc++lookup :: (Val v, Monad m) => Label -> Document -> m v+-- ^ Lookup field in document and cast to expected type. Fail (Nothing) if field not found or value not of expected type.+lookup k doc = cast =<< look k doc++valueAt :: Label -> Document -> Value+-- ^ Value of field in document. Error if missing.+valueAt k = runIdentity . look k++at :: forall v. (Val v) => Label -> Document -> v+-- ^ Typed value of field in document. Error if missing or wrong type.+at k doc = maybe err id (lookup k doc) where+ err = error $ "expected (" ++ show k ++ " :: " ++ show (typeOf (undefined :: v)) ++ ") in " ++ show doc++include :: [Label] -> Document -> Document+-- ^ Only include elements of document in key list+include keys doc = mapMaybe (\k -> find ((k ==) . label) doc) keys++exclude :: [Label] -> Document -> Document+-- ^ Exclude elements from document in key list+exclude keys doc = filter (\(k := _) -> notElem k keys) doc++merge :: Document -> Document -> Document+-- ^ Merge documents with preference given to first one when both have the same label. I.e. for every (k := v) in first argument, if k exists in second argument then replace its value with v, otherwise add (k := v) to second argument+merge es doc = foldl f doc es where+ f doc (k := v) = case findIndex ((k ==) . label) doc of+ Nothing -> doc ++ [k := v]+ Just i -> let (x, _ : y) = splitAt i doc in x ++ [k := v] ++ y++-- * Element++infix 0 :=, =:, =?++data Element = (:=) {label :: Label, value :: Value} deriving (Typeable, Eq)+-- ^ A BSON element is a named value, where the name is a string and the value is a BSON 'Value'++(=:) :: (Val v) => Label -> v -> Element+-- ^ Element with given label and typed value+k =: v = k := val v++(=?) :: (Val a) => Label -> Maybe a -> [Element]+-- ^ If Just then return one element list with given label, otherwise return empty list+k =? ma = maybeToList (fmap (k =:) ma)++instance Show Element where+ showsPrec d (k := v) = showParen (d > 0) $ showString (' ' : unpack k) . showString ": " . showsPrec 1 v++type Label = UString+-- ^ The name of a BSON element/field++-- * Value++-- | A BSON value is one of the following types of values+data Value =+ Float Double |+ String UString |+ Doc Document |+ Array [Value] |+ Bin Binary |+ Fun Function |+ Uuid UUID |+ Md5 MD5 |+ UserDef UserDefined |+ ObjId ObjectId |+ Bool Bool |+ UTC UTCTime |+ Null |+ RegEx Regex |+ JavaScr Javascript |+ Sym Symbol |+ Int32 Int32 |+ Int64 Int64 |+ Stamp MongoStamp |+ MinMax MinMaxKey+ deriving (Typeable, Eq)++instance Show Value where+ showsPrec d v = fval (showsPrec d) v++fval :: (forall a . (Val a) => a -> b) -> Value -> b+-- ^ Apply generic function to typed value+fval f v = case v of+ Float x -> f x+ String x -> f x+ Doc x -> f x+ Array x -> f x+ Bin x -> f x+ Fun x -> f x+ Uuid x -> f x+ Md5 x -> f x+ UserDef x -> f x+ ObjId x -> f x+ Bool x -> f x+ UTC x -> f x+ Null -> f ()+ RegEx x -> f x+ JavaScr x -> f x+ Sym x -> f x+ Int32 x -> f x+ Int64 x -> f x+ Stamp x -> f x+ MinMax x -> f x++-- * Value conversion++cast :: forall m a. (Val a, Monad m) => Value -> m a+-- ^ Convert Value to expected type, or fail (Nothing) if not of that type+cast v = maybe notType return (cast' v) where+ notType = fail $ "expected " ++ show (typeOf (undefined :: a)) ++ ": " ++ show v++typed :: (Val a) => Value -> a+-- ^ Convert Value to expected type. Error if not that type.+typed = runIdentity . cast++-- ** conversion class++-- | Haskell types of this class correspond to BSON value types+class (Typeable a, Show a, Eq a) => Val a where+ val :: a -> Value+ cast' :: Value -> Maybe a++instance Val Double where+ val = Float+ cast' (Float x) = Just x+ cast' (Int32 x) = Just (fromIntegral x)+ cast' (Int64 x) = Just (fromIntegral x)+ cast' _ = Nothing++instance Val Float where+ val = Float . realToFrac+ cast' (Float x) = Just (realToFrac x)+ cast' (Int32 x) = Just (fromIntegral x)+ cast' (Int64 x) = Just (fromIntegral x)+ cast' _ = Nothing++instance Val UString where+ val = String+ cast' (String x) = Just x+ cast' (Sym (Symbol x)) = Just x+ cast' _ = Nothing++instance Val String where+ val = String . u+ cast' (String x) = Just (unpack x)+ cast' (Sym (Symbol x)) = Just (unpack x)+ cast' _ = Nothing++instance Val Document where+ val = Doc+ cast' (Doc x) = Just x+ cast' _ = Nothing++instance Val [Value] where+ val = Array+ cast' (Array x) = Just x+ cast' _ = Nothing++instance (Val a) => Val [a] where+ val = Array . map val+ cast' (Array x) = mapM cast x+ cast' _ = Nothing++instance Val Binary where+ val = Bin+ cast' (Bin x) = Just x+ cast' _ = Nothing++instance Val Function where+ val = Fun+ cast' (Fun x) = Just x+ cast' _ = Nothing++instance Val UUID where+ val = Uuid+ cast' (Uuid x) = Just x+ cast' _ = Nothing++instance Val MD5 where+ val = Md5+ cast' (Md5 x) = Just x+ cast' _ = Nothing++instance Val UserDefined where+ val = UserDef+ cast' (UserDef x) = Just x+ cast' _ = Nothing++instance Val ObjectId where+ val = ObjId+ cast' (ObjId x) = Just x+ cast' _ = Nothing++instance Val Bool where+ val = Bool+ cast' (Bool x) = Just x+ cast' _ = Nothing++instance Val UTCTime where+ val = UTC . posixSecondsToUTCTime . roundTo (1/1000) . utcTimeToPOSIXSeconds+ cast' (UTC x) = Just x+ cast' _ = Nothing++instance Val POSIXTime where+ val = UTC . posixSecondsToUTCTime . roundTo (1/1000)+ cast' (UTC x) = Just (utcTimeToPOSIXSeconds x)+ cast' _ = Nothing++instance Val () where+ val () = Null+ cast' Null = Just ()+ cast' _ = Nothing++instance Val Regex where+ val = RegEx+ cast' (RegEx x) = Just x+ cast' _ = Nothing++instance Val Javascript where+ val = JavaScr+ cast' (JavaScr x) = Just x+ cast' _ = Nothing++instance Val Symbol where+ val = Sym+ cast' (Sym x) = Just x+ cast' (String x) = Just (Symbol x)+ cast' _ = Nothing++instance Val Int32 where+ val = Int32+ cast' (Int32 x) = Just x+ cast' (Int64 x) = mInt32 x+ cast' (Float x) = Just (round x)+ cast' _ = Nothing++instance Val Int64 where+ val = Int64+ cast' (Int64 x) = Just x+ cast' (Int32 x) = Just (fromIntegral x)+ cast' (Float x) = Just (round x)+ cast' _ = Nothing++instance Val Int where+ val n = maybe (Int64 $ fromIntegral n) Int32 (mInt32 n)+ cast' (Int32 x) = Just (fromIntegral x)+ cast' (Int64 x) = Just (fromEnum x)+ cast' (Float x) = Just (round x)+ cast' _ = Nothing++instance Val Integer where+ val n = maybe (Int64 . toEnum . fromEnum $ n) Int32 (mInt32 n)+ cast' (Int32 x) = Just (fromIntegral x)+ cast' (Int64 x) = Just (fromIntegral x)+ cast' (Float x) = Just (round x)+ cast' _ = Nothing++instance Val MongoStamp where+ val = Stamp+ cast' (Stamp x) = Just x+ cast' _ = Nothing++instance Val MinMaxKey where+ val = MinMax+ cast' (MinMax x) = Just x+ cast' _ = Nothing++mInt32 :: (Integral n) => n -> Maybe Int32+-- ^ If number fits in 32 bits then cast to Int32, otherwise Nothing+mInt32 n = if fromIntegral (minBound :: Int32) <= n && n <= fromIntegral (maxBound :: Int32)+ then Just (fromIntegral n)+ else Nothing++-- * Haskell types corresponding to special Bson value types++-- ** Binary types++newtype Binary = Binary ByteString deriving (Typeable, Show, Read, Eq)++newtype Function = Function ByteString deriving (Typeable, Show, Read, Eq)++newtype UUID = UUID ByteString deriving (Typeable, Show, Read, Eq)++newtype MD5 = MD5 ByteString deriving (Typeable, Show, Read, Eq)++newtype UserDefined = UserDefined ByteString deriving (Typeable, Show, Read, Eq)++-- ** Regex++data Regex = Regex UString UString deriving (Typeable, Show, Read, Eq)++-- ** Javascript++data Javascript = Javascript Document UString deriving (Typeable, Show, Eq)+-- ^ Javascript code with possibly empty environment mapping variables to values that the code may reference++-- ** Symbol++newtype Symbol = Symbol UString deriving (Typeable, Show, Read, Eq)++-- ** MongoStamp++newtype MongoStamp = MongoStamp Int64 deriving (Typeable, Show, Read, Eq)++-- ** MinMax++data MinMaxKey = MinKey | MaxKey deriving (Typeable, Show, Read, Eq)++-- ** ObjectId++data ObjectId = Oid Word32 Word64 deriving (Typeable, Eq, Ord)+-- ^ A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order.++instance Show ObjectId where+ showsPrec d (Oid x y) = showParen (d > 10) $ showString "Oid " . showHex x . showChar ' ' . showHex y++timestamp :: ObjectId -> UTCTime+-- ^ Time when objectId was created+timestamp (Oid time _) = posixSecondsToUTCTime (fromIntegral time)++genObjectId :: IO ObjectId+-- ^ Create a fresh ObjectId+genObjectId = do+ time <- truncate <$> getPOSIXTime+ pid <- fromIntegral <$> getProcessID+ inc <- nextCount+ return $ Oid time (composite machineId pid inc)+ where+ machineId :: Word24+ machineId = unsafePerformIO (fst . head . readHex . take 6 . md5sum . pack <$> getHostName)+ {-# NOINLINE machineId #-}+ counter :: IORef Word24+ counter = unsafePerformIO (newIORef 0)+ {-# NOINLINE counter #-}+ nextCount :: IO Word24+ nextCount = atomicModifyIORef counter $ \n -> (wrap24 (n + 1), n)+ composite :: Word24 -> Word16 -> Word24 -> Word64+ composite mid pid inc = fromIntegral mid `shift` 40 .|. fromIntegral pid `shift` 24 .|. fromIntegral inc++type Word24 = Word32+-- ^ low 3 bytes only, high byte must be zero++wrap24 :: Word24 -> Word24+wrap24 n = n `mod` 0x1000000+++{- Authors: Tony Hannan <tony@10gen.com>+ Copyright 2010 10gen Inc.+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -}
+ Data/Bson/Binary.hs view
@@ -0,0 +1,225 @@+-- | Standard binary encoding of BSON documents, version 1.0. See bsonspec.org++module Data.Bson.Binary (+ putDocument, getDocument,+ putDouble, getDouble,+ putInt32, getInt32,+ putInt64, getInt64,+ putCString, getCString+) where++import Prelude hiding (length, concat)+import Data.Bson+import Data.Int+import Data.Word+import Data.Binary.Put+import Data.Binary.Get+import Data.Binary.IEEE754+import Data.ByteString.Char8 (ByteString, pack, length, concat)+import qualified Data.ByteString.Lazy.Char8 as L (ByteString, toChunks, length)+import qualified Data.CompactString.UTF8 as U+import Data.Time.Clock (UTCTime)+import Data.Time.Clock.POSIX+import Control.Applicative ((<$>), (<*>))+import Control.Monad (when)++putElement :: Element -> Put+-- ^ Write binary representation of element+putElement (k := v) = case v of+ Float x -> putTL 0x01 >> putDouble x+ String x -> putTL 0x02 >> putString x+ Doc x -> putTL 0x03 >> putDocument x+ Array x -> putTL 0x04 >> putArray x+ Bin (Binary x) -> putTL 0x05 >> putBinary 0x00 x+ Fun (Function x) -> putTL 0x05 >> putBinary 0x01 x+ Uuid (UUID x) -> putTL 0x05 >> putBinary 0x03 x+ Md5 (MD5 x) -> putTL 0x05 >> putBinary 0x05 x+ UserDef (UserDefined x) -> putTL 0x05 >> putBinary 0x80 x+ ObjId x -> putTL 0x07 >> putObjectId x+ Bool x -> putTL 0x08 >> putBool x+ UTC x -> putTL 0x09 >> putUTC x+ Null -> putTL 0x0A+ RegEx x -> putTL 0x0B >> putRegex x+ JavaScr (Javascript env code) -> if null env+ then putTL 0x0D >> putString code+ else putTL 0x0F >> putClosure code env+ Sym x -> putTL 0x0E >> putSymbol x+ Int32 x -> putTL 0x10 >> putInt32 x+ Int64 x -> putTL 0x12 >> putInt64 x+ Stamp x -> putTL 0x11 >> putMongoStamp x+ MinMax x -> case x of+ MinKey -> putTL 0xFF+ MaxKey -> putTL 0x7F+ where+ putTL t = putTag t >> putLabel k++getElement :: Get Element+-- ^ Read binary representation of Element+getElement = do+ t <- getTag+ k <- getLabel+ v <- case t of+ 0x01 -> Float <$> getDouble+ 0x02 -> String <$> getString+ 0x03 -> Doc <$> getDocument+ 0x04 -> Array <$> getArray+ 0x05 -> getBinary >>= \(s, b) -> case s of+ 0x00 -> return $ Bin (Binary b)+ 0x01 -> return $ Fun (Function b)+ 0x03 -> return $ Uuid (UUID b)+ 0x05 -> return $ Md5 (MD5 b)+ 0x80 -> return $ UserDef (UserDefined b)+ _ -> fail $ "unknown Bson binary subtype " ++ show s+ 0x07 -> ObjId <$> getObjectId+ 0x08 -> Bool <$> getBool+ 0x09 -> UTC <$> getUTC+ 0x0A -> return Null+ 0x0B -> RegEx <$> getRegex+ 0x0D -> JavaScr . Javascript [] <$> getString+ 0x0F -> JavaScr . uncurry (flip Javascript) <$> getClosure+ 0x0E -> Sym <$> getSymbol+ 0x10 -> Int32 <$> getInt32+ 0x12 -> Int64 <$> getInt64+ 0x11 -> Stamp <$> getMongoStamp+ 0xFF -> return (MinMax MinKey)+ 0x7F -> return (MinMax MaxKey)+ _ -> fail $ "unknown Bson value type " ++ show t+ return (k := v)++putTag = putWord8+getTag = getWord8++putLabel = putCString+getLabel = getCString++putDouble = putFloat64le+getDouble = getFloat64le++putInt32 :: Int32 -> Put+putInt32 = putWord32le . fromIntegral++getInt32 :: Get Int32+getInt32 = fromIntegral <$> getWord32le++putInt64 :: Int64 -> Put+putInt64 = putWord64le . fromIntegral++getInt64 :: Get Int64+getInt64 = fromIntegral <$> getWord64le++putCString :: UString -> Put+putCString x = do+ putByteString (U.toByteString x)+ putWord8 0++getCString :: Get UString+getCString = U.fromByteString_ . concat . L.toChunks <$> getLazyByteStringNul++putString :: UString -> Put+putString x = let b = U.toByteString x in do+ putInt32 $ toEnum (length b + 1)+ putByteString b+ putWord8 0++getString :: Get UString+getString = do+ len <- subtract 1 <$> getInt32+ b <- getByteString (fromIntegral len)+ getWord8+ return (U.fromByteString_ b)++putDocument :: Document -> Put+putDocument es = let b = runPut (mapM_ putElement es) in do+ putInt32 $ (toEnum . fromEnum) (L.length b + 5) -- include length and null terminator+ putLazyByteString b+ putWord8 0++getDocument :: Get Document+getDocument = do+ len <- subtract 5 <$> getInt32+ b <- getLazyByteString (fromIntegral len)+ getWord8+ return (runGet getElements b)+ where+ getElements = isEmpty >>= \done -> if done+ then return []+ else (:) <$> getElement <*> getElements++putArray :: [Value] -> Put+putArray vs = putDocument (zipWith f [0..] vs)+ where f i v = U.pack (show i) := v++getArray :: Get [Value]+getArray = map value <$> getDocument++type Subtype = Word8++putBinary :: Subtype -> ByteString -> Put+putBinary t x = let len = toEnum (length x) in do+ putInt32 len+ putTag t+ putByteString x++getBinary :: Get (Subtype, ByteString)+getBinary = do+ len <- getInt32+ t <- getTag+ x <- getByteString (fromIntegral len)+ return (t, x)++{-putBinary :: Subtype -> ByteString -> Put+-- When Binary subtype (0x02) insert extra length field before bytes+putBinary t x = let len = toEnum (length x) in do+ putInt32 $ len + if t == 0x02 then 4 else 0+ putTag t+ when (t == 0x02) (putInt32 len)+ putByteString x-}++{-getBinary :: Get (Subtype, ByteString)+-- When Binary subtype (0x02) there is an extra length field before bytes+getBinary = do+ len <- getInt32+ t <- getTag+ len' <- if t == 0x02 then getInt32 else return len+ x <- getByteString (fromIntegral len')+ return (t, x)-}++putRegex (Regex x y) = putCString x >> putCString y+getRegex = Regex <$> getCString <*> getCString++putSymbol (Symbol x) = putString x+getSymbol = Symbol <$> getString++putMongoStamp (MongoStamp x) = putInt64 x+getMongoStamp = MongoStamp <$> getInt64++putObjectId (Oid x y) = putWord32be x >> putWord64be y+getObjectId = Oid <$> getWord32be <*> getWord64be++putBool x = putWord8 (if x then 1 else 0)+getBool = (> 0) <$> getWord8++putUTC :: UTCTime -> Put+-- store milliseconds since Unix epoch+putUTC x = putInt64 $ round (utcTimeToPOSIXSeconds x * 1000)++getUTC :: Get UTCTime+-- stored as milliseconds since Unix epoch+getUTC = posixSecondsToUTCTime . (/ 1000) . fromIntegral <$> getInt64++putClosure :: UString -> Document -> Put+putClosure x y = let b = runPut (putString x >> putDocument y) in do+ putInt32 $ (toEnum . fromEnum) (L.length b + 4) -- including this length field+ putLazyByteString b++getClosure :: Get (UString, Document)+getClosure = do+ getInt32+ x <- getString+ y <- getDocument+ return (x, y)+++{- Authors: Tony Hannan <tony@10gen.com>+ Copyright 2010 10gen Inc.+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -}
+ Data/UString.hs view
@@ -0,0 +1,33 @@+-- | UTF-8 String++{-# LANGUAGE TypeSynonymInstances, StandaloneDeriving #-}++module Data.UString (+ UString, u,+ module Data.CompactString.UTF8+) where++import Data.CompactString.UTF8+import qualified Data.CompactString as S+import qualified Data.CompactString.Encodings as E+import Text.Read (Read(..))+import Data.Typeable+import Control.Applicative ((<$>))++deriving instance Typeable1 S.CompactString++deriving instance Typeable E.UTF8++instance Read CompactString where+ readPrec = pack <$> readPrec++type UString = CompactString+-- ^ UTF-8 String++u :: String -> UString+u = pack+++{- Authors: Tony Hannan <tony@10gen.com>+ Copyright 2010 10gen Inc.+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -}
+ LICENSE view
@@ -0,0 +1,202 @@+ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONS++ APPENDIX: How to apply the Apache License to your work.++ To apply the Apache License to your work, attach the following+ boilerplate notice, with the fields enclosed by brackets "[]"+ replaced with your own identifying information. (Don't include+ the brackets!) The text should be enclosed in the appropriate+ comment syntax for the file format. We also recommend that a+ file or class name and description of purpose be included on the+ same "printed page" as the copyright notice for easier+ identification within third-party archives.++ Copyright [yyyy] [name of copyright owner]++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ bson.cabal view
@@ -0,0 +1,15 @@+Name: bson+Version: 0.0.1+Synopsis: BSON documents are JSON-like objects with a standard binary encoding+Description: BSON (short for Binary JSON) is a binary-encoded serialization of JSON-like documents.+ .+ This implements version 1.0 of the BSON spec, which is defined at <http://bsonspec.org>.+Category: Data+Homepage: http://github.com/TonyGen/bson-haskell+Author: Tony Hannan+Maintainer: Tony Hannan <tony@10gen.com>+License: OtherLicense+License-file: LICENSE+Build-Depends: base < 5, time, bytestring, unix, network, nano-md5, binary, data-binary-ieee754, compact-string-fix, mtl+Build-Type: Simple+Exposed-modules: Data.UString, Data.Bson, Data.Bson.Binary