Change in pysim[master]: extend JSONEncoder to serialze 'bytes' style objects as hex strings

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Apr 10 16:41:27 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23702 )


Change subject: extend JSONEncoder to serialze 'bytes' style objects as hex strings
......................................................................

extend JSONEncoder to serialze 'bytes' style objects as hex strings

This means we can skip a lot of code that manually converts from
bytes to hex before JSON serialization.

Change-Id: I9c9eff0556d9d196e64553b5276e162f69d0c18f
---
M pySim-shell.py
M pySim/utils.py
2 files changed, 13 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/02/23702/1

diff --git a/pySim-shell.py b/pySim-shell.py
index ce1b3bf..1d81eb8 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -39,7 +39,7 @@
 from pySim.commands import SimCardCommands
 from pySim.transport import init_reader, ApduTracer
 from pySim.cards import card_detect, Card
-from pySim.utils import h2b, swap_nibbles, rpad, h2s
+from pySim.utils import h2b, swap_nibbles, rpad, h2s, JsonEncoder
 from pySim.utils import dec_st, sanitize_pin_adm, tabulate_str_list, is_hex
 from pySim.card_handler import card_handler
 
@@ -81,9 +81,9 @@
 	def poutput_json(self, data, force_no_pretty = False):
 		"""line cmd2.putput() but for a json serializable dict."""
 		if force_no_pretty or self.json_pretty_print == False:
-			output = json.dumps(data)
+			output = json.dumps(data, cls=JsonEncoder)
 		else:
-			output = json.dumps(data, indent=4)
+			output = json.dumps(data, cls=JsonEncoder, indent=4)
 		self.poutput(output)
 
 	def _onchange_numeric_path(self, param_name, old, new):
diff --git a/pySim/utils.py b/pySim/utils.py
index 6a093fa..5f66170 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -3,9 +3,12 @@
 """ pySim: various utilities
 """
 
+import json
+from io import BytesIO
 from typing import Optional, List, Dict, Any, Tuple
 
 # Copyright (C) 2009-2010  Sylvain Munaut <tnt at 246tNt.com>
+# Copyright (C) 2021 Harald Welte <laforge at osmocom.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -831,3 +834,10 @@
 		format_str_row = (" " * lspace) + format_str_row
 		table.append(format_str_row % tuple(str_list_row))
 	return '\n'.join(table)
+
+class JsonEncoder(json.JSONEncoder):
+    """Extend the standard library JSONEncoder with support for more types."""
+    def default(self, o):
+        if isinstance(o, BytesIO) or isinstance(o, bytes) or isinstance(o, bytearray):
+            return b2h(o)
+        return json.JSONEncoder.default(self, o)

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23702
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I9c9eff0556d9d196e64553b5276e162f69d0c18f
Gerrit-Change-Number: 23702
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210410/53dfe09f/attachment.htm>


More information about the gerrit-log mailing list