bson 1.0.2 → 1.0.3
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/Rakefile +3 -3
- data/bin/b2json +2 -2
- data/lib/bson.rb +1 -1
- data/lib/bson/ordered_hash.rb +11 -1
- data/test/mongo_bson/jruby_encode_test.rb +32 -14
- data/test/mongo_bson/json_test.rb +20 -0
- metadata +47 -40
- data/lib/bson/bson_java.rb +0 -123
data/Rakefile
CHANGED
@@ -110,7 +110,7 @@ end
|
|
110
110
|
|
111
111
|
desc "Generate documentation"
|
112
112
|
task :rdoc do
|
113
|
-
version = eval(File.read("mongo
|
113
|
+
version = eval(File.read("mongo.gemspec")).version
|
114
114
|
out = File.join('html', version.to_s)
|
115
115
|
FileUtils.rm_rf('html')
|
116
116
|
system "rdoc --main README.rdoc --op #{out} --inline-source --quiet README.rdoc `find lib -name '*.rb'`"
|
@@ -121,7 +121,7 @@ task :ydoc do
|
|
121
121
|
require File.join(File.dirname(__FILE__), 'lib', 'mongo')
|
122
122
|
out = File.join('ydoc', Mongo::VERSION)
|
123
123
|
FileUtils.rm_rf('ydoc')
|
124
|
-
system "yardoc lib/**/*.rb lib/mongo/**/*.rb -e docs/yard_ext.rb -p docs/templates -o #{out} --title MongoRuby-#{Mongo::VERSION}"
|
124
|
+
system "yardoc lib/**/*.rb lib/mongo/**/*.rb lib/bson/**/*.rb -e docs/yard_ext.rb -p docs/templates -o #{out} --title MongoRuby-#{Mongo::VERSION}"
|
125
125
|
end
|
126
126
|
|
127
127
|
desc "Publish documentation to mongo.rubyforge.org"
|
@@ -136,7 +136,7 @@ namespace :gem do
|
|
136
136
|
task :install do
|
137
137
|
sh "gem build bson.gemspec"
|
138
138
|
sh "gem install bson-*.gem"
|
139
|
-
sh "gem build mongo
|
139
|
+
sh "gem build mongo.gemspec"
|
140
140
|
sh "gem install mongo-*.gem"
|
141
141
|
sh "rm mongo-*.gem"
|
142
142
|
sh "rm bson-*.gem"
|
data/bin/b2json
CHANGED
@@ -40,10 +40,10 @@ end
|
|
40
40
|
# print usage
|
41
41
|
def usage()
|
42
42
|
STDERR << <<END_OF_USAGE
|
43
|
-
usage: b2json
|
43
|
+
usage: b2json [-h] [file1 [file2]]
|
44
44
|
|
45
45
|
Converts a BSON file to JSON on STDOUT.
|
46
|
-
You can pass multiple
|
46
|
+
You can pass multiple filenames.
|
47
47
|
If no filenames are passed, then STDIN is consumed.
|
48
48
|
|
49
49
|
END_OF_USAGE
|
data/lib/bson.rb
CHANGED
@@ -5,7 +5,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
5
5
|
MINIMUM_BSON_EXT_VERSION = "1.0.1"
|
6
6
|
|
7
7
|
module BSON
|
8
|
-
VERSION = "1.0.
|
8
|
+
VERSION = "1.0.3"
|
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
|
data/lib/bson/ordered_hash.rb
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
#
|
21
21
|
# Under Ruby 1.9 and greater, this class has no added methods because Ruby's
|
22
22
|
# Hash already keeps its keys ordered by order of insertion.
|
23
|
+
require 'set'
|
24
|
+
|
23
25
|
module BSON
|
24
26
|
class OrderedHash < Hash
|
25
27
|
|
@@ -55,6 +57,7 @@ module BSON
|
|
55
57
|
def initialize(*a, &b)
|
56
58
|
super
|
57
59
|
@ordered_keys = []
|
60
|
+
@ordered_set = Set.new
|
58
61
|
end
|
59
62
|
|
60
63
|
def keys
|
@@ -63,7 +66,11 @@ module BSON
|
|
63
66
|
|
64
67
|
def []=(key, value)
|
65
68
|
@ordered_keys ||= []
|
66
|
-
@
|
69
|
+
@ordered_set ||= Set.new
|
70
|
+
unless @ordered_set.member?(key)
|
71
|
+
@ordered_keys << key
|
72
|
+
@ordered_set.add(key)
|
73
|
+
end
|
67
74
|
super(key, value)
|
68
75
|
end
|
69
76
|
|
@@ -93,6 +100,7 @@ module BSON
|
|
93
100
|
@ordered_keys ||= []
|
94
101
|
@ordered_keys += other.keys # unordered if not an BSON::OrderedHash
|
95
102
|
@ordered_keys.uniq!
|
103
|
+
@ordered_set = Set.new(@ordered_keys)
|
96
104
|
super(other)
|
97
105
|
end
|
98
106
|
|
@@ -106,6 +114,7 @@ module BSON
|
|
106
114
|
|
107
115
|
def delete(key, &block)
|
108
116
|
@ordered_keys.delete(key) if @ordered_keys
|
117
|
+
@ordered_set.delete(key) if @ordered_set
|
109
118
|
super
|
110
119
|
end
|
111
120
|
|
@@ -119,6 +128,7 @@ module BSON
|
|
119
128
|
|
120
129
|
def clear
|
121
130
|
super
|
131
|
+
@ordered_set.clear
|
122
132
|
@ordered_keys = []
|
123
133
|
end
|
124
134
|
|
@@ -36,15 +36,33 @@ class BSONTest < Test::Unit::TestCase
|
|
36
36
|
include BSON
|
37
37
|
|
38
38
|
def setup
|
39
|
-
@
|
39
|
+
@encoder = BSON::BSON_CODER
|
40
40
|
end
|
41
41
|
|
42
42
|
def assert_doc_pass(doc)
|
43
|
-
bson = @
|
44
|
-
assert_equal doc, @
|
43
|
+
bson = @encoder.serialize(doc)
|
44
|
+
assert_equal doc, @encoder.deserialize(bson)
|
45
45
|
end
|
46
46
|
|
47
|
-
|
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_null_character
|
54
|
+
# #assert_raise InvalidDocument do
|
55
|
+
# bson = @encoder.serialize({"\x00" => "a"})
|
56
|
+
# puts bson
|
57
|
+
# #end
|
58
|
+
# puts BSON_RUBY.deserialize(bson)
|
59
|
+
#
|
60
|
+
# #assert_raise InvalidDocument do
|
61
|
+
# # @encoder.serialize({"a" => (Regexp.compile "ab\x00c")})
|
62
|
+
# #end
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
## def test_symbol_key
|
48
66
|
# doc = {:foo => "bar"}
|
49
67
|
# p doc.keys
|
50
68
|
# bson = @coder.serialize(doc)
|
@@ -55,17 +73,17 @@ class BSONTest < Test::Unit::TestCase
|
|
55
73
|
# def test_string
|
56
74
|
# assert_doc_pass({'doc' => 'hello, world'})
|
57
75
|
# end
|
76
|
+
##
|
77
|
+
# require 'iconv'
|
78
|
+
# def test_invalid_string
|
79
|
+
# require 'iconv'
|
80
|
+
# string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
81
|
+
# doc = {'doc' => string}
|
82
|
+
# bson = @coder.serialize(doc)
|
83
|
+
# assert_equal doc, @coder.deserialize(bson)
|
84
|
+
# end
|
85
|
+
#
|
58
86
|
#
|
59
|
-
require 'iconv'
|
60
|
-
def test_invalid_string
|
61
|
-
require 'iconv'
|
62
|
-
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé')
|
63
|
-
doc = {'doc' => string}
|
64
|
-
bson = @coder.serialize(doc)
|
65
|
-
assert_equal doc, @coder.deserialize(bson)
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
87
|
# def test_nested_string
|
70
88
|
# assert_doc_pass({'doc' => {'text' => 'hello, world'}})
|
71
89
|
# end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
class JSONTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mongo
|
8
|
+
include BSON
|
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
|
16
|
+
id = ObjectID.new
|
17
|
+
assert_json id, "{\"$oid\": \"#{id}\"}"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
metadata
CHANGED
@@ -1,45 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
version: 1.0.3
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
|
-
|
8
|
-
|
9
|
-
|
12
|
+
- Jim Menard
|
13
|
+
- Mike Dirolf
|
14
|
+
- Kyle Banker
|
10
15
|
autorequire:
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
18
|
|
14
|
-
date: 2010-06-
|
19
|
+
date: 2010-06-15 00:00:00 -04:00
|
15
20
|
default_executable:
|
16
21
|
dependencies: []
|
17
22
|
|
18
23
|
description: A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.
|
19
24
|
email: mongodb-dev@googlegroups.com
|
20
25
|
executables:
|
21
|
-
|
26
|
+
- b2json
|
22
27
|
extensions: []
|
23
28
|
|
24
29
|
extra_rdoc_files: []
|
25
30
|
|
26
31
|
files:
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
- bin/b2json
|
32
|
+
- Rakefile
|
33
|
+
- bson.gemspec
|
34
|
+
- LICENSE.txt
|
35
|
+
- lib/bson.rb
|
36
|
+
- lib/bson/bson_c.rb
|
37
|
+
- lib/bson/bson_ruby.rb
|
38
|
+
- lib/bson/byte_buffer.rb
|
39
|
+
- lib/bson/exceptions.rb
|
40
|
+
- lib/bson/ordered_hash.rb
|
41
|
+
- lib/bson/types/binary.rb
|
42
|
+
- lib/bson/types/code.rb
|
43
|
+
- lib/bson/types/dbref.rb
|
44
|
+
- lib/bson/types/min_max_keys.rb
|
45
|
+
- lib/bson/types/objectid.rb
|
46
|
+
- bin/b2json
|
43
47
|
has_rdoc: true
|
44
48
|
homepage: http://www.mongodb.org
|
45
49
|
licenses: []
|
@@ -48,32 +52,35 @@ post_install_message:
|
|
48
52
|
rdoc_options: []
|
49
53
|
|
50
54
|
require_paths:
|
51
|
-
|
55
|
+
- lib
|
52
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
57
|
requirements:
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
58
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
64
|
requirements:
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
64
70
|
requirements: []
|
65
71
|
|
66
72
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.3.
|
73
|
+
rubygems_version: 1.3.6
|
68
74
|
signing_key:
|
69
75
|
specification_version: 3
|
70
76
|
summary: Ruby implementation of BSON
|
71
77
|
test_files:
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
- test/mongo_bson/basic_test.rb
|
79
|
+
- test/mongo_bson/binary_test.rb
|
80
|
+
- test/mongo_bson/bson_test.rb
|
81
|
+
- test/mongo_bson/byte_buffer_test.rb
|
82
|
+
- test/mongo_bson/jruby_bson_test.rb
|
83
|
+
- test/mongo_bson/jruby_encode_test.rb
|
84
|
+
- test/mongo_bson/json_test.rb
|
85
|
+
- test/mongo_bson/objectid_test.rb
|
86
|
+
- test/mongo_bson/ordered_hash_test.rb
|
data/lib/bson/bson_java.rb
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
# A thin wrapper for the CBson class
|
3
|
-
module BSON
|
4
|
-
module JMongo
|
5
|
-
import com.mongodb.BasicDBList
|
6
|
-
import com.mongodb.BasicDBObject
|
7
|
-
import com.mongodb.Bytes
|
8
|
-
import org.bson.BSONDecoder
|
9
|
-
import org.bson.BSONEncoder
|
10
|
-
|
11
|
-
import org.bson.BSONCallback
|
12
|
-
import org.bson.types.BasicBSONList
|
13
|
-
import org.bson.BasicBSONObject
|
14
|
-
import org.bson.types.Binary
|
15
|
-
import org.bson.types.ObjectId
|
16
|
-
import org.bson.types.Symbol
|
17
|
-
import org.bson.types.CodeWScope
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module BSON
|
22
|
-
class BSON_JAVA
|
23
|
-
#ENC = JMongo::BSONEncoder.new
|
24
|
-
BSON::ObjectID
|
25
|
-
|
26
|
-
ENC = Java::OrgJbson::RubyBSONEncoder.new(JRuby.runtime)
|
27
|
-
DEC = JMongo::BSONDecoder.new
|
28
|
-
|
29
|
-
|
30
|
-
def self.deserialize(buf)
|
31
|
-
if buf.is_a? String
|
32
|
-
buf = ByteBuffer.new(buf.unpack("C*")) if buf
|
33
|
-
end
|
34
|
-
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
|
35
|
-
DEC.decode(buf.to_a.to_java(Java::byte), callback)
|
36
|
-
callback.get
|
37
|
-
end
|
38
|
-
|
39
|
-
def deserialize(buf)
|
40
|
-
callback = Java::OrgJbson::RubyBSONCallback.new(JRuby.runtime)
|
41
|
-
DEC.decode(buf.to_a.to_java(Java::byte), callback)
|
42
|
-
callback.get
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.serialize(obj, check=false, move=false)
|
46
|
-
ENC.encode(obj)
|
47
|
-
end
|
48
|
-
|
49
|
-
def to_dbobject obj
|
50
|
-
case obj
|
51
|
-
when Array
|
52
|
-
array_to_dblist obj
|
53
|
-
when Hash
|
54
|
-
hash_to_dbobject obj
|
55
|
-
#when BSON::Binary
|
56
|
-
# JMongo::Binary.new(obj.subtype, obj.to_a)
|
57
|
-
when BSON::ObjectID
|
58
|
-
JMongo::ObjectId.new(obj.to_s)
|
59
|
-
when Regexp
|
60
|
-
str = obj.source
|
61
|
-
options = obj.options
|
62
|
-
options_flag = 0
|
63
|
-
options_flag |= JavaPattern::CASE_INSENSITIVE if ((options & Regexp::IGNORECASE) != 0)
|
64
|
-
options_flag |= JavaPattern::MULTILINE if ((options & Regexp::MULTILINE) != 0)
|
65
|
-
#options_flag |= JavaPattern::EXTENDED if ((options & Regexp::EXTENDED) != 0)
|
66
|
-
Java::JavaUtilRegex::Pattern.compile(str, options_flag)
|
67
|
-
when Symbol
|
68
|
-
JMongo::Symbol.new(obj)
|
69
|
-
when BSON::Binary
|
70
|
-
obj.put_int(obj.size, 0)
|
71
|
-
b = JMongo::Binary.new(obj.subtype, obj.to_a.to_java(Java::byte))
|
72
|
-
obj.to_a.to_java(Java::byte)
|
73
|
-
when BSON::DBRef
|
74
|
-
|
75
|
-
|
76
|
-
else
|
77
|
-
# primitive value, no conversion necessary
|
78
|
-
#puts "Un-handled class type [#{obj.class}]"
|
79
|
-
obj
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def from_java_object(obj)
|
84
|
-
case obj
|
85
|
-
when JMongo::BasicBSONList
|
86
|
-
obj
|
87
|
-
when JMongo::BasicBSONObject
|
88
|
-
hsh = {}
|
89
|
-
obj.toMap.keySet.each do |key|
|
90
|
-
value = obj.get(key)
|
91
|
-
hsh[key] = self.from_java_object(value)
|
92
|
-
end
|
93
|
-
hsh
|
94
|
-
when JMongo::ObjectId
|
95
|
-
BSON::ObjectID.from_string(obj.toStringMongod())
|
96
|
-
else
|
97
|
-
obj
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
|
103
|
-
def hash_to_dbobject doc
|
104
|
-
obj = JMongo::BasicDBObject.new
|
105
|
-
|
106
|
-
doc.each_pair do |key, value|
|
107
|
-
obj.append(key, to_dbobject(value))
|
108
|
-
end
|
109
|
-
|
110
|
-
obj
|
111
|
-
end
|
112
|
-
|
113
|
-
def array_to_dblist ary
|
114
|
-
list = JMongo::BasicDBList.new
|
115
|
-
|
116
|
-
ary.each_with_index do |element, index|
|
117
|
-
list.put(index, to_dbobject(element))
|
118
|
-
end
|
119
|
-
|
120
|
-
list
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|