Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 漂亮的BSON::OrderedHash使用Rails_Ruby On Rails_Json_Mongodb_Bson - Fatal编程技术网

Ruby on rails 漂亮的BSON::OrderedHash使用Rails

Ruby on rails 漂亮的BSON::OrderedHash使用Rails,ruby-on-rails,json,mongodb,bson,Ruby On Rails,Json,Mongodb,Bson,如果我将BSON::OrderedHash(来自MongoDb集合)传递给JSON.pretty\u generate,我会得到未格式化的JSON文档。如何获得格式类似.pretty()的bson文档?它适用于我的mongo 1.9.1、bson 1.9.1和json 1.8.0,如以下测试代码所示,尽可能接近预期 pretty_bson.rb require 'mongo' require 'bson' require 'json' require 'test/unit' class Bso

如果我将
BSON::OrderedHash
(来自MongoDb集合)传递给
JSON.pretty\u generate
,我会得到未格式化的JSON文档。如何获得格式类似.pretty()的bson文档?

它适用于我的mongo 1.9.1、bson 1.9.1和json 1.8.0,如以下测试代码所示,尽可能接近预期

pretty_bson.rb

require 'mongo'
require 'bson'
require 'json'
require 'test/unit'

class BsonPrettyTest < Test::Unit::TestCase
  def setup
    @client = Mongo::MongoClient.new
    @db = @client['test']
    @coll = @db['people']
    @coll.remove
    assert_equal 0, @coll.count
  end

  test "bson pretty" do
    text = <<-EOF
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
},
    "phoneNumbers": [
    {
        "type": "home",
    "number": "212 555-1234"
},
    {
        "type": "fax",
    "number": "646 555-4567"
}
]
}
    EOF
    hash = JSON.parse(text)
    @coll.insert(hash)
    doc = @coll.find_one
    assert_equal BSON::OrderedHash, doc.class
    puts JSON.pretty_generate(hash)
    puts JSON.pretty_generate(doc)
  end
end
Loaded suite pretty_bson
Started
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ],
  "_id": {"$oid": "51df0c537f11bab55d000001"}
}
{
  "_id": {"$oid": "51df0c537f11bab55d000001"},
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ]
}
.

Finished in 0.005689 seconds.

1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

175.78 tests/s, 351.56 assertions/s