{"id":25599266,"url":"https://github.com/aktsk/simple_json","last_synced_at":"2025-04-13T05:06:18.549Z","repository":{"id":56895776,"uuid":"397896097","full_name":"aktsk/simple_json","owner":"aktsk","description":"A simple \u0026 fast view solution for Rails JSON rendering.","archived":false,"fork":false,"pushed_at":"2023-07-24T02:06:21.000Z","size":52,"stargazers_count":17,"open_issues_count":2,"forks_count":2,"subscribers_count":65,"default_branch":"master","last_synced_at":"2025-04-13T05:06:13.072Z","etag":null,"topics":["jbuilder","rails","ruby","template-engine"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/simple_json","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aktsk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-19T09:59:15.000Z","updated_at":"2024-12-30T16:37:05.000Z","dependencies_parsed_at":"2024-08-05T18:35:23.962Z","dependency_job_id":null,"html_url":"https://github.com/aktsk/simple_json","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aktsk%2Fsimple_json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aktsk%2Fsimple_json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aktsk%2Fsimple_json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aktsk%2Fsimple_json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aktsk","download_url":"https://codeload.github.com/aktsk/simple_json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665748,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jbuilder","rails","ruby","template-engine"],"created_at":"2025-02-21T14:21:33.188Z","updated_at":"2025-04-13T05:06:18.530Z","avatar_url":"https://github.com/aktsk.png","language":"Ruby","readme":"# SimpleJson\nA simple \u0026 fast solution for Rails JSON rendering.\n\n## Get started\nIn Gemfile\n```ruby\ngem 'simple_json'\n```\n\nIn controller\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include SimpleJson::SimpleJsonRenderable\n\n  ...\nend\n```\n\nIn view, create your simple_json template file in `app/views`.\n\n```ruby\n# {controller_name}/{action_name}.simple_json.rb\n{\n  key1: @value1,\n  key2: helper(@value2),\n  key3: partial!(\"partial_path\", param1: param1, param2: param2)\n}\n```\n\nAnd, partial as well.\n\n```ruby\n-\u003e(param1:, param2:) {\n  {\n    key1: param1,\n    key2: param2,\n  }\n}\n\n```\n\nThat's all!\n\nHave fun!\n\n## Special thanks\nThis project is built on work of [jb](https://github.com/amatsuda/jb).\n\n## Template Syntax\nSimpleJson templates are simply lambda objects that return data(Hashes or Arrays) for json.\n```ruby\n-\u003e {\n  {\n    key: @value,\n  }\n}\n```\nWhen no parameters specified, `-\u003e {` and `}` can be omitted.\n```ruby\n{\n  key: @value,\n}\n```\n\nUse `partial!` method to call another template in template. Note that path is always required. Also, there is no difference between partials and templates, so that you cannot omit `_` before template name.\n(So, no omitting for template path is allowed.)\n\n```ruby\n# app/views/posts/show.simple_json.rb\n{\n  title: @post.title,\n  comments: @post.comments.map { |comment| partial!('comments/_comment', comment: comment) }\n}\n```\n\n```ruby\n# app/views/comments/_comment.simple_json.rb\n-\u003e(comment:) {\n  {\n    body: comment.body\n  }\n}\n```\n\nCache helpers of simple_json is similar to jbuilder.\n```ruby\ncache! key, options do\n  data_to_cache\nend\n```\n\nCache helpers uses `Rails.cache` to cache, so array keys, expirations are available. Make sure `perform_caching` is enabled.\n```ruby\ncache! [key1, key2], expires_in: 10.minutes do\n  data_to_cache\nend\n```\n\n`cache_if!` is also available\n```ruby\ncache_if! boolean, key1, options do\n  data_to_cache\nend\n```\n\nYou can set key_prefix for caching like this\n```ruby\nSimpleJson.cache_key_prefix = \"MY_PREFIX\"\n```\n\n## Configurations\nLoad all templates on boot. (For production)\nTemplates loaded will not load again, so it is not recommended in development environment.\n```ruby\n# config/environments/production.rb\nSimpleJson.enable_template_cache\n```\n\nThe default path for templates is `app/views`, you can change it by\n```ruby\nSimpleJson.template_paths.append(\"app/simple_jsons\")\n# or\nSimpleJson.template_paths=[\"app/views\", \"app/simple_jsons\"]\n```\nNote that these paths should not be eager loaded cause using .rb as suffix.\n\nSimpleJson uses Oj as json serializer by default. Modules with `#encode` and `#decode` method can be used here.\n```ruby\nSimpleJson.json_module = ActiveSupport::JSON\n```\n\n## The Generator\nSimpleJson extends the default Rails scaffold generator and adds some simple_json templates. If you don't need them, please configure like so.\n```rb\nRails.application.config.generators.simple_json false\n```\n\n## Benchmarks\nHere'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.\n\n### RAILS_ENV=development\n```\n% ./bin/benchmark.sh\n\n* Rendering 10 partials via render_partial\nWarming up --------------------------------------\n                  jb   257.000  i/100ms\n            jbuilder   108.000  i/100ms\n         simple_json     2.039k i/100ms\nCalculating -------------------------------------\n                  jb      2.611k (± 7.1%) i/s -     13.107k in   5.046110s\n            jbuilder      1.084k (± 3.5%) i/s -      5.508k in   5.088845s\n         simple_json     20.725k (± 4.4%) i/s -    103.989k in   5.026914s\n\nComparison:\n         simple_json:    20725.5 i/s\n                  jb:     2610.5 i/s - 7.94x  (± 0.00) slower\n            jbuilder:     1083.8 i/s - 19.12x  (± 0.00) slower\n\n\n* Rendering 100 partials via render_partial\nWarming up --------------------------------------\n                  jb    88.000  i/100ms\n            jbuilder    14.000  i/100ms\n         simple_json   290.000  i/100ms\nCalculating -------------------------------------\n                  jb    928.202  (± 5.0%) i/s -      4.664k in   5.037314s\n            jbuilder    137.980  (± 6.5%) i/s -    700.000  in   5.094658s\n         simple_json      2.931k (± 5.2%) i/s -     14.790k in   5.060707s\n\nComparison:\n         simple_json:     2931.1 i/s\n                  jb:      928.2 i/s - 3.16x  (± 0.00) slower\n            jbuilder:      138.0 i/s - 21.24x  (± 0.00) slower\n\n\n* Rendering 1000 partials via render_partial\nWarming up --------------------------------------\n                  jb    11.000  i/100ms\n            jbuilder     1.000  i/100ms\n         simple_json    29.000  i/100ms\nCalculating -------------------------------------\n                  jb    106.150  (± 5.7%) i/s -    539.000  in   5.094255s\n            jbuilder     13.012  (± 7.7%) i/s -     65.000  in   5.054016s\n         simple_json    271.683  (± 5.2%) i/s -      1.363k in   5.030646s\n\nComparison:\n         simple_json:      271.7 i/s\n                  jb:      106.1 i/s - 2.56x  (± 0.00) slower\n            jbuilder:       13.0 i/s - 20.88x  (± 0.00) slower\n```\n### RAILS_ENV=production\n```\n% RAILS_ENV=production ./bin/benchmark.sh\n\n* Rendering 10 partials via render_partial\nWarming up --------------------------------------\n                  jb   246.000  i/100ms\n            jbuilder    97.000  i/100ms\n         simple_json     1.957k i/100ms\nCalculating -------------------------------------\n                  jb      2.611k (± 4.1%) i/s -     13.038k in   5.002304s\n            jbuilder    972.031  (± 4.7%) i/s -      4.850k in   5.001200s\n         simple_json     20.383k (± 3.8%) i/s -    101.764k in   4.999989s\n\nComparison:\n         simple_json:    20382.8 i/s\n                  jb:     2611.3 i/s - 7.81x  (± 0.00) slower\n            jbuilder:      972.0 i/s - 20.97x  (± 0.00) slower\n\n\n* Rendering 100 partials via render_partial\nWarming up --------------------------------------\n                  jb    90.000  i/100ms\n            jbuilder    11.000  i/100ms\n         simple_json   280.000  i/100ms\nCalculating -------------------------------------\n                  jb    883.446  (± 4.8%) i/s -      4.410k in   5.003438s\n            jbuilder    119.932  (± 8.3%) i/s -    605.000  in   5.085382s\n         simple_json      2.886k (± 4.2%) i/s -     14.560k in   5.054327s\n\nComparison:\n         simple_json:     2885.7 i/s\n                  jb:      883.4 i/s - 3.27x  (± 0.00) slower\n            jbuilder:      119.9 i/s - 24.06x  (± 0.00) slower\n\n\n* Rendering 1000 partials via render_partial\nWarming up --------------------------------------\n                  jb    12.000  i/100ms\n            jbuilder     1.000  i/100ms\n         simple_json    32.000  i/100ms\nCalculating -------------------------------------\n                  jb    124.627  (± 4.8%) i/s -    624.000  in   5.018515s\n            jbuilder     12.710  (± 7.9%) i/s -     64.000  in   5.073018s\n         simple_json    314.896  (± 3.2%) i/s -      1.600k in   5.086509s\n\nComparison:\n         simple_json:      314.9 i/s\n                  jb:      124.6 i/s - 2.53x  (± 0.00) slower\n            jbuilder:       12.7 i/s - 24.78x  (± 0.00) slower\n```\n\n## Migrating from Jbuilder\nWhen migrating from Jbuilder, you can include `Migratable` in controller for migrating mode.\n```\ninclude SimpleJson::SimpleJsonRenderable\ninclude SimpleJson::Migratable\n```\n\nIn migrating mode\n- Comparision will be performed for simple_json and ActionView render(Jbuilder) result.\n- simple_json partials not found will use Jbuilder partial as an alternative.\n\nNote that render will be performed twice, so using it in production mode is not recommended.\n\n## Contributing\n\nPull requests are welcome on GitHub at https://github.com/aktsk/simple_json.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faktsk%2Fsimple_json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faktsk%2Fsimple_json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faktsk%2Fsimple_json/lists"}