-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_schema.py
86 lines (57 loc) · 2.12 KB
/
custom_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import colander
import json
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
class DisplayAnswerObject(colander.MappingSchema):
nam = colander.SchemaNode(colander.String())
answer = colander.SchemaNode(colander.String())
# class displayanswerobj(colander.MappingSchema):
# displayanswe = DisplayAnswerObject()
class Custom_string(colander.SchemaType):
def __init__(self, DisplayAnswerObject, encoding=None, allow_empty=False):
self.objec = DisplayAnswerObject
# self.objec
self.encoding = encoding
self.allow_empty = allow_empty
def serialize(self, node, appstruct):
if appstruct is None:
return None
try:
result = appstruct
if isinstance(result, dict):
print("ser is dict")
return self.objec.serialize(result)
# if not isinstance(result, str):
# result = str(result)
# token = f.encrypt(result.encode())
# print(token)
return result
except Exception as e:
raise e
def deserialize(self, node, cstruct):
if cstruct == '' and self.allow_empty:
return ''
try:
if isinstance(cstruct, (str)):
print("string")
return cstruct
if isinstance(cstruct, dict):
print("deser is dict", self.objec)
return self.objec.deserialize(cstruct)
# if self.encoding and isinstance(cstruct, bytes):
# return str(cstruct, self.encoding)
except Exception as e:
raise e
class Personal(colander.MappingSchema):
id = colander.SchemaNode(colander.String())
displayanswer = colander.SchemaNode(Custom_string(DisplayAnswerObject()))
# displayanswer = DisplayAnswerObject()
schema=Personal()
# serialize = schema.serialize(data)
fl=open("new.json")
data = json.load(fl)
# print(serialize)
# print("**********")
deserialize = schema.deserialize(data)
print("printing serialized data",deserialize)