Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MINKEY = -1
- EOO = 0
- NUMBER = 1
- STRING = 2
- OBJECT = 3
- ARRAY = 4
- BINARY = 5
- UNDEFINED = 6
- OID = 7
- BOOLEAN = 8
- DATE = 9
- NULL = 10
- REGEX = 11
- REF = 12
- CODE = 13
- SYMBOL = 14
- CODE_W_SCOPE = 15
- NUMBER_INT = 16
- TIMESTAMP = 17
- NUMBER_LONG = 18
- MAXKEY = 127
- def bson_type(o)
- case o
- when nil
- NULL
- when Integer
- NUMBER_INT
- when Float
- NUMBER
- when ByteBuffer
- BINARY
- when Code
- CODE_W_SCOPE
- when String
- STRING
- when Array
- ARRAY
- when Regexp
- REGEX
- when ObjectId
- OID
- when DBRef
- REF
- when true, false
- BOOLEAN
- when Time
- DATE
- when Hash
- OBJECT
- when Symbol
- SYMBOL
- when MaxKey
- MAXKEY
- when MinKey
- MINKEY
- when Numeric
- raise Exception, "Cannot serialize the Numeric type #{o.class} as BSON; only Fixum, Bignum, and Float are supported."
- when Date, DateTime
- raise Exception, "#{o.class} is not currently supported; " +
- "use a UTC Time instance instead."
- else
- if defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
- raise Exception, "ActiveSupport::TimeWithZone is not currently supported; " +
- "use a UTC Time instance instead."
- else
- raise Exception, "Cannot serialize #{o.class} as a BSON type; it either isn't supported or won't translate to BSON."
- end
- end
- end
- def serialize_number_element(val, type)
- if type == NUMBER
- puts "#{val} is type #{type} and written as a double"
- else
- if val > 2**64 / 2 - 1 or val < -2**64 / 2
- raise RangeError.new("MongoDB can only handle 8-byte ints")
- end
- if val > 2**32 / 2 - 1 or val < -2**32 / 2
- puts "#{val} is type #{type} and written as a int64"
- else
- puts "#{val} is type #{type} and written as a int32"
- end
- end
- end
- a = [1, 32767, 2**32 / 2 -1, 2**32, 2**33, 2**63 / 2, 2**63]
- a.each do |number|
- puts "type #{bson_type(number)}"
- serialize_number_element(number, bson_type(number))
- end
Add Comment
Please, Sign In to add comment