I remember on an old 2.x version of OJ, I had to do
module ActiveSupport::JSON::Encoding
class Oj < JSONGemEncoder
def encode value
::Oj.dump(value.as_json, options.dup)
end
end
end
ActiveSupport.json_encoder = ActiveSupport::JSON::Encoding::Oj
Oj.default_options = { mode: :compat }
To get OJ working with ActiveModel::Serializers (as the AMS gem simply hooks into render json: User.first).
In 3.0 - is this still something I need to do? I've added the Oj.optimize_rails() call to an initializer, but after binding into the OJ Gem i'm not seeing anything catch when running my test suite.
Happy to add something to the Rails readme once I figure this out!
Just calling Oj.optimize_rails should be enough. What makes you think it is not working? Have you done a trace?
Yes - I ran a trace (against an endpoint that uses AMS) - I can't see any calls to the Oj Gem. I'm seeing:
#0:/Users/hhff/.rvm/gems/ruby-2.3.1/gems/active_model_serializers-0.10.6/lib/active_model_serializers/serializable_resource.rb:36:ActiveModelSerializers::SerializableResource:<: end
#0:/Users/hhff/.rvm/gems/ruby-2.3.1/gems/activesupport-5.1.1/lib/active_support/core_ext/object/json.rb:33:ActiveSupport::ToJsonWithActiveSupportEncoder:>: def to_json(options = nil)
But no mention of OJ. (Thankyou for this great Gem BTW!)
Glad you like it. Looks like I'll have to dig into the ActiveSupport code and figure out what has changed.
Can you give me a longer trace. That would help me figure out where to look.
Even better would be a simple test case and I'll trace it through ActiveSupport.
On it!
On Fri, Sep 1, 2017 at 11:27 AM Peter Ohler notifications@github.com
wrote:
Even better would be a simple test case and I'll trace it through
ActiveSupport.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ohler55/oj/issues/430#issuecomment-326610383, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACu4_FLUwrDjjVz5a96mtdPlW2wVgB0Aks5seCJNgaJpZM4PKQ-N
.
Thanks. We will get to the bottom of this and then update the docs.
Hi @ohler55 !
You can clone a repro here: https://github.com/hhff/tracerr
This is just a vanilla Rails 5.1.2 app with a single resource, serializer & test. Commits should explain the additions to the rails boilerplate: https://github.com/hhff/tracerr/commits/master
bin/rails db:migrate RAILS_ENV=test
rake test
And you should see a trace in your console. Searching oj in stack trace won't yield anything from the OJ gem, but you should see calls to active_model_serializers. Lemme know if this helps!
Still looking but I have a theory. So I put in a puts in active_support/json/encoding.rb. It shows self.encode is called and that the Encoding.json_encoder is Oj::Rails::Encoder which is correct. I'm guessing that the trace stops there because it drops into the C extension. Will add a print to Oj in C and see if it shows up.
Ok! Sounds promising!
On Fri, Sep 1, 2017 at 1:10 PM Peter Ohler notifications@github.com wrote:
Still looking but I have a theory. So I put in a puts in
active_support/json/encoding.rb. It shows self.encode is called and that
the Encoding.json_encoder is Oj::Rails::Encoder which is correct. I'm
guessing that the trace stops there because it drops into the C extension.
Will add a print to Oj in C and see if it shows up.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ohler55/oj/issues/430#issuecomment-326634961, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACu4_NAnYkBNfBKbrULlo4lmD1ISNb3Zks5seDp4gaJpZM4PKQ-N
.
Oj is being called. Now how can this be confirmed is another story. Maybe a flag is needed to trace or at least indicate that Oj was called. Let me think about that.
That would be great! It's almost too magic and I think most implementers will get an uneasy feeling (like I did) a spend a few hours trying to prove that it's in place!
Thankyou @ohler55 !
A follow up, Oj is being called and I am surprises the trace does not show entering a C call. Seems like a bug in the tracer. Once in the Oj encode function Oj does check to see if the object is a special one or if the to_json call should be made. It looks like the to_json is being called on some part of the object if not the object itself. Once I add some tracing that should show up as well. Armed with that info a few more classes can be added to the optimized list for some performance gains.
Here is the trick!
Tracer.display_c_call = true
I think that is all that is needed. That and an update to the Rails.md file.
Most helpful comment
Here is the trick!
I think that is all that is needed. That and an update to the Rails.md file.