Json 2 XML feature. Fixing issue #34
This commit is contained in:
parent
f7ea94beda
commit
890459dc5d
|
@ -7,6 +7,10 @@
|
|||
"caption": "Pretty JSON: Minify (compress) JSON",
|
||||
"command": "un_pretty_json"
|
||||
},
|
||||
{
|
||||
"caption": "Pretty JSON: JSON 2 XML",
|
||||
"command": "json_to_xml"
|
||||
},
|
||||
{
|
||||
"caption": "Pretty JSON: JSON query with ./jq",
|
||||
"command": "jq_pretty_json"
|
||||
|
|
|
@ -4,6 +4,8 @@ import decimal
|
|||
import sys
|
||||
import os
|
||||
import re
|
||||
import xml.etree.ElementTree as ET
|
||||
from xml.dom import minidom
|
||||
|
||||
try:
|
||||
# python 3 / Sublime Text 3
|
||||
|
@ -168,6 +170,71 @@ class JqPrettyJson(sublime_plugin.WindowCommand):
|
|||
sublime.status_message(str(exc))
|
||||
|
||||
|
||||
class JsonToXml(PrettyJsonCommand):
|
||||
"""
|
||||
converts Json to XML
|
||||
"""
|
||||
def run(self, edit):
|
||||
""" overwriting base class run function to remove intent """
|
||||
self.view.erase_regions('json_errors')
|
||||
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:
|
||||
h = json.loads(self.view.substr(selection))
|
||||
root = ET.Element("root")
|
||||
root = self.traverse(root, h)
|
||||
|
||||
xml_string = "<?xml version='1.0' encoding='UTF-8' ?>"
|
||||
rtn = ET.tostring(root, "utf-8", "xml")
|
||||
|
||||
if type(rtn) is bytes:
|
||||
rtn = rtn.decode("utf-8")
|
||||
|
||||
xml_string += rtn
|
||||
|
||||
xml_string = minidom.parseString(xml_string).toprettyxml(encoding="UTF-8")
|
||||
|
||||
if type(xml_string) is bytes:
|
||||
xml_string = xml_string.decode("utf-8")
|
||||
|
||||
self.view.replace(edit, selection, xml_string)
|
||||
|
||||
if selected_entire_file:
|
||||
self.change_syntax()
|
||||
|
||||
except Exception:
|
||||
exc = sys.exc_info()[1]
|
||||
self.highlight_error(str(exc))
|
||||
sublime.status_message(str(exc))
|
||||
|
||||
def traverse(self, el, ha):
|
||||
if type(ha) is dict and ha.keys():
|
||||
for i in ha.keys():
|
||||
e = ET.Element(i)
|
||||
el.append(self.traverse(e, ha[i]))
|
||||
elif type(ha) is list:
|
||||
e_items = ET.Element('items')
|
||||
for i in ha:
|
||||
e_items.append(self.traverse(ET.Element('item'), i))
|
||||
el.append(e_items)
|
||||
else:
|
||||
el.set('value', str(ha))
|
||||
|
||||
return el
|
||||
|
||||
def change_syntax(self):
|
||||
self.view.set_syntax_file("Packages/XML/XML.tmLanguage")
|
||||
|
||||
|
||||
class JqPrettyJsonOut(sublime_plugin.TextCommand):
|
||||
def run(self, edit, jq_output=''):
|
||||
self.view.insert(edit, 0, jq_output)
|
||||
|
@ -175,4 +242,4 @@ class JqPrettyJsonOut(sublime_plugin.TextCommand):
|
|||
|
||||
def plugin_loaded():
|
||||
global s
|
||||
s = sublime.load_settings("Pretty JSON.sublime-settings")
|
||||
s = sublime.load_settings("Pretty JSON.sublime-settings")
|
||||
|
|
|
@ -29,6 +29,10 @@ If JSON is not valid it will be displayed in status bar of Sublime Text
|
|||
|
||||
Using Command Palette <kbd>Ctrl+Shift+P</kbd> 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 (curl/httpie) or somewhere else...
|
||||
|
||||
### Convert JSON to XML
|
||||
|
||||
Using Command Palette <kbd>Ctrl+Shift+P</kbd> search fo "Pretty JSON: JSON 2 XML" this will convert your selected JSON of full buffer to XML and replace syntax and buffer to XML output
|
||||
|
||||
## ./jQ query/filter usage
|
||||
|
||||
Demo:
|
||||
|
|
Loading…
Reference in New Issue