Make minified JSON

in-house-export-serialization
Kevin Chung 2020-04-27 02:00:11 -04:00
parent 89ade49695
commit 41a0ebc68d
2 changed files with 13 additions and 15 deletions

View File

@ -1,11 +0,0 @@
import json
from datetime import datetime, date
from decimal import Decimal
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if isinstance(obj, Decimal):
return str(obj)

View File

@ -1,8 +1,16 @@
import json
import six
from collections import defaultdict, OrderedDict
from datetime import datetime, date
from decimal import Decimal
from CTFd.utils.exports.encoders import JSONEncoder
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if isinstance(obj, Decimal):
return str(obj)
class JSONSerializer(object):
@ -20,8 +28,9 @@ class JSONSerializer(object):
self.buckets[path].append(result)
def wrap(self, result):
result = OrderedDict([("count", len(result)), ("results", result)])
result["meta"] = {}
result = OrderedDict(
[("count", len(result)), ("results", result), ("meta", {})]
)
return result
def close(self):
@ -40,5 +49,5 @@ class JSONSerializer(object):
except ValueError:
pass
data = json.dumps(result, cls=JSONEncoder, indent=2)
data = json.dumps(result, cls=JSONEncoder, separators=(",", ":"))
self.fileobj.write(data.encode("utf-8"))