Skip to content
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

jrjackson improperly dumps BSON::ObjectId #42

Closed
cheald opened this issue Sep 24, 2015 · 5 comments
Closed

jrjackson improperly dumps BSON::ObjectId #42

cheald opened this issue Sep 24, 2015 · 5 comments

Comments

@cheald
Copy link

cheald commented Sep 24, 2015

Using jrjackson 0.3.4 and multi_json:

BSON::ObjectId defines both #to_a and #to_json. AnySerializer prefers to_a over to_json, which results in object IDs being dumped as byte arrays rather than hex strings.

It seems incorrect that AnySerializer should prefer anything over to_json, since that should be canonically what the representation of the object should be, no?

:001 > i = Page.first._id
 => BSON::ObjectId('55fac1442f904994449b68d8')

:002 > JSON.dump(i)
 => "\"55fac1442f904994449b68d8\""
:003 > MultiJson.dump(i)
 => "\"55fac1442f904994449b68d8\""

:005 > JSON.dump({_id: i})
 => "{\"_id\":\"55fac1442f904994449b68d8\"}"
:004 > MultiJson.dump({_id: i})
 => "{\"_id\":[85,250,193,68,47,144,73,148,68,155,104,216]}"

Repro:

# Gemfile
source "https://rubygems.org"
gem "mongo", "~> 1.0"
gem "jrjackson"

# Repro:
require 'mongo'
require 'jrjackson'

id = BSON::ObjectId.new
JrJackson::Json.dump({_id: id})
@guyboertje
Copy link
Owner

@cheald - I hope that this issue, which I leave open for this exact problem, explains my position.

You could use a different adapter in multi_son. The standard Json gem has java extensions that are almost as good now days.

@cheald
Copy link
Author

cheald commented Sep 25, 2015

I actually saw the to_json_data check, but it's no good - it's checked after even to_json! to_h/to_a still get priority.

require 'mongo'
require 'jrjackson'

module ::BSON
  class ObjectId
    def to_json_data
      to_s
    end
  end
end

id = BSON::ObjectId.new
JrJackson::Json.dump({_id: id})

# => "{\"_id\":[86,5,143,203,47,144,169,173,39,236,44,187]}"

id.to_json_data
# => "56058fcb2f90a9ad27ec2cbb"

I see that you opened an issue to promote to_json_data which should fix the issue, though!

I needed to use not-the-standard JSON lib because of an issue like this one - it still doesn't properly handle invalid UTF-8, while JrJackson and Gson do. I ended up swapping to Gson and all is fine now, but I'd like to be able to use JrJackson, as well!

Is it fair to leave this ticket open until #43 is closed?

@guyboertje
Copy link
Owner

@cheald - I will leave this open. BTW, I created #43 exactly because I could not recommend using to_json_data to you as the call order was wrong.

@guyboertje
Copy link
Owner

fixed in v0.3.5

@cheald - FYI HTH

@cheald
Copy link
Author

cheald commented Oct 7, 2015

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants