Skip to content

Instantly share code, notes, and snippets.

@dominik-hadl
Last active December 19, 2015 20:08
Show Gist options
  • Save dominik-hadl/6011179 to your computer and use it in GitHub Desktop.
Save dominik-hadl/6011179 to your computer and use it in GitHub Desktop.
Automatically minifies all JSON files after building (release or debug).
#!/usr/bin/python
#-------------------------
# Import the dependencies
#-------------------------
import json, sys
#-------------------------
#-------------------------------------
# Get the file names from the argument
#-------------------------------------
files = sys.argv[1:]
#-------------------------------------
#-------------------------------------
# Repeat for all the files
#-------------------------------------
for jsonFile in files:
#-------------------------------------
#-------------------------
# Open the file
#-------------------------
with open(jsonFile) as f:
data = json.load(f)
#-------------------------
#-------------------------
# Compact JSON encoding
#-------------------------
compressed = json.dumps(data, separators=( ',', ':' ))
#-------------------------
#-------------------------------------------
# Replace the contents with the compact JSON
#-------------------------------------------
with open(jsonFile, 'w') as f:
f.write(compressed)
#-------------------------------------------
#-------------------------------------
# xcode-auto-json-minify.sh
# Author: Dominik Hadl (@dominikhadl)
# @desc Automatically minifies all JSON files after building (release or debug).
# @usage
# 1. Add 'JSONPrettyToUgly.py' script to your project (make sure 'copy files if needed blabla...' checkbox is selected) and uncheck your target.
# 2. Go to your project (blue icon in the top of file navigator), select your target, then to "Build Phases" and "Add Build Phase" with "Add Run Script".
# 3. Move the script after the "Copy Bundle Resources" phase and paste in this script.
# 4. Enjoy minifed JSON files and faster loading times on the device or simulator! :)
PYTHONSCRIPT=$(find "${SRCROOT}" -name "JSONPrettyToUgly.py");
find "${BUILT_PRODUCTS_DIR}" -name "*.json" -exec "$PYTHONSCRIPT" {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment