Skip to content

Commit 558a218

Browse files
committed
Finish object deserializer implementation
1 parent 48525ea commit 558a218

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

object_serializer/serializer.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@ def serialize(obj):
1212
return json.dumps(attributes_dict, indent=2)
1313

1414

15-
def deserialize(content, klass):
15+
def deserialize(content, klass, many=False):
1616
json_content = json.loads(content)
17-
if type(json_content) == list:
18-
for content in json:
19-
pass
20-
return
17+
if many:
18+
class_list = []
19+
for content in json_content:
20+
c = klass()
21+
for key, value in content.items():
22+
if hasattr(c, key):
23+
setattr(c, key, value)
24+
class_list.append(c)
25+
return class_list
2126

2227
c = klass()
2328
for key, value in json_content.items():
24-
setattr(c, key, value)
29+
if hasattr(c, key):
30+
setattr(c, key, value)
2531
return c
26-
27-
28-
29-
class Pessoa:
30-
31-
def __init__(self):
32-
self.nome = 'Renato'
33-
self.idade = 23

0 commit comments

Comments
 (0)