Skip to content

Commit 6647f84

Browse files
committed
Ruby: specify UTF-8 encoding for file IO
1 parent 00c2d8f commit 6647f84

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

server.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
server = WEBrick::HTTPServer.new Port: port, DocumentRoot: root
2020

2121
server.mount_proc '/api/comments' do |req, res|
22-
comments = JSON.parse(File.read('./comments.json'))
22+
comments = JSON.parse(File.read('./comments.json', encoding: 'UTF-8'))
2323

2424
if req.request_method == 'POST'
2525
# Assume it's well formed
@@ -28,7 +28,11 @@
2828
comment[key] = value.force_encoding('UTF-8')
2929
end
3030
comments << comment
31-
File.write('./comments.json', JSON.pretty_generate(comments, indent: ' '))
31+
File.write(
32+
'./comments.json',
33+
JSON.pretty_generate(comments, indent: ' '),
34+
encoding: 'UTF-8'
35+
)
3236
end
3337

3438
# always return json

0 commit comments

Comments
 (0)