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

How to make Oj optional #117

Open
ybiquitous opened this issue Jan 27, 2021 · 7 comments
Open

How to make Oj optional #117

ybiquitous opened this issue Jan 27, 2021 · 7 comments

Comments

@ybiquitous
Copy link

Hello. I have some reason not to be able to use Oj, so is there a way to use another JSON library like the standard library JSON instead of Oj?

When I look into the code, it seems hard to replace Ougai::Serializer.for_json with some serializer without any monkey-patch.

@serializer = Ougai::Serializer.for_json

If we could make Oj an optional dependency, it would be ideal for me.

spec.add_dependency "oj", "~> 3.10"

I would appreciate it if you would consider it. Thanks.

@tilfin
Copy link
Owner

tilfin commented Jan 27, 2021

I use Oj instead of standard JSON for two reasons.

First, it's faster.
Second, it doesn't fail even when garbled string is mixed in values of structured data.

I actually fell into this trap when logging info entered by users in mobile apps.
If you still intend to deal with this issue, I will consider implementation.

Thanks.

@ybiquitous
Copy link
Author

Thank you so much for your quick response!

I fully understand the benefits of Oj, but I think making the Ougai serializer pluggable will be very useful for this gem's fan.
(especially, users like me who cannot use Oj for some reason 😅 )

I'd appreciate it so much if you could proceed with this issue.

@tilfin
Copy link
Owner

tilfin commented Jan 27, 2021

I understood it.

I think that your purpose can be achieved by the following code.

require 'rubygems'
require 'ougai'
require 'json'

class StandardJSONSerializer < Ougai::Serializer
  def serialize(data)
    JSON.generate(data)
  end
end

module Ougai
  class Serializer
    def self.for_json
      StandardJSONSerializer.new
    end
  end
end

logger = Ougai::Logger.new(STDOUT)
logger.info('log')

@Freaky
Copy link

Freaky commented Jan 31, 2021

I'd suggest using multi_json, like many other gems.

@tilfin
Copy link
Owner

tilfin commented Feb 20, 2021

I decided that Ougai should not use multi_json from now based on the following links.

I want to keep Oj and JrJackson as default, but allow users to change their custom serializer.

@ybiquitous
Copy link
Author

Sounds good. How will not Ougai install Oj if a user wants to install another JSON library?
(in other words, will Oj always be installed?)

@tilfin
Copy link
Owner

tilfin commented Feb 24, 2021

Ougai have already been used by a considerable number of applications, so I'm carefully considering how to make the users install OJ(JrJackson) separately.

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

No branches or pull requests

3 participants