Skip to content

Commit

Permalink
Refactor json encoder to modern standards, stop handling PyPy 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
maszyk99 committed Mar 13, 2024
1 parent 1d1cc20 commit fcd1719
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions splunklib/searchcommands/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from collections import deque, namedtuple
from collections import OrderedDict
from itertools import chain
from json import JSONDecoder, JSONEncoder
from json import JSONDecoder, JSONEncoder, dumps
from json.encoder import encode_basestring_ascii as json_encode_string


Expand Down Expand Up @@ -662,32 +662,19 @@ def _write_record(self, record):
if self.pending_record_count >= self._maxresultrows:
self.flush(partial=True)

try:
# noinspection PyUnresolvedReferences
from _json import make_encoder
except ImportError:
# We may be running under PyPy 2.5 which does not include the _json module
_iterencode_json = JSONEncoder(separators=(',', ':')).iterencode
else:
from json.encoder import encode_basestring_ascii

@staticmethod
def _default(o):
raise TypeError(repr(o) + ' is not JSON serializable')

_iterencode_json = make_encoder(
{}, # markers (for detecting circular references)
_default, # object_encoder
encode_basestring_ascii, # string_encoder
None, # indent
':', ',', # separators
False, # sort_keys
False, # skip_keys
True # allow_nan
@staticmethod
def _iterencode_json(obj, indent_level=0):
def _default():
raise TypeError(repr(obj) + ' is not JSON serializable')

return dumps(
obj,
separators=(',', ':'),
default=_default,
ensure_ascii=True,
indent=indent_level
)

del make_encoder


class RecordWriterV1(RecordWriter):

Expand Down

0 comments on commit fcd1719

Please sign in to comment.