Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: shjson #1

Open
kseistrup opened this issue Apr 6, 2013 · 2 comments
Open

Idea: shjson #1

kseistrup opened this issue Apr 6, 2013 · 2 comments
Labels

Comments

@kseistrup
Copy link

The shyaml script is very useful. You could easily make a JSON version, shjson, of the script:

--- shyaml  2013-03-29 19:42:05.893082254 +0100
+++ shjson  2013-03-29 21:13:47.920416719 +0100
@@ -6,11 +6,12 @@
 ##   nosetests

 import sys
-import yaml
+import simplejson
 import os.path

 EXNAME = os.path.basename(sys.argv[0])

+json2python = lambda fp: simplejson.load(fp, encoding='utf-8')

 def mget(dct, key, default=None):
     """Allow to get values deep in a dict with doted keys.
@@ -63,7 +64,7 @@
     if action in ["get-value", "get-type"]:
         key_value = "" if len(args) == 1 else args[1]
         default = args[2] if len(args) > 2 else ""
-        contents = yaml.load(sys.stdin)
+        contents = json2python(sys.stdin)
         try:
             value = mget(contents, key_value, default)
         except IndexError:
@@ -72,8 +73,13 @@
             die("Error: invalid path %r." % key_value)
         if action == "get-value":
             if not isinstance(value, (basestring, int, float)):
-                value = yaml.dump(value, default_flow_style=False)
-            print value,
+                value = simplejson.dumps(
+                    value,
+                    ensure_ascii = False,
+                    indent = 2,
+                    sort_keys = sort
+                ).encode('utf-8')
+            print value
             return 0
         else:  ## action == "get-type"
             print "struct" if isinstance(value, dict) else \
@@ -81,7 +87,7 @@
                   type(value).__name__
             return 0
     if action == "keys":
-        for k in yaml.load(sys.stdin).keys():
+        for k in sorted(json2python(sys.stdin).keys()):
             print k
         return 0
     die("Error: Invalid argument.\n%s" % usage)

Or something like that…

@vaab
Copy link
Member

vaab commented Apr 6, 2013

Thanks, that's of some interest ! The biggest concerns I could have to integrate this is to find a project name to hold both scripts ;) . Any ideas ?

I would naturally put them in the same project as they share 90% of their small code.

@kseistrup
Copy link
Author

Hm, good question… I'm blank.

@vaab vaab added the idea label Sep 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants