Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Commit

Permalink
Ruby: specify UTF-8 encoding for file IO
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Sep 29, 2015
1 parent 00c2d8f commit 6647f84
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
server = WEBrick::HTTPServer.new Port: port, DocumentRoot: root

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

if req.request_method == 'POST'
# Assume it's well formed
Expand All @@ -28,7 +28,11 @@
comment[key] = value.force_encoding('UTF-8')
end
comments << comment
File.write('./comments.json', JSON.pretty_generate(comments, indent: ' '))
File.write(
'./comments.json',
JSON.pretty_generate(comments, indent: ' '),
encoding: 'UTF-8'
)
end

# always return json
Expand Down

0 comments on commit 6647f84

Please sign in to comment.