Skip to content

Commit be57a92

Browse files
committed
Format JSON consistently (pretty print) when writing to disk
1 parent ce16398 commit be57a92

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

_comments.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
2-
{
3-
"author": "Pete Hunt",
4-
"text": "Hey there!"
5-
}
2+
{
3+
"author": "Pete Hunt",
4+
"text": "Hey there!"
5+
}
66
]

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ app.post('/comments.json', function(req, res) {
3131
fs.readFile('_comments.json', function(err, data) {
3232
var comments = JSON.parse(data);
3333
comments.push(req.body);
34-
fs.writeFile('_comments.json', JSON.stringify(comments), function(err) {
34+
fs.writeFile('_comments.json', JSON.stringify(comments, null, 4), function(err) {
3535
res.setHeader('Content-Type', 'application/json');
3636
res.send(JSON.stringify(comments));
3737
});

server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function routeRequest()
2222
$commentsDecoded[] = ['author' => $_POST['author'],
2323
'text' => $_POST['text']];
2424

25-
$comments = json_encode($commentsDecoded);
25+
$comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT);
2626
file_put_contents('_comments.json', $comments);
2727
}
2828
header('Content-Type: application/json');

server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if req.request_method == 'POST'
2323
# Assume it's well formed
2424
comments << req.query
25-
File.write('./_comments.json', comments.to_json)
25+
File.write('./_comments.json', JSON.pretty_generate(comments, :indent => ' '))
2626
end
2727

2828
# always return json

0 commit comments

Comments
 (0)