From 714e5d48dccfb5195019ee4f0ceb1059527e3504 Mon Sep 17 00:00:00 2001 From: fumihumi Date: Wed, 7 May 2025 10:47:23 +0900 Subject: [PATCH] Change default_json_module: 'oj' to 'json' --- README.md | 23 ++++++++++++++++--- lib/simple_json.rb | 3 +-- simple_json.gemspec | 2 +- .../config/initializers}/simple_json/json.rb | 0 4 files changed, 22 insertions(+), 6 deletions(-) rename {lib => test/dummy_app/config/initializers}/simple_json/json.rb (100%) diff --git a/README.md b/README.md index a6c6c7f..c3c0a53 100644 --- a/README.md +++ b/README.md @@ -141,10 +141,27 @@ SimpleJson.template_paths=["app/views", "app/simple_jsons"] Note that these paths should not be eager loaded cause using .rb as suffix. -SimpleJson uses Oj as json serializer by default. Modules with `#encode` and `#decode` method can be used here. +SimpleJson uses ActiveSupport::JSON as the default JSON serializer. If you want to change to a module that has `#encode` and `#decode` methods, such as the Oj gem, you can do so as follows. ```ruby -SimpleJson.json_module = ActiveSupport::JSON + +# define Custom Json class +# ex. config/initializers/simple_json/json.rb +module SimpleJson + module Json + class Oj + def self.encode(json) + ::Oj.dump(json, mode: :rails) + end + + def self.decode(json_string) + ::Oj.load(json_string, mode: :rails) + end + end + end +end + +SimpleJson.json_module = SimpleJson::Json::Oj ``` ## The Generator @@ -283,7 +300,7 @@ Note that render will be performed twice, so using it in production mode is not ## Contributing -Pull requests are welcome on GitHub at https://github.com/aktsk/simple_json. +Pull requests are welcome on GitHub at . ## License diff --git a/lib/simple_json.rb b/lib/simple_json.rb index f95ddbd..5f5894e 100644 --- a/lib/simple_json.rb +++ b/lib/simple_json.rb @@ -8,14 +8,13 @@ require 'simple_json/migratable' require 'simple_json/simple_json_renderer_for_migration' -require 'simple_json/json' module SimpleJson @config = { cache_enabled: false, template_paths: ['app/views'], cache_key_prefix: 'simple_json/views', - default_json_module: Json::Oj + default_json_module: ActiveSupport::JSON } class << self diff --git a/simple_json.gemspec b/simple_json.gemspec index c496ef4..3ca5f23 100644 --- a/simple_json.gemspec +++ b/simple_json.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'oj', '~> 3.13' + spec.add_development_dependency 'oj' spec.add_development_dependency 'action_args' spec.add_development_dependency 'bundler' diff --git a/lib/simple_json/json.rb b/test/dummy_app/config/initializers/simple_json/json.rb similarity index 100% rename from lib/simple_json/json.rb rename to test/dummy_app/config/initializers/simple_json/json.rb