mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-30 12:45:46 +03:00
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
This commit is contained in:
@@ -39,7 +39,7 @@ from pySim.exceptions import *
|
|||||||
from pySim.commands import SimCardCommands
|
from pySim.commands import SimCardCommands
|
||||||
from pySim.transport import init_reader, ApduTracer
|
from pySim.transport import init_reader, ApduTracer
|
||||||
from pySim.cards import card_detect, Card
|
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.utils import dec_st, sanitize_pin_adm, tabulate_str_list, is_hex
|
||||||
from pySim.card_handler import card_handler
|
from pySim.card_handler import card_handler
|
||||||
|
|
||||||
@@ -81,9 +81,9 @@ class PysimApp(cmd2.Cmd):
|
|||||||
def poutput_json(self, data, force_no_pretty = False):
|
def poutput_json(self, data, force_no_pretty = False):
|
||||||
"""line cmd2.putput() but for a json serializable dict."""
|
"""line cmd2.putput() but for a json serializable dict."""
|
||||||
if force_no_pretty or self.json_pretty_print == False:
|
if force_no_pretty or self.json_pretty_print == False:
|
||||||
output = json.dumps(data)
|
output = json.dumps(data, cls=JsonEncoder)
|
||||||
else:
|
else:
|
||||||
output = json.dumps(data, indent=4)
|
output = json.dumps(data, cls=JsonEncoder, indent=4)
|
||||||
self.poutput(output)
|
self.poutput(output)
|
||||||
|
|
||||||
def _onchange_numeric_path(self, param_name, old, new):
|
def _onchange_numeric_path(self, param_name, old, new):
|
||||||
|
|||||||
@@ -3,9 +3,12 @@
|
|||||||
""" pySim: various utilities
|
""" pySim: various utilities
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from io import BytesIO
|
||||||
from typing import Optional, List, Dict, Any, Tuple
|
from typing import Optional, List, Dict, Any, Tuple
|
||||||
|
|
||||||
# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
|
# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
|
||||||
|
# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -831,3 +834,10 @@ def tabulate_str_list(str_list, width:int = 79, hspace:int = 2, lspace:int = 1,
|
|||||||
format_str_row = (" " * lspace) + format_str_row
|
format_str_row = (" " * lspace) + format_str_row
|
||||||
table.append(format_str_row % tuple(str_list_row))
|
table.append(format_str_row % tuple(str_list_row))
|
||||||
return '\n'.join(table)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user