Skip to content

Commit

Permalink
Format JSON consistently (pretty print) when writing to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Feb 6, 2015
1 parent ce16398 commit be57a92
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions _comments.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"author": "Pete Hunt",
"text": "Hey there!"
}
{
"author": "Pete Hunt",
"text": "Hey there!"
}
]
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ app.post('/comments.json', function(req, res) {
fs.readFile('_comments.json', function(err, data) {
var comments = JSON.parse(data);
comments.push(req.body);
fs.writeFile('_comments.json', JSON.stringify(comments), function(err) {
fs.writeFile('_comments.json', JSON.stringify(comments, null, 4), function(err) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(comments));
});
Expand Down
2 changes: 1 addition & 1 deletion server.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function routeRequest()
$commentsDecoded[] = ['author' => $_POST['author'],
'text' => $_POST['text']];

$comments = json_encode($commentsDecoded);
$comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT);
file_put_contents('_comments.json', $comments);
}
header('Content-Type: application/json');
Expand Down
2 changes: 1 addition & 1 deletion server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
if req.request_method == 'POST'
# Assume it's well formed
comments << req.query
File.write('./_comments.json', comments.to_json)
File.write('./_comments.json', JSON.pretty_generate(comments, :indent => ' '))
end

# always return json
Expand Down

0 comments on commit be57a92

Please sign in to comment.