Last active
December 22, 2015 11:11
-
-
Save westerlund/15b34ac4ea9f398ac6ce to your computer and use it in GitHub Desktop.
Command line tool to pretty-print piped JSON in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Simon Westerlund on 22/12/15. | |
// Copyright © 2015 Simon Westerlund. All rights reserved. | |
// | |
import Foundation | |
func main() -> Int32 { | |
let data = NSFileHandle.fileHandleWithStandardInput().readDataToEndOfFile() | |
do { | |
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) | |
let jsonData = try NSJSONSerialization.dataWithJSONObject(jsonObject, options: .PrettyPrinted) | |
if let json = String(data: jsonData, encoding: NSUTF8StringEncoding) { | |
print(json) | |
return EXIT_SUCCESS | |
} else { | |
return EXIT_FAILURE | |
} | |
} catch (let error as NSError) { | |
print(error.localizedDescription) | |
return EXIT_FAILURE | |
} | |
} | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment