gpt4 book ai didi

json - Haskell - 将 BSON 映射到 JSON 的正确方法 - 将代码放在哪里

转载 作者:可可西里 更新时间:2023-11-01 09:15:57 25 4
gpt4 key购买 nike

所以,我是 Haskell 及其社区的新手。我想制作一个 mongodb 支持的 JSON API。 Mongo 和 JSON 非常适合(至少在节点中),因为它将文档存储在 BSON 中,这是“二进制 json”,因此理论上很容易将其转换为 JSON。

经过多次错误,我终于写出了下面的代码。

{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}

-- https://github.com/mailrank/aeson/blob/master/examples/Demo.hs
-- cabal install aeson
-- cabal install mongoDb

import Data.Aeson
import qualified Data.Aeson.Types as T

import Data.Attoparsec (parse, Result(..))
import Data.Attoparsec.Number (Number(..))
import qualified Data.Text as Text
import Control.Applicative ((<$>))
import Control.Monad (mzero)
import qualified Data.ByteString.Char8 as BS
-- Aeson's "encode" to JSON generates lazy bytestrings
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.CompactString as CS

import Database.MongoDB
import Data.Bson
import qualified Data.Bson as Bson
import qualified Data.Vector

import GHC.Int

-- Is there a better way to convert between string representations?
csToTxt :: UString -> Text.Text
csToTxt cs = Text.pack $ CS.unpack cs

bsToTxt :: BS.ByteString -> Text.Text
bsToTxt bs = Text.pack $ BS.unpack bs

fieldToPair :: Field -> T.Pair
fieldToPair f = key .= val
where key = csToTxt $ label f
val = toJSON (value f)

instance ToJSON Field where
toJSON f = object [fieldToPair f]

-- Is this what I'm supposed to do? Just go through and map everything?
instance ToJSON Data.Bson.Value where
toJSON (Float f) = T.Number $ D f
toJSON (Bson.String s) = T.String $ csToTxt s
toJSON (Bson.Array xs) = T.Array $ Data.Vector.fromList (map toJSON xs)
toJSON (Doc fs) = object $ map fieldToPair fs
toJSON (Uuid (UUID bs)) = T.String $ bsToTxt bs
toJSON (Bson.Bool b) = T.Bool b
toJSON (Int32 i) = T.Number (I (fromIntegral i))
toJSON (Int64 i) = T.Number (I (fromIntegral i))

toJSON (ObjId (Oid w32 w64)) = T.String "look up GHC.Word.Word32 and GHC.Word.Word64"
toJSON (UTC time) = T.String "look up Data.Time.Clock.UTC.UTCTime"

toJSON (Md5 m) = T.Null
toJSON (UserDef u) = T.Null
toJSON (Bin b) = T.Null
toJSON (Fun f) = T.Null
toJSON Bson.Null = T.Null
toJSON (RegEx r) = T.Null
toJSON (JavaScr j) = T.Null
toJSON (Sym s) = T.Null
toJSON (Stamp s) = T.Null
toJSON (MinMax mm) = T.Null

-- Data.Bson.Value and T.Value for reference
-- data Data.Bson.Value
-- = Float Double
-- | Data.Bson.String UString
-- | Doc Document
-- | Data.Bson.Array [Data.Bson.Value]
-- | Bin Binary
-- | Fun Function
-- | Uuid UUID
-- | Md5 MD5
-- | UserDef UserDefined
-- | ObjId ObjectId
-- | Data.Bson.Bool Bool
-- | UTC time-1.2.0.3:Data.Time.Clock.UTC.UTCTime
-- | Data.Bson.Null
-- | RegEx Regex
-- | JavaScr Javascript
-- | Sym Symbol
-- | Int32 GHC.Int.Int32
-- | Int64 GHC.Int.Int64
-- | Stamp MongoStamp
-- | MinMax MinMaxKey

-- data T.Value
-- = Object Object
-- | T.Array Array
-- | T.String Text.Text
-- | Number Data.Attoparsec.Number.Number
-- | T.Bool !Bool
-- | T.Null

main ::IO ()
main = do
putStrLn $ "testing again: " ++ BSL.unpack (encode ["Hello", "I", "am", "angry"])

let field = "key" =: "value"
print field
print $ label field
putStrLn $ CS.unpack $ label field

putStrLn $ show "asdf"

-- Getting close
putStrLn $ "testing again: " ++ BSL.unpack (encode ["hello" =: "world", "num" =: 10.05, "num2" =: 5, "sub" =: ["doc","charlie"], "bool" =: False])
putStrLn $ "testing again: " ++ BSL.unpack (encode ["hello" =: "world", "sub" =: ["one" =: 1, "two" =: 2]])
  1. 有没有更好的方法来映射两种非常相似的类型?

  2. 有没有更好的方法在两个字符串实现之间进行映射?

  3. 完成后,它应该放在哪里?它属于 JSON 或 BSON/MongoDB 项目,还是应该作为自己的模块发布?

最佳答案

为了现在找到它的人们的利益,这已经完成了:https://hackage.haskell.org/package/AesonBson .看起来是相同的方法。

关于json - Haskell - 将 BSON 映射到 JSON 的正确方法 - 将代码放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996140/

25 4 0