# bzjsons **Repository Path**: starlight168/bzjsons ## Basic Information - **Project Name**: bzjsons - **Description**: 支持自定义model的序列化,以规避json序列化自定义模型类出现的不支持序列化问题 - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-09 - **Last Updated**: 2022-05-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## bzjsons A enhanced json utility based upon json, which can be used to serialize or deserialize custom classes, especially it solved some known issues with json. ###### author: bright.zhang ###### version: 0.0.5 #### 安装 (Installing) Install it from PyPI: > pip install bzjsons #### 示例 (Example) # demo classes: class SalesOrder: def __init__(self, order_id=None, customer=None, goods=None, amount=None, on=None): self.order_id = order_id self.customer = customer self.goods = goods self.amount = amount self.on = on self.deleted = False class Customer: def __init__(self, customer_id=None, name=None, tel=None, addr=None): self.customer_id = customer_id self.name = name self.tel = tel self.addr = addr class Goods: def __init__(self, goods_id=None, goods_name=None, price=None): self.goods_id = goods_id self.goods_name = goods_name self.price = price > # to serialize the Customer c as below: if __name__ == '__main__': from bzjsons import Jsoner c = Customer(customer_id=1, name='bzjsons', tel='13988888888', addr='gz') c_serialized = Jsoner.dumps(c) > # to deserialize the json-string to dict, which is the same with the json.dumps(): if __name__ == '__main__': c_deserialized_as_dict = Jsoner.loads(c_serialized) # or, you can also use this instead, which will let you get your own custom object directly. c_deserialized_as_instance = Jsoner.loads_as_instance(c_serialized, cls=Customer) > # if your custom class is complex class which has other custom classes bounded, you could set **kw to specify all the specific class used. if __name__ == '__main__': customer01 = Customer('C001', 'bright', '1372388888', '广州') goods1_1 = Goods('G001', '海苔', 25.00) goods1_2 = Goods('G002', '手机', 2588.00) so1 = SalesOrder('SO-001', customer01, [goods1_1, goods1_2], 1200.38, datetime.datetime.now()) customer02 = Customer('C002', 'yoyo', '1362388888', '武汉') goods2_1 = Goods('G003', '绿瘦子', 258.00) goods2_2 = Goods('G004', '泡泡水', 9.98) so2 = SalesOrder('SO-002', customer02, [goods2_1, goods2_2], 3888.38, datetime.datetime.now()) so_list = [so1, so2] # 序列化,得到序列化后的json串: so_list_json = Jsoner.dumps(so_list) # 下面对上面序列化后的json串做反序列化-loads_as_instance: kw = dict() kw['customer'] = Customer kw['goods'] = Goods # then pass it besides: so_list_deserialized = Jsoner.loads_as_instance(so_list_json, cls=SalesOrder, **kw) # that's it. #### 注意事项 (Warnings) #####1. If you download the source code into your local machine and try to build it manually, please be notified: please try to install some packages to run a test: sqlalchemy_objects_test_manually.py if needed:
* pip install sqlalchemy * pip install sqlalchemy_utils Or, your can exclude this test manually. #####2. Please be notified that the __init__ method of entity / model class should provide None for every argument as default value, otherwise it will lead errors when performing loads or loads_as_instance like as below: class DemoModel: def __init__(self, a=None, b=None, c=None): self.a = a self.b = b self.c = c