Skip to content

Commit 9391850

Browse files
authored
[ruby/roda] Use rapidjson for faster JSON serialization (#9164)
1 parent d2c0d69 commit 9391850

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

frameworks/Ruby/roda-sequel/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ gem "sequel", "~> 5.67"
77
gem "roda", "~> 3.66"
88
gem "tilt", "~> 2.1", require: "tilt/erb"
99
gem "unicorn", "~> 6.1", platforms: %i[ruby mswin], require: false
10-
gem "oj", "~> 3.14"
10+
gem "rapidjson"
1111

1212
group :mysql do
1313
gem "mysql2", "~> 0.5", platforms: %i[ruby mswin]

frameworks/Ruby/roda-sequel/boot.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# frozen_string_literal: true
22
require "bundler/setup"
33
require "time"
4-
require "oj"
4+
require "rapidjson"
55
MAX_PK = 10_000
66
QUERY_RANGE = (1..MAX_PK).freeze
77
ALL_IDS = QUERY_RANGE.to_a
88
QUERIES_MIN = 1
99
QUERIES_MAX = 500
1010
SEQUEL_NO_ASSOCIATIONS = true
1111

12-
# Use the OJ gem instead of the JSON one
13-
Oj.mimic_JSON()
14-
1512
SERVER_STRING =
1613
if defined?(PhusionPassenger)
1714
[

frameworks/Ruby/roda-sequel/hello_world.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def rand1
2222
# Test type 1: JSON serialization
2323
r.is "json" do
2424
response[CONTENT_TYPE] = JSON_TYPE
25-
{ message: "Hello, World!" }.to_json
25+
RapidJSON.encode({ message: "Hello, World!" })
2626
end
2727

2828
# Test type 2: Single database query
2929
r.is "db" do
3030
response[CONTENT_TYPE] = JSON_TYPE
31-
World.with_pk(rand1).values.to_json
31+
RapidJSON.encode(World.with_pk(rand1).values)
3232
end
3333

3434
# Test type 3: Multiple database queries
@@ -40,7 +40,7 @@ def rand1
4040
World.with_pk(id).values
4141
end
4242
end
43-
worlds.to_json
43+
RapidJSON.encode(worlds)
4444
end
4545

4646
# Test type 4: Fortunes
@@ -70,7 +70,7 @@ def rand1
7070
end
7171
World.batch_update(worlds)
7272
end
73-
worlds.map(&:values).to_json
73+
RapidJSON.encode(worlds.map!(&:values))
7474
end
7575

7676
# Test type 6: Plaintext

0 commit comments

Comments
 (0)