草庐IT

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

coder 2023-10-28 原文

所以,我是 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/

有关json - Haskell - 将 BSON 映射到 JSON 的正确方法 - 将代码放在哪里的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  6. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  7. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  10. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

随机推荐