Skip to content

Instantly share code, notes, and snippets.

@jctanner
Created October 16, 2018 04:56
Show Gist options
  • Select an option

  • Save jctanner/1b8a1aaaf0881b898cae1af89785bb20 to your computer and use it in GitHub Desktop.

Select an option

Save jctanner/1b8a1aaaf0881b898cae1af89785bb20 to your computer and use it in GitHub Desktop.
dumper reproducer
#!/usr/bin/env python
import datetime
import io
import json
import os
import shutil
import six
from ansibullbot._text_compat import to_bytes, to_text
meta = {
u'hello': u'world'
}
mfile = os.path.join(
u'/tmp',
u'meta.json'
)
if os.path.exists(mfile):
os.remove(mfile)
meta[u'time'] = to_text(datetime.datetime.now().isoformat())
# Using in-memory buffer here to work around inability to dump
# json to file directly
tmp_inmemory_json = six.StringIO()
json.dump(meta, tmp_inmemory_json, sort_keys=True, indent=2)
with io.open(mfile, 'w', encoding='utf-8') as f:
shutil.copyfileobj(tmp_inmemory_json, f, -1)
tmp_inmemory_json.close()
del tmp_inmemory_json
assert os.path.isfile(mfile)
assert json.load(open(mfile, 'r'))
@webknjaz
Copy link
Copy Markdown

Even better:

diff --git a/test_dumper.py.old b/test_dumper.py
index 3b92e3e..4530c9f 100644
--- a/test_dumper.py.old
+++ b/test_dumper.py
@@ -5,7 +5,6 @@ import io
 import json
 import os
 import shutil
-import six
 
 from ansibullbot._text_compat import to_bytes, to_text
 
@@ -23,16 +22,17 @@ if os.path.exists(mfile):
 
 meta[u'time'] = to_text(datetime.datetime.now().isoformat())
 
-# Using in-memory buffer here to work around inability to dump
-# json to file directly
-tmp_inmemory_json = six.StringIO()
-json.dump(meta, tmp_inmemory_json, sort_keys=True, indent=2)
 
-with io.open(mfile, 'w', encoding='utf-8') as f:
-    shutil.copyfileobj(tmp_inmemory_json, f, -1)
+class JSONUnicodeEncoder(json.JSONEncoder):
+    def iterencode(self, o):
+        def noop(v): return v
+        transformer = noop if self.ensure_ascii else to_text
+        for chunk in super(JSONUnicodeEncoder, self).iterencode(o):
+            yield transformer(chunk)
+
 
-tmp_inmemory_json.close()
-del tmp_inmemory_json
+with io.open(mfile, 'w', encoding='utf-8') as f:
+    json.dump(meta, f, cls=JSONUnicodeEncoder, ensure_ascii=False, sort_keys=True, indent=2)
 
 
 assert os.path.isfile(mfile)

I'll send a PR.

@webknjaz
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment