Created
July 20, 2017 13:50
-
-
Save Daij-Djan/5d1ffeb3ee1e68e11873a3533a0a78ff to your computer and use it in GitHub Desktop.
small swift 3 tool to convert an apple plist to json
This file contains hidden or 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
// | |
// main.swift | |
// plist-to-json | |
// | |
// Created by Dominik Pich on 20.07.17. | |
// Copyright © 2017 Dominik Pich. All rights reserved. | |
// | |
import Foundation | |
//get arg | |
guard ProcessInfo.processInfo.arguments.count >= 2 else { | |
print("usage: plist-to-json %input-plist--to-convert% [%optional-output-path%]") | |
exit(-1) | |
} | |
let inputPath = (ProcessInfo.processInfo.arguments[1] as NSString).expandingTildeInPath | |
let inputURL = URL(fileURLWithPath: inputPath) | |
//read plist | |
var inputData: Data! | |
do { | |
inputData = try Data(contentsOf:inputURL) | |
} catch { | |
print("error reading data from file: \(error)") | |
exit(-1) | |
} | |
var inputDict: [String:Any]! | |
do { | |
inputDict = try PropertyListSerialization.propertyList(from: inputData, options: [], format: nil) as! [String:Any] | |
// do something with the dictionary | |
} catch { | |
print("error parsing data as plist: \(error)") | |
exit(-1) | |
} | |
//write json | |
var jsonData : Data! | |
do { | |
jsonData = try JSONSerialization.data( | |
withJSONObject: inputDict, | |
options: .prettyPrinted) | |
} | |
catch { | |
print("error generating json data: \(error)") | |
exit(-1) | |
} | |
//output (write or print) | |
if ProcessInfo.processInfo.arguments.count > 2 { | |
let outputPath = (ProcessInfo.processInfo.arguments[2] as NSString).expandingTildeInPath | |
let outputURL = URL(fileURLWithPath: outputPath) | |
do { | |
try jsonData.write(to: outputURL) | |
} | |
catch { | |
print("error writing json data to disk: \(error)") | |
exit(-1) | |
} | |
} | |
else { | |
guard let jsonString = String(data: jsonData, encoding: .utf8) else { | |
print("error generating json output") | |
exit(-1) | |
} | |
print(jsonString) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment