Skip to content

Draft: #as_json should be API-ready #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/bson/decimal128.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ class Decimal128
# @since 4.2.0
NATIVE_TYPE = BigDecimal

# Get the Decimal128 as JSON hash data.
# Return a string representation of the Decimal128 use in standard
# application-level JSON serialization. This method is intentionally
# different from #as_extended_json.
#
# @example Get the Decimal128 as a JSON hash.
# @example Get the Decimal128 as a JSON-serializable object.
# decimal.as_json
#
# @return [ Hash ] The number as a JSON hash.
#
# @since 4.2.0
# @deprecated Use as_extended_json instead.
# @return [ String ] The object id as a string.
def as_json(*args)
as_extended_json
to_s
end

# Converts this object to a representation directly serializable to
Expand Down
4 changes: 4 additions & 0 deletions lib/bson/int32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def ==(other)
alias :eql? :==
alias :=== :==

def as_json(**options)
value
end

# Converts this object to a representation directly serializable to
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
#
Expand Down
4 changes: 4 additions & 0 deletions lib/bson/int64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def ==(other)
alias :eql? :==
alias :=== :==

def as_json(**options)
value
end

# Converts this object to a representation directly serializable to
# Extended JSON (https://github.com/mongodb/specifications/blob/master/source/extended-json.rst).
#
Expand Down
13 changes: 6 additions & 7 deletions lib/bson/object_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,16 @@ def ===(other)
super
end

# Return the object id as a JSON hash representation.
# Return a string representation of the object id for use in standard
# application-level JSON serialization. This method is intentionally
# different from #as_extended_json.
#
# @example Get the object id as JSON.
# @example Get the object id as a JSON-serializable object.
# object_id.as_json
#
# @return [ Hash ] The object id as a JSON hash.
#
# @since 2.0.0
# @deprecated Use as_extended_json instead.
# @return [ String ] The object id as a string.
def as_json(*args)
as_extended_json
to_s
end

# Converts this object to a representation directly serializable to
Expand Down
14 changes: 1 addition & 13 deletions lib/bson/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ module Regexp
# @deprecated Will be removed in 5.0
RUBY_MULTILINE_VALUE = 'ms'.freeze

# Get the regexp as JSON hash data.
#
# @example Get the regexp as a JSON hash.
# regexp.as_json
#
# @return [ Hash ] The regexp as a JSON hash.
#
# @since 2.0.0
def as_json(*args)
{ "$regex" => source, "$options" => bson_options }
end

# Get the regular expression as encoded BSON.
#
# @example Get the regular expression as encoded BSON.
Expand Down Expand Up @@ -211,7 +199,7 @@ def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
#
# @since 4.2.0
def as_json(*args)
as_extended_json(mode: :legacy)
compile.to_s
end

# Converts this object to a representation directly serializable to
Expand Down
17 changes: 12 additions & 5 deletions lib/bson/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,24 @@ def <=>(other)
[ a, b ].sort[0] == a ? -1 : 1
end

# Compile the Timestamp into a native Time type.
#
# @example Compile the timestamp.
# timestamp.compile
#
# @return [ ::Time ] The compiled timestamp.
def compile
::Time.at(seconds).utc
end

# Get the timestamp as JSON hash data.
#
# @example Get the timestamp as a JSON hash.
# timestamp.as_json
#
# @return [ Hash ] The timestamp as a JSON hash.
#
# @since 2.0.0
# @deprecated Use as_extended_json instead.
# @return [ Time ] The timestamp as a JSON hash.
def as_json(*args)
as_extended_json
compile
end

# Converts this object to a representation directly serializable to
Expand Down