def to_json(obj):
    if isinstance(obj, (list, tuple)):
        return _tuple_or_list_to_json(obj)
    elif isinstance(obj, dict):
        return _dict_to_json(obj)
    elif inspect.isclass(obj) or inspect.isfunction(obj):
        return _dict_to_json(tools.class_to_dict(obj))
    else:
        return _dict_to_json(tools.obj_to_dict(obj))
예제 #2
0
 def dumps(obj):
     if isinstance(obj, (list, tuple)):
         return _tuple_or_list_to_pickle(obj)
     elif isinstance(obj, dict):
         return pickle.dumps(_dict_to_pickle(obj), 0).decode()
     elif inspect.isclass(obj) or inspect.isfunction(obj):
         return pickle.dumps(_dict_to_pickle(tools.class_to_dict(obj)),
                             0).decode()
     else:
         return pickle.dumps(_dict_to_pickle(tools.obj_to_dict(obj)),
                             0).decode()
예제 #3
0
def _tuple_or_list_to_pickle(obj):
    temp_list = []
    for i in obj:
        if isinstance(i, (int, float, str, bool, type(None))):
            temp_list.append(i)
        elif isinstance(i, (list, tuple)):
            temp_list.append(_tuple_or_list_to_pickle(i))
        elif isinstance(i, dict):
            temp_list.append(_dict_to_pickle(i))
        elif inspect.isclass(i) or inspect.isfunction(i):
            temp_list.append(_dict_to_pickle(tools.class_to_dict(i)))
        else:
            temp_list.append(_dict_to_pickle(tools.obj_to_dict(i)))
    return temp_list
예제 #4
0
def _dict_to_pickle(dct):
    temp_dct = {}
    for key, value in dct.items():
        if isinstance(value, (int, float, str, bool, type(None))):
            temp_dct[key] = value
        elif isinstance(value, (list, tuple)):
            temp_dct[key] = _tuple_or_list_to_pickle(value)
        elif isinstance(value, dict):
            temp_dct[key] = _dict_to_pickle(value)
        elif inspect.isclass(value) or inspect.isfunction(value):
            temp_dct[key] = _dict_to_pickle(tools.class_to_dict(value))
        else:
            temp_dct[key] = _dict_to_pickle(tools.obj_to_dict(value))
    return temp_dct
def _tuple_or_list_to_json(obj):
    temp_list = []
    for i in obj:
        if isinstance(i, (int, float, str, bool, type(None))):
            temp_list.append(_basic_type_to_json(i))
        elif isinstance(i, (list, tuple)):
            temp_list.append(_tuple_or_list_to_json(i))
        elif isinstance(i, dict):
            temp_list.append(_dict_to_json(i))
        elif inspect.isclass(obj) or inspect.isfunction(obj):
            temp_list.append(_dict_to_json(tools.class_to_dict(i)))
        else:
            temp_list.append(_dict_to_json(tools.obj_to_dict(i)))
    output_str = ', '.join(temp_list)
    return '[ ' + output_str + ' ]'
def _dict_to_json(dct):
    temp_list = []
    for key, value in dct.items():
        if isinstance(value, (int, float, str, bool, type(None))):
            temp_list.append(_json_element(key, _basic_type_to_json(value)))
        elif isinstance(value, (list, tuple)):
            temp_list.append(_json_element(key, _tuple_or_list_to_json(value)))
        elif isinstance(value, dict):
            temp_list.append(_json_element(key, _dict_to_json(value)))
        elif inspect.isclass(value) or inspect.isfunction(value):
            temp_list.append(_json_element(key, _dict_to_json(tools.class_to_dict(value))))
        else:
            temp_list.append(_json_element(key, _dict_to_json(tools.obj_to_dict(value))))

    output_str = ', '.join(temp_list)
    return f'{{{output_str}}}'