forked from iml130/FiwareObjectConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectFiwareConverter.py
70 lines (61 loc) · 2.6 KB
/
objectFiwareConverter.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
# Copyright 2018 Fraunhofer IML
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" This Module converts Python-Objects into the Fiware-JSON-Format.
For more Information how to use this class, see the Readme.md
You can find the needed Files to convert from an Object into JSON
in the folder JsonToObject and vice versa
"""
__author__ = "Dominik Lux"
__credits__ = ["Peter Detzner"]
__maintainer__ = "Dominik Lux"
__version__ = "0.0.1a"
__status__ = "Developement"
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from JsonToObject.reverseEntity import ReverseEntity
from ObjectToJson.entity import Entity
import json
class ObjectFiwareConverter(object):
""" This class should be primarily used to convert a Object <-> JSON-string.
The classes in subdirectories are either used to convert them into JSON
or into a Python-specific-Object.
"""
@classmethod
def obj2Fiware(clsself, _object, ind=0, dataTypeDict={}, ignorePythonMetaData=False, showIdValue=True):
en = Entity()
en.setObject(_object, dataTypeDict, ignorePythonMetaData, showIdValue= showIdValue)
return clsself._json(en, ind)
@classmethod
def fiware2Obj(clsself, _fiwareEntity, _objectStructure={}, useMetaData=True, ignoreWrongDataType=False, setAttr=False):
jsonObj= None
if(type(_fiwareEntity) is str):
jsonObj = clsself._obj(_fiwareEntity)
else:
jsonObj = _fiwareEntity
re = ReverseEntity(**jsonObj)
return re.setObject(_objectStructure, useMetaData, ignoreWrongDataType, setAttr)
@classmethod
def _complex_handler(clsself, Obj):
if hasattr(Obj, '__dict__'):
return Obj.__dict__
else:
raise TypeError('Object of type %s with value of %s is not JSON serializable' % (
type(Obj), repr(Obj)))
@classmethod
def _json(clsself, obj, ind=0):
return json.dumps(obj.__dict__, default=clsself._complex_handler, indent=ind)
@classmethod
def _obj(clsself, json_str):
return json.loads(json_str)