bson 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

data/bin/b2json CHANGED
@@ -20,6 +20,8 @@
20
20
  require 'rubygems'
21
21
  require 'bson'
22
22
 
23
+ # Note that this will not properly round-trip in all cases
24
+ # from the output generated by j2bson.
23
25
  begin
24
26
  require 'yajl'
25
27
  rescue LoadError
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: UTF-8
3
+
4
+ # --
5
+ # Copyright (C) 2008-2010 10gen Inc.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ # ++
19
+
20
+ require 'rubygems'
21
+ require 'bson'
22
+
23
+ # We don't use YAJL because we need to specify the class
24
+ # to store the JSON object in. BSON is ordered so we need BSON::OrderedHash
25
+ #
26
+ # We use json/pure because json/ext is broken with BSON::OrderedHash
27
+ # (probably for the same reasons that we can't use YAJL).
28
+ #
29
+ # Note that, at the moment, this will not properly round-trip
30
+ # in all cases from the output generated by b2json.
31
+ begin
32
+ require 'json/pure' # broken with 'json/ext'
33
+ rescue LoadError
34
+ puts "This script requires json/pure. Please install one of the following:"
35
+ puts " gem install json_pure"
36
+ puts " gem install json"
37
+ Process.exit
38
+ end
39
+
40
+ # Convert all JSON objects in an IO into BSON.
41
+ def print_j2bson(io)
42
+ io.each_line do |line|
43
+ jsonobj = JSON.parse(line, { :object_class => BSON::OrderedHash } )
44
+ bsonobj = BSON.serialize(jsonobj)
45
+ STDOUT << bsonobj.to_s
46
+ end
47
+ end
48
+
49
+ # print usage
50
+ def usage()
51
+ STDERR << <<END_OF_USAGE
52
+ usage: j2bson [-h] [file1 [file2]]
53
+
54
+ Converts a JSON file to BSON on STDOUT.
55
+ You can pass multiple filenames.
56
+ If no filenames are passed, then STDIN is consumed.
57
+
58
+ END_OF_USAGE
59
+ exit
60
+ end
61
+
62
+ # no arg, use STDIN
63
+ # -h, print usage and exit
64
+ # otherwise loop of filenames
65
+ if ARGV.empty? then
66
+ print_j2bson(STDIN)
67
+ exit
68
+ elsif ARGV[0] == "-h" then
69
+ usage()
70
+ else
71
+ ARGV.each { |fname| print_j2bson(File.new(fname)) }
72
+ end
@@ -13,10 +13,10 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.files = ['Rakefile', 'bson.gemspec', 'LICENSE.txt']
15
15
  s.files += ['lib/bson.rb'] + Dir['lib/bson/**/*.rb']
16
- s.files += ['bin/b2json']
16
+ s.files += ['bin/b2json', 'bin/j2bson']
17
17
  s.test_files = Dir['test/mongo_bson/*.rb']
18
18
 
19
- s.executables = ['b2json']
19
+ s.executables = ['b2json', 'j2bson']
20
20
 
21
21
  s.has_rdoc = true
22
22
 
@@ -2,10 +2,10 @@
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
- MINIMUM_BSON_EXT_VERSION = "1.0.1"
5
+ MINIMUM_BSON_EXT_VERSION = "1.0.4"
6
6
 
7
7
  module BSON
8
- VERSION = "1.0.3"
8
+ VERSION = "1.0.4"
9
9
  def self.serialize(obj, check_keys=false, move_id=false)
10
10
  BSON_CODER.serialize(obj, check_keys, move_id)
11
11
  end
@@ -27,9 +27,12 @@ module BSON
27
27
 
28
28
  def ==(other)
29
29
  begin
30
- !other.nil? &&
31
- keys == other.keys &&
32
- values == other.values
30
+ case other
31
+ when BSON::OrderedHash
32
+ keys == other.keys && values == other.values
33
+ else
34
+ !other.nil? && keys.size == other.keys.size && keys.all? {|x| self[x] == other[x] }
35
+ end
33
36
  rescue
34
37
  false
35
38
  end
@@ -140,7 +140,7 @@ module BSON
140
140
  # but lacks an ObjectID type, this JSON format encodes the type using an $oid key.
141
141
  #
142
142
  # @return [String] the object id represented as MongoDB extended JSON.
143
- def to_json(escaped=false)
143
+ def to_json(*a)
144
144
  "{\"$oid\": \"#{to_s}\"}"
145
145
  end
146
146
 
@@ -44,11 +44,23 @@ class BSONTest < Test::Unit::TestCase
44
44
  assert_equal doc, @encoder.deserialize(bson)
45
45
  end
46
46
 
47
- def test_null
48
- #doc = {"\x00" => "foo"}
49
- #@encoder.serialize(doc)
50
- @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
51
- end
47
+ # def test_null
48
+ # #doc = {"\x00" => "foo"}
49
+ # #@encoder.serialize(doc)
50
+ # @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
51
+ # end
52
+
53
+ # def test_dbref
54
+ # oid = ObjectID.new
55
+ # doc = {}
56
+ # doc['dbref'] = DBRef.new('namespace', oid)
57
+ # bson = BSON::BSON_CODER.serialize(doc)
58
+ # doc2 = BSON::BSON_CODER.deserialize(bson)
59
+ # puts doc2.class
60
+ # puts doc2
61
+ # assert_equal 'namespace', doc2['dbref'].namespace
62
+ # assert_equal oid, doc2['dbref'].object_id
63
+ # end
52
64
 
53
65
  # def test_null_character
54
66
  # #assert_raise InvalidDocument do
@@ -168,15 +180,15 @@ class BSONTest < Test::Unit::TestCase
168
180
  # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
169
181
  # end
170
182
  #
171
- # def test_ordered_hash
172
- # doc = BSON::OrderedHash.new
173
- # doc["b"] = 1
174
- # doc["a"] = 2
175
- # doc["c"] = 3
176
- # doc["d"] = 4
177
- # bson = BSON::BSON_CODER.serialize(doc)
178
- # assert_equal doc, BSON::BSON_CODER.deserialize(bson)
179
- # end
183
+ def test_ordered_hash
184
+ doc = BSON::OrderedHash.new
185
+ doc["b"] = 1
186
+ doc["a"] = 2
187
+ doc["c"] = 3
188
+ doc["d"] = 4
189
+ bson = BSON::BSON_CODER.serialize(doc)
190
+ assert_equal doc, BSON::BSON_CODER.deserialize(bson)
191
+ end
180
192
  #
181
193
  # def test_object
182
194
  # doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
@@ -7,14 +7,10 @@ class JSONTest < Test::Unit::TestCase
7
7
  include Mongo
8
8
  include BSON
9
9
 
10
- def assert_json(obj, json)
11
- assert_equal json, obj.to_json
12
- assert_equal obj, obj.class.json_create(json)
13
- end
14
-
15
- def test_json
10
+ def test_object_id_as_json
16
11
  id = ObjectID.new
17
- assert_json id, "{\"$oid\": \"#{id}\"}"
12
+ obj = {'_id' => id}
13
+ assert_equal "{\"_id\":{\"$oid\": \"#{id.to_s}\"}}", obj.to_json
18
14
  end
19
15
 
20
16
  end
@@ -140,6 +140,16 @@ class OrderedHashTest < Test::Unit::TestCase
140
140
  assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
141
141
  end
142
142
 
143
+ def test_equality_with_hash
144
+ o = BSON::OrderedHash.new
145
+ o[:a] = 1
146
+ o[:b] = 2
147
+ o[:c] = 3
148
+ r = {:a => 1, :b => 2, :c => 3}
149
+ assert r == o
150
+ assert o == r
151
+ end
152
+
143
153
  def test_update
144
154
  other = BSON::OrderedHash.new
145
155
  other['f'] = 'foo'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 3
9
- version: 1.0.3
8
+ - 4
9
+ version: 1.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jim Menard
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-06-15 00:00:00 -04:00
19
+ date: 2010-07-13 00:00:00 -04:00
20
20
  default_executable:
21
21
  dependencies: []
22
22
 
@@ -24,6 +24,7 @@ description: A Ruby BSON implementation for MongoDB. For more information about
24
24
  email: mongodb-dev@googlegroups.com
25
25
  executables:
26
26
  - b2json
27
+ - j2bson
27
28
  extensions: []
28
29
 
29
30
  extra_rdoc_files: []
@@ -44,6 +45,7 @@ files:
44
45
  - lib/bson/types/min_max_keys.rb
45
46
  - lib/bson/types/objectid.rb
46
47
  - bin/b2json
48
+ - bin/j2bson
47
49
  has_rdoc: true
48
50
  homepage: http://www.mongodb.org
49
51
  licenses: []