blueprinter icon indicating copy to clipboard operation
blueprinter copied to clipboard

Oj does not work after version 3.13

Open sdrioux opened this issue 1 year ago • 7 comments

While this library technically supports Oj, it only supports using a very outdated version: 3.13, which is missing almost 3 years of development: https://github.com/ohler55/oj/blob/develop/CHANGELOG.md#3163---2023-12-11.

Users technically can use a newer version, however, will not always mimic the JSON gem because Blueprinter is not passing a mode of :rails when calling Oj.generate.

See the issue below for details:

https://github.com/ohler55/oj/issues/658

sdrioux avatar Jan 22 '24 18:01 sdrioux

Does Oj allow setting a global default mode? That would be a good place to look at to fix this issue. The only other way I see forward with this is to allow a json_options configuration which allows passing options to the JSON generator method, but it does seem like we're overdoing the JSON bit here. Ideally, the application(rails - Oj/YAJL, etc) should take care of what JSON library is being used underneath rather than spreading that concern to util libraries like Blueprinter in this case.

ritikesh avatar Jan 23 '24 19:01 ritikesh

I dug into this a little more, and the problem seems to be that Blueprinter calls Oj.generate at all.

[18] pry(main)> Time.zone.now.as_json
=> "2024-01-23T16:57:41.954-05:00"

[19] pry(main)> JSON.generate(Time.zone.now)
=> "\"2024-01-23 16:57:57 -0500\""

If I am just using the native JSON library on an object it is calling as_json under the hood, not JSON.generate, which retains the milliseconds of each DateTime:

my_object.to_json

"{\"id\":1,\"email\":\"[email protected]\",\"created_at\":\"2024-01-22T13:26:55.101-05:00\",\"updated_at\":\"2024-01-22T13:26:55.101-05:00\"}"

sdrioux avatar Jan 23 '24 21:01 sdrioux

You can set the default configuration for this in config/application.rb or an initializer file to fix this -

ActiveSupport::JSON::Encoding.time_precision = 0

Before -

=> {a: Time.now.utc}.as_json
=> {"a"=>"2024-01-24T05:16:22.970Z"}

After -

=> {a: Time.now.utc}.as_json
=> {"a"=>"2024-01-24T05:17:09Z"}

ritikesh avatar Jan 24 '24 05:01 ritikesh

Interesting, I didn't know you could do that, but thats going the opposite direction - I dont want to configure JSON to behave like Oj - I'd like to configure Oj to behave like JSON - the way that it does when using version 3.13.

sdrioux avatar Jan 24 '24 17:01 sdrioux

but isn't JSON format setting a more application level setting? 🤔 I'm assuming Oj might have just gone about respecting the default settings that other libraries like the native JSON itself adheres to.

ritikesh avatar Jan 25 '24 06:01 ritikesh

.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/blueprinter-1.0.1/lib/blueprinter/configuration.rb:44: warning:  
 Oj::Rails.mimic_JSON was called implicitly. Call it explicitly beforehand if you want to remove this warning.

Seems like related to the discussion. And the warning may be related due to it depends on outdated library (3 years ago). Is there plan to handle this warning for contributors, or there's known issues why it hasn't been done for years?

zhisme avatar Jan 26 '24 16:01 zhisme

@zhisme we're open to contributions 🙂

njbbaer avatar Jan 29 '24 23:01 njbbaer

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Mar 30 '24 01:03 github-actions[bot]