From f40d7ba42c313cc459658a6f9eed368b22bf2925 Mon Sep 17 00:00:00 2001 From: fumihumi Date: Sun, 4 May 2025 21:49:23 +0900 Subject: [PATCH 1/4] format readme --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 673fb55..a6c6c7f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,17 @@ # SimpleJson + A simple & fast solution for Rails JSON rendering. ## Get started + In Gemfile + ```ruby gem 'simple_json' ``` In controller + ```ruby class ApplicationController < ActionController::Base include SimpleJson::SimpleJsonRenderable @@ -44,10 +48,13 @@ That's all! Have fun! ## Special thanks + This project is built on work of [jb](https://github.com/amatsuda/jb). ## Template Syntax + SimpleJson templates are simply lambda objects that return data(Hashes or Arrays) for json. + ```ruby -> { { @@ -55,7 +62,9 @@ SimpleJson templates are simply lambda objects that return data(Hashes or Arrays } } ``` + When no parameters specified, `-> {` and `}` can be omitted. + ```ruby { key: @value, @@ -83,6 +92,7 @@ Use `partial!` method to call another template in template. Note that path is al ``` Cache helpers of simple_json is similar to jbuilder. + ```ruby cache! key, options do data_to_cache @@ -90,6 +100,7 @@ end ``` Cache helpers uses `Rails.cache` to cache, so array keys, expirations are available. Make sure `perform_caching` is enabled. + ```ruby cache! [key1, key2], expires_in: 10.minutes do data_to_cache @@ -97,6 +108,7 @@ end ``` `cache_if!` is also available + ```ruby cache_if! boolean, key1, options do data_to_cache @@ -104,41 +116,51 @@ end ``` You can set key_prefix for caching like this + ```ruby SimpleJson.cache_key_prefix = "MY_PREFIX" ``` ## Configurations + Load all templates on boot. (For production) Templates loaded will not load again, so it is not recommended in development environment. + ```ruby # config/environments/production.rb SimpleJson.enable_template_cache ``` The default path for templates is `app/views`, you can change it by + ```ruby SimpleJson.template_paths.append("app/simple_jsons") # or 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. + ```ruby SimpleJson.json_module = ActiveSupport::JSON ``` ## The Generator + SimpleJson extends the default Rails scaffold generator and adds some simple_json templates. If you don't need them, please configure like so. + ```rb Rails.application.config.generators.simple_json false ``` ## Benchmarks + Here're the results of a benchmark (which you can find [here](https://github.com/aktsk/simple_json/blob/master/test/dummy_app/app/controllers/benchmarks_controller.rb) in this repo) rendering a collection to JSON. ### RAILS_ENV=development + ``` % ./bin/benchmark.sh @@ -189,7 +211,9 @@ Comparison: jb: 106.1 i/s - 2.56x (± 0.00) slower jbuilder: 13.0 i/s - 20.88x (± 0.00) slower ``` + ### RAILS_ENV=production + ``` % RAILS_ENV=production ./bin/benchmark.sh @@ -242,13 +266,16 @@ Comparison: ``` ## Migrating from Jbuilder + When migrating from Jbuilder, you can include `Migratable` in controller for migrating mode. + ``` include SimpleJson::SimpleJsonRenderable include SimpleJson::Migratable ``` In migrating mode + - Comparision will be performed for simple_json and ActionView render(Jbuilder) result. - simple_json partials not found will use Jbuilder partial as an alternative. From e29f1a90f19ed4b9a8d10b12cebf007adbb066b7 Mon Sep 17 00:00:00 2001 From: fumihumi Date: Sun, 4 May 2025 21:49:57 +0900 Subject: [PATCH 2/4] bump development dependency gems * update rails to 8.0 * Modify the settings in config/environments/test (the setting for action_dispatch.show_exceptions has changed in Rails 7.2). * Change debugger gem 'byebug' to 'debug' --- simple_json.gemspec | 5 +++-- test/dummy_app/config/environments/test.rb | 2 +- test/test_helper.rb | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/simple_json.gemspec b/simple_json.gemspec index 10fa57c..2cae41a 100644 --- a/simple_json.gemspec +++ b/simple_json.gemspec @@ -24,11 +24,12 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'action_args' spec.add_development_dependency 'bundler' - spec.add_development_dependency 'byebug' - spec.add_development_dependency 'rails' + spec.add_development_dependency 'debug' + spec.add_development_dependency 'rails', '~> 8.0' spec.add_development_dependency 'rake' spec.add_development_dependency 'selenium-webdriver' spec.add_development_dependency 'test-unit-rails' + spec.add_development_dependency 'mutex_m' spec.required_ruby_version = '>= 2.5.0' end diff --git a/test/dummy_app/config/environments/test.rb b/test/dummy_app/config/environments/test.rb index b1d2da7..d8da507 100644 --- a/test/dummy_app/config/environments/test.rb +++ b/test/dummy_app/config/environments/test.rb @@ -19,7 +19,7 @@ config.action_controller.perform_caching = false # Raise exceptions instead of rendering exception templates. - config.action_dispatch.show_exceptions = false + config.action_dispatch.show_exceptions = :none # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false diff --git a/test/test_helper.rb b/test/test_helper.rb index 3fea8a6..3fdfcbd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,5 +8,5 @@ Bundler.require require 'action_args' -require 'byebug' +require 'debug' require 'test/unit/rails/test_help' From 8f25c1b265f236636a3836eeedce40dea1260345 Mon Sep 17 00:00:00 2001 From: fumihumi Date: Sun, 4 May 2025 22:17:11 +0900 Subject: [PATCH 3/4] Fix the broken bin/benchmark.sh --- .../app/controllers/benchmarks_controller.rb | 18 ++++++++++++++++-- ...son.simple_json.rb => index.simple_json.rb} | 0 test/dummy_app/bin/bench.rb | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) rename test/dummy_app/app/views/benchmarks/{index_simple_json.simple_json.rb => index.simple_json.rb} (100%) diff --git a/test/dummy_app/app/controllers/benchmarks_controller.rb b/test/dummy_app/app/controllers/benchmarks_controller.rb index f7b0761..d5a2cca 100644 --- a/test/dummy_app/app/controllers/benchmarks_controller.rb +++ b/test/dummy_app/app/controllers/benchmarks_controller.rb @@ -8,14 +8,28 @@ def index(n = '100') jb = render_to_string 'index_jb' jbuilder = render_to_string 'index_jbuilder' - simple_json = render_to_string 'index_simple_json' + + SimpleJson.json_module = SimpleJson::Json::Oj + simple_json = render_to_string 'index' + + SimpleJson.json_module = ActiveSupport::JSON + simple_json_active_support_json = render_to_string 'index' + raise 'jb != jbuilder' unless jb == jbuilder raise 'simple_json != jbuilder' unless simple_json == jbuilder + raise 'simple_json_active_support_json != jbuilder' unless simple_json_active_support_json == jbuilder result = Benchmark.ips do |x| x.report('jb') { render_to_string 'index_jb' } x.report('jbuilder') { render_to_string 'index_jbuilder' } - x.report('simple_json') { render_to_string 'index_simple_json' } + x.report('simple_json(oj)') { + SimpleJson.json_module = SimpleJson::Json::Oj + render_to_string 'index' + } + x.report('simple_json(AS::json)') { + SimpleJson.json_module = ActiveSupport::JSON + render_to_string 'index' + } x.compare! end render plain: result.data.to_s diff --git a/test/dummy_app/app/views/benchmarks/index_simple_json.simple_json.rb b/test/dummy_app/app/views/benchmarks/index.simple_json.rb similarity index 100% rename from test/dummy_app/app/views/benchmarks/index_simple_json.simple_json.rb rename to test/dummy_app/app/views/benchmarks/index.simple_json.rb diff --git a/test/dummy_app/bin/bench.rb b/test/dummy_app/bin/bench.rb index f99419b..d795a88 100644 --- a/test/dummy_app/bin/bench.rb +++ b/test/dummy_app/bin/bench.rb @@ -2,6 +2,13 @@ require 'action_dispatch/testing/integration' +puts 'SimpleJson Benchmark' +puts "ruby: #{RUBY_VERSION}" +puts "rails: #{Rails.version}" +puts "json: #{JSON::VERSION}" +puts "oj: #{Oj::VERSION}" +puts "----------------------" + puts '* Rendering 10 partials via render_partial' ActionDispatch::Integration::Session.new(Rails.application).get '/benchmarks.json?n=10' From 213a821acde1c4fc2d4dacf1e9c68fa996ef3ab8 Mon Sep 17 00:00:00 2001 From: fumihumi Date: Sun, 4 May 2025 22:46:37 +0900 Subject: [PATCH 4/4] CI: fix matrix specs --- .github/workflows/main.yml | 44 +++---------------- gemfiles/rails_42.gemfile | 9 ---- gemfiles/rails_50.gemfile | 8 ---- gemfiles/rails_52.gemfile | 8 ---- .../{rails_60.gemfile => rails_7.1.gemfile} | 2 +- .../{rails_61.gemfile => rails_7.2.gemfile} | 2 +- .../{rails_51.gemfile => rails_8.0.gemfile} | 3 +- simple_json.gemspec | 2 +- 8 files changed, 10 insertions(+), 68 deletions(-) delete mode 100644 gemfiles/rails_42.gemfile delete mode 100644 gemfiles/rails_50.gemfile delete mode 100644 gemfiles/rails_52.gemfile rename gemfiles/{rails_60.gemfile => rails_7.1.gemfile} (83%) rename gemfiles/{rails_61.gemfile => rails_7.2.gemfile} (83%) rename gemfiles/{rails_51.gemfile => rails_8.0.gemfile} (66%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4d6ef44..72500a0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,52 +6,18 @@ jobs: build: strategy: matrix: - ruby_version: ['3.0', '2.7', '2.6'] - - gemfile: - - gemfiles/rails_61.gemfile - - gemfiles/rails_60.gemfile - - include: - - ruby_version: ruby-head - gemfile: gemfiles/rails_edge.gemfile - allow_failures: 'true' - - ruby_version: '3.0' - gemfile: gemfiles/rails_edge.gemfile - allow_failures: 'true' - - - ruby_version: '2.7' - gemfile: gemfiles/rails_edge.gemfile - allow_failures: 'true' - - - ruby_version: '2.6' - gemfile: gemfiles/rails_52.gemfile - - ruby_version: '2.6' - gemfile: gemfiles/rails_51.gemfile - - ruby_version: '2.6' - gemfile: gemfiles/rails_50.gemfile - - ruby_version: '2.6' - gemfile: gemfiles/rails_42.gemfile - bundler_version: '1' - - - ruby_version: '2.5' - gemfile: gemfiles/rails_52.gemfile - - ruby_version: '2.5' - gemfile: gemfiles/rails_42.gemfile - bundler_version: '1' + ruby: ['3.2', '3.3', '3.4'] + rails: ["7.1", "7.2", "8.0"] runs-on: ubuntu-latest env: - BUNDLE_GEMFILE: ${{ matrix.gemfile }} + BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: - ruby-version: ${{ matrix.ruby_version }} - bundler: ${{ matrix.bundler_version }} + ruby-version: ${{ matrix.ruby }} bundler-cache: true - continue-on-error: ${{ matrix.allow_failures == 'true' }} - run: bundle exec rake - continue-on-error: ${{ matrix.allow_failures == 'true' }} diff --git a/gemfiles/rails_42.gemfile b/gemfiles/rails_42.gemfile deleted file mode 100644 index 58018ef..0000000 --- a/gemfiles/rails_42.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -gemspec path: '../' - -gem 'jbuilder' -gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7' -gem 'rails', '~> 4.2.0' diff --git a/gemfiles/rails_50.gemfile b/gemfiles/rails_50.gemfile deleted file mode 100644 index 5b3e5a7..0000000 --- a/gemfiles/rails_50.gemfile +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -gemspec path: '../' - -gem 'jbuilder' -gem 'rails', '~> 5.0.0' diff --git a/gemfiles/rails_52.gemfile b/gemfiles/rails_52.gemfile deleted file mode 100644 index 1b91b90..0000000 --- a/gemfiles/rails_52.gemfile +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -gemspec path: '../' - -gem 'jbuilder' -gem 'rails', '~> 5.2.0' diff --git a/gemfiles/rails_60.gemfile b/gemfiles/rails_7.1.gemfile similarity index 83% rename from gemfiles/rails_60.gemfile rename to gemfiles/rails_7.1.gemfile index 1975ecc..021c42f 100644 --- a/gemfiles/rails_60.gemfile +++ b/gemfiles/rails_7.1.gemfile @@ -5,5 +5,5 @@ source 'https://rubygems.org' gemspec path: '../' gem 'jbuilder' -gem 'rails', '~> 6.0.0' +gem 'rails', '~> 7.1.0' gem 'selenium-webdriver' diff --git a/gemfiles/rails_61.gemfile b/gemfiles/rails_7.2.gemfile similarity index 83% rename from gemfiles/rails_61.gemfile rename to gemfiles/rails_7.2.gemfile index 51bf378..d7339d9 100644 --- a/gemfiles/rails_61.gemfile +++ b/gemfiles/rails_7.2.gemfile @@ -5,5 +5,5 @@ source 'https://rubygems.org' gemspec path: '../' gem 'jbuilder' -gem 'rails', '~> 6.1.0' +gem 'rails', '~> 7.2.0' gem 'selenium-webdriver' diff --git a/gemfiles/rails_51.gemfile b/gemfiles/rails_8.0.gemfile similarity index 66% rename from gemfiles/rails_51.gemfile rename to gemfiles/rails_8.0.gemfile index b4ed4c5..718c4af 100644 --- a/gemfiles/rails_51.gemfile +++ b/gemfiles/rails_8.0.gemfile @@ -5,4 +5,5 @@ source 'https://rubygems.org' gemspec path: '../' gem 'jbuilder' -gem 'rails', '~> 5.1.0' +gem 'rails', '~> 8.0.0' +gem 'selenium-webdriver' diff --git a/simple_json.gemspec b/simple_json.gemspec index 2cae41a..c496ef4 100644 --- a/simple_json.gemspec +++ b/simple_json.gemspec @@ -31,5 +31,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'test-unit-rails' spec.add_development_dependency 'mutex_m' - spec.required_ruby_version = '>= 2.5.0' + spec.required_ruby_version = '>= 3.2.0' end