Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save royratcliffe/1b42ac346538a531fe8ef42cd7c22e64 to your computer and use it in GitHub Desktop.
Save royratcliffe/1b42ac346538a531fe8ef42cd7c22e64 to your computer and use it in GitHub Desktop.
Pretty JSON encoding for Active Support
require 'active_support/json/encoding'
module ActiveSupport
module JSON
module Encoding
class JSONGemEncoder
# Replaces the JSON gem encoder's string-ify method with a
# pretty-generator implementation. Just clobbers the original method,
# so makes assumptions about method name, arguments and
# implementation details. That makes it Rails version dependent.
# Useful just for development only.
def stringify jsonified
::JSON.pretty_generate jsonified, quirks_mode: true, max_nesting: false
end
end
end
end
end if Rails.env.development? && Gem::Dependency.new('rails', '~> 5.0.2').match?('rails', Rails.version)
@royratcliffe
Copy link
Author

For Rails applications, put this in config/initializers.

@ZzZombo
Copy link

ZzZombo commented Dec 16, 2021

You are a life savior! FYI, it still works fine in Rails 6.1.4.4.

@royratcliffe
Copy link
Author

Cheers ZzZombo, glad to know it still works.

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