ruby:BSON序列化和反序列化

后端开发   发布日期:2025年08月11日   浏览次数:208
require 'bson'
doc = {
  _id: BSON::ObjectId.new,  
  username:"kbanker",
  action_code:rand(5),
  time:Time.now.utc,
  n:1
}
bson = doc.to_bson
puts "Document #{doc.inspect} takes up #{bson.length} bytes as BSON"
deserialized_doc = BSON::Document.from_bson(bson);
puts "Here's our document deserialized from BSON:"
puts deserialized_doc.inspect

 

注意:Ruby散列的值必须能转换为BSON类型,键名必须合法(由null结尾的字符串组成;最大长度为255字节;字符串不能以$开头,不能包含.字符,除结尾处外不能包含null字节)。

以上就是ruby:BSON序列化和反序列化的详细内容,更多关于ruby:BSON序列化和反序列化的资料请关注九品源码其它相关文章!