本文整理汇总了Python中json.JSONEncoder.default方法的典型用法代码示例。如果您正苦于以下问题:Python JSONEncoder.default方法的具体用法?Python JSONEncoder.default怎么用?Python JSONEncoder.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类json.JSONEncoder
的用法示例。
在下文中一共展示了JSONEncoder.default方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encoderpolicy
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def encoderpolicy(arg=None):
"""
Decorator for encoder policy.
Allows default behaviour to be built up from methods
registered for different types of things, rather than
chain of isinstance() calls in a long if-else block.
"""
def _mutator(func):
wrapped = singledispatch(func)
@wraps(wrapped)
def wrapper(*args, **kwargs):
obj = kwargs.get("obj") or args[-1]
return wrapped.dispatch(type(obj))(*args, **kwargs)
wrapper.register = wrapped.register
return wrapper
assert isfunction(arg), arg
return _mutator(arg)
示例2: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, obj):
"""Default object encoder function
Args:
obj (:obj:`Any`): Object to be serialized
Returns:
JSON string
"""
if isinstance(obj, datetime):
return obj.isoformat()
if issubclass(obj.__class__, Enum.__class__):
return obj.value
to_json = getattr(obj, 'to_json', None)
if to_json:
out = obj.to_json()
if issubclass(obj.__class__, Model):
out.update({'__type': obj.__class__.__name__})
return out
return JSONEncoder.default(self, obj)
示例3: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, obj):
if isinstance(obj, QuerySpecification):
keys = ("select", "from_", "where", "group_by", "having", "order_by", "limit")
ret = OrderedDict([(key, getattr(obj, key)) for key in keys if getattr(obj, key)])
return ret
if isinstance(obj, (Node, JoinCriteria)):
keys = [key for key in obj.__dict__.keys() if
key[0] != '_' and key not in ('line', 'pos')]
ret = {key: getattr(obj, key) for key in keys if getattr(obj, key)}
return ret
if isinstance(obj, QualifiedName):
return obj.parts
return JSONEncoder.default(self, obj)
示例4: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, o):
return o.__dict__ if isinstance(o, ObjectView) else JSONEncoder.default(self, o)
示例5: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, obj):
try:
return encoder(obj)
except EncoderTypeError:
return JSONEncoder.default(self, obj)
示例6: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, o):
translator = getattr(o, "toJSON", None)
if (translator is None) or (not callable(translator)):
return JSONEncoder.default(self, o)
return translator()
示例7: _default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def _default(self, obj):
return getattr(obj.__class__, "to_json", _default.default)(obj)
示例8: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, obj):
"""
"""
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32,
np.float64)):
return float(obj)
elif isinstance(obj, (np.ndarray,)):
return obj.tolist()
return JSONEncoder.default(self, obj)
示例9: configure_confidential
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def configure_confidential(secure_id, secure_key, endpoint, client_name=LOG_CONFIG_SECTION, sts_token=None):
""" configure confidential
:type secure_id: string
:param secure_id: secure id
:type secure_key: string
:param secure_key: secure key
:type endpoint: string
:param endpoint: endpoint
:type client_name: string
:param client_name: section name, default is "main"
:type sts_token: string
:param sts_token: sts token name, default is None
:return:
"""
config = configparser.ConfigParser()
config.read(LOG_CREDS_FILENAME)
if not config.has_section(client_name):
config.add_section(client_name)
config.set(client_name, 'access-id', secure_id)
config.set(client_name, 'access-key', secure_key)
config.set(client_name, 'region-endpoint', endpoint)
config.set(client_name, 'sts-token', verify_sts_token(secure_id, sts_token))
with open(LOG_CREDS_FILENAME, 'w') as configfile:
config.write(configfile)
示例10: get_encoder_cls
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def get_encoder_cls(encodings):
class NonUtf8Encoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, six.binary_type):
for encoding in encodings:
try:
return obj.decode(encoding)
except UnicodeDecodeError as ex:
pass
return obj.decode('utf8', "ignore")
return JSONEncoder.default(self, obj)
return NonUtf8Encoder
示例11: default
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def default(self, obj):
if isinstance(obj, AbstractCti):
return obj.asJsonDict()
else:
return JSONEncoder.default(self, obj)
示例12: __init__
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def __init__(self, nodeName, defaultData, enabled=True, expanded=True):
""" Constructor
:param nodeName: name of this node (used to construct the node path).
:param data: the configuration data. If omitted the defaultData will be used.
:param defaultData: default data to which the data can be reset by the reset button.
:param enabled: True if the item is enabled
:param expanded: True if the item is expanded
"""
super(AbstractCti, self).__init__(nodeName=nodeName)
self._defaultData = self._enforceDataType(defaultData)
self._data = self.defaultData
self._enabled = enabled
self._expanded = expanded
self._blockRefresh = False
示例13: _closeResources
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def _closeResources(self):
""" Can be overridden to close the underlying resources or disconnect signals.
The default implementation does nothing.
Is called by self.finalize when the cti is deleted. There is no corresponding
_openResources; all resources are claimed in the constructor.
"""
pass
示例14: _dataToString
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def _dataToString(self, data):
""" Conversion function used to convert the (default)data to the display value.
"""
return str(data)
示例15: displayDefaultValue
# 需要导入模块: from json import JSONEncoder [as 别名]
# 或者: from json.JSONEncoder import default [as 别名]
def displayDefaultValue(self):
""" Returns the string representation of default data for use in the tree view.
"""
return self._dataToString(self.defaultData)