From 5d2fad5b626a7808a3a29aa5ebf1d7975bc67b7e Mon Sep 17 00:00:00 2001 From: Nikolajus Date: Wed, 2 Apr 2014 10:55:01 +0200 Subject: [PATCH] Fix issue #32 and issue #30 . adding command to compress/minify json into single line. Added test for it and travis for tests --- .gitignore | 1 + .travis.yml | 8 ++++++ Default (Linux).sublime-keymap | 2 +- Default (OSX).sublime-keymap | 2 +- Default (Windows).sublime-keymap | 2 +- Default.sublime-commands | 6 ++++- PrettyJson.py | 46 +++++++++++++++++++++++++++----- README.md | 8 ++++++ tests/tests.py | 30 ++++++++++++++------- 9 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 336f2e6..a3f62f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc *.cache *.sublime-project +.idea \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ad3522f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: python +python: + - "2.7" + - "3.3" +script: + # Run our tests + - cd tests + - python tests.py \ No newline at end of file diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 3bb8a5f..03f2957 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -3,6 +3,6 @@ "keys": [ "ctrl+alt+j" ], - "command": "prettyjson" + "command": "pretty_json" } ] \ No newline at end of file diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index 5817f9c..df5aa79 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -3,6 +3,6 @@ "keys": [ "super+ctrl+j" ], - "command": "prettyjson" + "command": "pretty_json" } ] \ No newline at end of file diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 3bb8a5f..03f2957 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -3,6 +3,6 @@ "keys": [ "ctrl+alt+j" ], - "command": "prettyjson" + "command": "pretty_json" } ] \ No newline at end of file diff --git a/Default.sublime-commands b/Default.sublime-commands index 6117e8d..e83d70f 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -1,6 +1,10 @@ [ { "caption": "Pretty JSON: Reformat (Pretty Print) JSON", - "command": "prettyjson" + "command": "pretty_json" + }, + { + "caption": "Pretty JSON: Minify (compress) JSON", + "command": "un_pretty_json" } ] \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index 8899fc9..b16544b 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -14,10 +14,8 @@ except (ValueError): s = sublime.load_settings("Pretty JSON.sublime-settings") -class PrettyjsonCommand(sublime_plugin.TextCommand): - - """ Pretty Print JSON - """ +class PrettyJsonCommand(sublime_plugin.TextCommand): + """ Pretty Print JSON """ def run(self, edit): for region in self.view.sel(): @@ -50,13 +48,49 @@ class PrettyjsonCommand(sublime_plugin.TextCommand): exc = sys.exc_info()[1] sublime.status_message(str(exc)) - """Changes syntax to JSON if its in plain text - """ def change_syntax(self): + """ Changes syntax to JSON if its in plain text """ if "Plain text" in self.view.settings().get('syntax'): self.view.set_syntax_file("Packages/JavaScript/JSON.tmLanguage") +class UnPrettyJsonCommand(PrettyJsonCommand): + """ + Compress/minify JSON + it makes json as one-liner + """ + def run(self, edit): + """ overwriting base class run function to remove intent """ + for region in self.view.sel(): + + selected_entire_file = False + + # If no selection, use the entire file as the selection + if region.empty() and s.get("use_entire_file_if_no_selection", True): + selection = sublime.Region(0, self.view.size()) + selected_entire_file = True + else: + selection = region + + try: + obj = json.loads(self.view.substr(selection), + object_pairs_hook=OrderedDict, + parse_float=decimal.Decimal) + + self.view.replace(edit, selection, json.dumps(obj, + ensure_ascii=s.get("ensure_ascii", False), + sort_keys=s.get("sort_keys", False), + separators=(',', ':'), + use_decimal=True)) + + if selected_entire_file: + self.change_syntax() + + except Exception: + import sys + exc = sys.exc_info()[1] + sublime.status_message(str(exc)) + def plugin_loaded(): global s s = sublime.load_settings("Pretty JSON.sublime-settings") diff --git a/README.md b/README.md index aff618b..d52541b 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,27 @@ Prettify JSON plugin for Sublime Text 2 & 3 ## Installation + Install this sublime text package via [Package Control](http://wbond.net/sublime_packages/package_control) ## Usage + To prettify JSON, make selection of json and press keys: - Linux: ctrl+alt+j - Windows: ctrl+alt+j - OS X: cmd+ctrl+j +or through Command Palette Ctrl+Shift+P find "Pretty JSON: Reformat (Pretty Print) JSON" + If selection is empty and configuration entry **use_entire_file_if_no_selection** is true, tries to prettify whole file. If JSON is not valid it will be displayed in status bar of sublime. +### Compress / Minify JSON + +Using Command Palette Ctrl+Shift+P find "Pretty JSON: Minify (compress) JSON" this will make selection or full buffer as single line JSON which later you can use in command lines or somewhere else + ## Default configuration **use_entire_file_if_no_selection** - true diff --git a/tests/tests.py b/tests/tests.py index 658e590..f6b472a 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,11 +1,11 @@ -try: - # python 3 / Sublime Text 3 - from . import simplejson as json - from .simplejson import OrderedDict -except (ValueError): - # python 2 / Sublime Text 2 - import simplejson as json - from simplejson import OrderedDict +import sys +import os + +# parent folder holds libraries which needs to be included +sys.path.append(os.path.realpath('..')) + +import simplejson as json +from simplejson import OrderedDict import decimal import unittest @@ -19,7 +19,7 @@ class TestIssues(unittest.TestCase): def test_ascii_issue_10(self): tmp_str = '{"tempstr":"\u2022"}' expected_output = '''{ - "tempstr": "\u2022" + "tempstr": "\\u2022" }''' obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal) tmp_str = json.dumps(obj, indent=2, ensure_ascii=True, sort_keys=False, @@ -67,6 +67,18 @@ class TestIssues(unittest.TestCase): use_decimal=True) self.assertEqual(tmp_str, expected_output) + def test_compress_feature(self): + tmp_str = """{ + "real": 0.99 +}""" + expected_output = '{"real":0.99}' + obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal) + tmp_str = json.dumps(obj, ensure_ascii=False, sort_keys=False, + separators=(',', ':'), + use_decimal=True) + self.assertEqual(tmp_str, expected_output) + + if __name__ == '__main__': unittest.main()