Browse Source

[ruby|rack-sequel] Use OJ for faster json serialization. (#9147)

Petrik de Heus 1 year ago
parent
commit
bd015859fa
2 changed files with 8 additions and 4 deletions
  1. 1 0
      frameworks/Ruby/rack-sequel/Gemfile
  2. 7 4
      frameworks/Ruby/rack-sequel/hello_world.rb

+ 1 - 0
frameworks/Ruby/rack-sequel/Gemfile

@@ -1,6 +1,7 @@
 source 'https://rubygems.org'
 
 gem 'json', '~> 2.0'
+gem 'oj', '~> 3.14', platforms: %i[ruby mswin]
 gem 'passenger', '~> 6.0', :platforms=>[:ruby, :mswin], :require=>false
 gem 'puma', '~> 6.4', :require=>false
 gem 'sequel', '~> 5.0'

+ 7 - 4
frameworks/Ruby/rack-sequel/hello_world.rb

@@ -1,5 +1,8 @@
 # frozen_string_literal: true
 
+require 'oj'
+Oj.mimic_JSON
+
 # Our Rack application to be executed by rackup
 class HelloWorld
   DEFAULT_HEADERS = {}.tap do |h|
@@ -91,19 +94,19 @@ class HelloWorld
       case env['PATH_INFO']
       when '/json'
         # Test type 1: JSON serialization
-        [JSON_TYPE, JSON.fast_generate(message: 'Hello, World!')]
+        [JSON_TYPE, { message: 'Hello, World!' }.to_json]
       when '/db'
         # Test type 2: Single database query
-        [JSON_TYPE, JSON.fast_generate(db)]
+        [JSON_TYPE, db.to_json]
       when '/queries'
         # Test type 3: Multiple database queries
-        [JSON_TYPE, JSON.fast_generate(queries(env))]
+        [JSON_TYPE, queries(env).to_json]
       when '/fortunes'
         # Test type 4: Fortunes
         [HTML_TYPE, fortunes]
       when '/updates'
         # Test type 5: Database updates
-        [JSON_TYPE, JSON.fast_generate(updates(env))]
+        [JSON_TYPE, updates(env).to_json]
       when '/plaintext'
         # Test type 6: Plaintext
         [PLAINTEXT_TYPE, 'Hello, World!']