forked from TheAcademyofNaturalSciences/WikiSRATMicroService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringParser.py
More file actions
31 lines (26 loc) · 803 Bytes
/
Copy pathStringParser.py
File metadata and controls
31 lines (26 loc) · 803 Bytes
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
import re
import json
from constants import schema
from jsonschema import validate as jsonvalidate
from jsonschema import ValidationError
class StringParser:
@classmethod
def parse(self, input_string):
parsed = self.string_to_ob(input_string)
self.validate(parsed)
return parsed
@classmethod
def validate(self, object):
try:
jsonvalidate(object, schema)
except ValidationError as e:
raise AttributeError(e.message)
@classmethod
def string_to_ob(self, input_string):
try:
return json.loads(input_string)
except json.decoder.JSONDecodeError as e:
raise AttributeError(e.args[0])
@classmethod
def ob_to_string(self, object_in):
return json.dumps(object_in)