def data_list_serialize(value, **kwargs): return [ json.serialize(v, attributes=('name', 'date'), strategies={'date': date_serialize}, **kwargs) for v in value ]
def test_dict(self): obj = support.MyObject('test', date=datetime.now()) obj.data_dict = { 'test': support.MyObject('testing', date=datetime.now()), } d = json.serialize(obj, ('name', 'date', 'data_dict'), strategies={'data_dict': support.data_dict_serialize, 'date': support.date_serialize}) assert isinstance(d['dataDict']['test']['date'], str)
def test_list(self): obj = support.MyObject('test', date=datetime.now()) obj.data_list = [ support.MyObject('testing', date=datetime.now()), support.MyObject('again', date=datetime.now()) ] d = json.serialize(obj, ('name', 'date', 'data_list'), strategies={ 'data_list': support.data_list_serialize, 'date': support.date_serialize}) assert isinstance(d['dataList'][0]['date'], str)
def test_dict(self): obj = support.MyObject('test', date=datetime.now()) obj.data_dict = { 'test': support.MyObject('testing', date=datetime.now()), } d = json.serialize(obj, ('name', 'date', 'data_dict'), strategies={ 'data_dict': support.data_dict_serialize, 'date': support.date_serialize }) assert isinstance(d['dataDict']['test']['date'], str)
def data_dict_serialize(value, **kwargs): return { k: json.serialize(v, attributes=( 'name', 'date', ), strategies={'date': date_serialize}, **kwargs) for k, v in value.items() }
def test_list(self): obj = support.MyObject('test', date=datetime.now()) obj.data_list = [ support.MyObject('testing', date=datetime.now()), support.MyObject('again', date=datetime.now()) ] d = json.serialize(obj, ('name', 'date', 'data_list'), strategies={ 'data_list': support.data_list_serialize, 'date': support.date_serialize }) assert isinstance(d['dataList'][0]['date'], str)
def test_basic(self): obj = support.MyObject('test') d = json.serialize(obj, ('name',)) assert d == {'name': 'test'}
def test_none_attribute(self): obj = support.MyObject('test') d = json.serialize(obj, ('name', 'date'), strategies={'date': support.date_serialize}) assert 'date' not in d
def test_complex(self): obj = support.MyObject('test', date=datetime.now()) d = json.serialize(obj, ('name', 'date'), strategies={'date': support.date_serialize}) assert isinstance(d['date'], str)
def test_basic(self): obj = support.MyObject('test') d = json.serialize(obj, ('name', )) assert d == {'name': 'test'}
def data_dict_serialize(value, **kwargs): return { k: json.serialize(v, attributes=('name', 'date',), strategies={'date': date_serialize}, **kwargs) for k, v in value.items()}
def data_list_serialize(value, **kwargs): return [ json.serialize(v, attributes=('name', 'date'), strategies={'date': date_serialize}, **kwargs) for v in value]