Skip to content

Instantly share code, notes, and snippets.

@1mm0rt41PC
Last active August 25, 2016 12:50
Show Gist options
  • Save 1mm0rt41PC/b4c924029a12eadac3ecb1b130c1b2a4 to your computer and use it in GitHub Desktop.
Save 1mm0rt41PC/b4c924029a12eadac3ecb1b130c1b2a4 to your computer and use it in GitHub Desktop.
Convert binary plist (iOS) to yaml and json format
#!/usr/bin/python3
#
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# ImmortalPC <https://github.com/ImmortalPC> wrote this file. As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return.
# -----------------------------------------------------------------------------
#
# Version 1.0
# Date: 2015/09/25
#
# Description:
# Convert binary plist (iOS) to yaml and json format
#
# Require:
# python3
#
# OS:
# GNU/Linux, Windows, MacOS (not tested)
#
# Usage:
# ./plist2yaml.py file1.plist file2.plist
# # Will create: file1.plist.yaml and file2.plist.yaml
#
# # Mass convertion
# wget https://gist.githubusercontent.com/ImmortalPC/b4c924029a12eadac3ecb1b130c1b2a4/raw/plist2yaml.py
# find . -name '*.plist' -print0 | xargs -0 -I'{}' python3 -u ./plist2yaml.py '{}'
# OR
# find . -name '*.plist' -print0 | xargs -0 -I'{}' sh -c "python3 -u ./plist2yaml.py '{}' && notepad '{}'.yaml"
#
#
import sys, json;
from base64 import b64encode;
import plistlib;#!< Standard Library available for all OS
try:
from StringIO import StringIO;
except:
from io import StringIO;
# Force json encode for all type
class MyEncoder(json.JSONEncoder):
mMaxDepth = 1000;
mCurrentDepth = 0;
def default(self, o):
if self.mCurrentDepth > self.mMaxDepth:
raise Exception("OutOfMemory in MyEncoder");
self.mCurrentDepth += 1;
if type(o) == str:
self.mCurrentDepth = 0;
return o;
if type(o) == bytes:
return str(b64encode(o));
return o.__dict__;
def json2yaml( jsonData, indent='', indentType=' '*4, prefix='', dictSplit=False ):
_ret = '';
if type(jsonData) == list:
for key in jsonData:
if type(key) == list:
_ret += json2yaml(key, indent+indentType, indentType=indentType, prefix='- ', dictSplit=dictSplit);
elif type(key) == dict:
_ret += indent+'-\n'+json2yaml(key, indent+indentType, indentType=indentType, prefix='', dictSplit=dictSplit);
else:
_ret += indent+'- '+str(key)+'\n';
elif type(jsonData) == dict:
for key in jsonData:
if type(jsonData[key]) == list:
_ret += indent+prefix+key+':\n'+json2yaml(jsonData[key], indent+indentType, indentType=indentType, prefix='- ', dictSplit=dictSplit);
elif type(jsonData[key]) == dict:
_ret += indent+prefix+key+':\n'+json2yaml(jsonData[key], indent+indentType, indentType=indentType, prefix='', dictSplit=dictSplit);
else:
if dictSplit:
_ret += indent+prefix+key+':\n'+indentType+indent+str(jsonData[key])+'\n';
else:
_ret += indent+prefix+key+': '+str(jsonData[key])+'\n';
else:
return indent+prefix+str(jsonData)+'\n';
return _ret+'\n';
for thisFile in sys.argv[1:]:
sys.stderr.write('> '+thisFile+'\n');
theData = plistlib.readPlist(thisFile);
theData = json.loads(json.dumps(theData,cls=MyEncoder));
with open(thisFile+'.yaml','w') as fp:
fp.write(json2yaml(theData, dictSplit=False));#################<< dictSplit=True allows more readable yaml
fp.write('\n');
fp.write(('='*100)+'\n');
fp.write('= JSON FORMAT =\n');
fp.write(json.dumps(theData,indent=4,cls=MyEncoder));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment