def serializeIt(fromJson, toClass, fatherClass=None) : # print(f'fromJson: {fromJson}, toClass: {toClass}, fatherClass: {fatherClass}') if ObjectHelper.isDictionary(fromJson) and ObjectHelper.isDictionaryClass(toClass) : # objectInstance = {} # for key, value in fromJson.items() : # innerToClass = getTargetClassFromFatherClassAndChildMethodName(fatherClass, key) # objectInstance[key] = serializeIt(fromJson, innerToClass, fatherClass=fatherClass) # return objectInstance return fromJson # print() # print() # print(fromJson) # print(f'fromJson: {fromJson}') # print(f'toClass: {toClass}') if ObjectHelper.isNativeClassInstance(fromJson) and toClass == fromJson.__class__ : return fromJson attributeNameList = ReflectionHelper.getAttributeNameList(toClass) classRole = getClassRole(toClass) # print(f' classRole = {classRole}') # print(f' attributeNameList = {attributeNameList}') fromJsonToDictionary = {} for attributeName in attributeNameList : # print(f' fromJson.get({attributeName}) = {fromJson.get(attributeName)}') jsonAttributeValue = fromJson.get(attributeName) if ObjectHelper.isNone(jsonAttributeValue) : jsonAttributeValue = fromJson.get(f'{attributeName[0].upper()}{attributeName[1:].lower()}') if ObjectHelper.isNotNone(jsonAttributeValue) : # print(f'jsonAttributeValue: {jsonAttributeValue}') fromJsonToDictionary[attributeName] = resolveValue(jsonAttributeValue, attributeName, classRole, fatherClass=fatherClass) # logList = [ # f'jsonAttributeValue: {jsonAttributeValue}', # f'attributeName: {attributeName}', # f'classRole: {classRole}', # f'fromJsonToDictionary: {fromJsonToDictionary}', # f'toClass: {toClass}' # ] # log.prettyPython(serializeIt, 'logList', logList, logLevel=log.DEBUG) else : fromJsonToDictionary[attributeName] = jsonAttributeValue # if jsonAttributeValue : # ReflectionHelper.setAttributeOrMethod(fromObject, attributeName, jsonAttributeValue) args = [] kwargs = fromJsonToDictionary.copy() # print(f'fromJsonToDictionary = {fromJsonToDictionary}') objectInstance = None for key,value in fromJsonToDictionary.items() : # print(f'*args{args},**kwargs{kwargs}') try : objectInstance = toClass(*args,**kwargs) break except : args.append(value) del kwargs[key] # print(f'args = {args}, kwargs = {kwargs}') # print(f'args = {args}, kwargs = {kwargs}') if ObjectHelper.isNone(objectInstance) : raise Exception(f'Not possible to instanciate {ReflectionHelper.getName(toClass, muteLogs=True)} class') # print(objectInstance) # print() # print() # if objectInstance is [] : # print(fromJson, toClass, fatherClass) return objectInstance
def serializeIt(fromJson, toClass, fatherClass=None): if ObjectHelper.isNotDictionary(fromJson): if ObjectHelper.isNativeClassInstance(fromJson) and toClass == fromJson.__class__ : return fromJson if isinstance(fromJson, UUID): return str(fromJson) raiseUnhandledConversion(fromJson, toClass) # print(f'fromJson: {fromJson}, toClass: {toClass}, fatherClass: {fatherClass}') else: validateToClassIsNotNone(fromJson, toClass) validateJsonIsNotNone(fromJson, toClass) if ObjectHelper.isDictionaryClass(toClass): # objectInstance = {} # for key, value in fromJson.items(): # innerToClass = getTargetClassFromFatherClassAndChildMethodName(fatherClass, key) # objectInstance[key] = serializeIt(fromJson, innerToClass, fatherClass=fatherClass) # return objectInstance return fromJson # print(fromJson) # print(f'fromJson: {fromJson}') # print(f'toClass: {toClass}') attributeNameList = getAttributeNameList_andPleaseSomeoneSlapTheFaceOfTheGuyWhoDidItInSqlAlchemy(toClass) classRole = getClassRole(toClass) # print(f' classRole = {classRole}') # print(f' attributeNameList = {attributeNameList}') fromJsonToDictionary = {} for attributeName in attributeNameList : # print(f' fromJson.get({attributeName}) = {fromJson.get(attributeName)}') jsonAttributeValue = fromJson.get(attributeName) if ObjectHelper.isNone(jsonAttributeValue): jsonAttributeValue = fromJson.get(f'{attributeName[0].upper()}{attributeName[1:].lower()}') if ObjectHelper.isNotNone(jsonAttributeValue): # print(f'jsonAttributeValue: {jsonAttributeValue}') fromJsonToDictionary[attributeName] = resolveValue(jsonAttributeValue, attributeName, classRole, fatherClass=fatherClass) # logList = [ # f'jsonAttributeValue: {jsonAttributeValue}', # f'attributeName: {attributeName}', # f'classRole: {classRole}', # f'fromJsonToDictionary: {fromJsonToDictionary}', # f'toClass: {toClass}' # ] # log.prettyPython(serializeIt, 'logList', logList, logLevel=log.DEBUG) else : fromJsonToDictionary[attributeName] = jsonAttributeValue # if jsonAttributeValue : # ReflectionHelper.setAttributeOrMethod(fromObject, attributeName, jsonAttributeValue) if isModelClass(toClass): objectInstance = pleaseSomeoneSlapTheFaceOfTheGuyWhoDidItInSqlAlchemy(toClass, fromJsonToDictionary) else: args = [] kwargs = fromJsonToDictionary.copy() # print(f'fromJsonToDictionary = {fromJsonToDictionary}') objectInstance = None for key,value in fromJsonToDictionary.items(): # print(f'*args{args},**kwargs{kwargs}') try : objectInstance = toClass(*args,**kwargs) break except Exception as exception : # print(exception) args.append(value) # del kwargs[key] kwargs.pop(key) if ObjectHelper.isNone(objectInstance): raise Exception(f'Not possible to instanciate {ReflectionHelper.getName(toClass, muteLogs=True)} class') # print(objectInstance) # if objectInstance is [] : # print(fromJson, toClass, fatherClass) return objectInstance
def getDtoDocumentationName(objectClass): if ObjectHelper.isDictionaryClass(objectClass): return JSON_OBJECT_NAME else: return ReflectionHelper.getName(objectClass, muteLogs=True)
def basicMethods(): # arrange def generatorInstance(): while True: yield False break STR_INSTANCE = str() BOOLEAN_INSTANCE = bool() INTEGER_INSTANCE = int() FLOAT_INSTANCE = float() DICTIONARY_INSTANCE = dict() LIST_INSTANCE = list() TUPLE_INSTANCE = tuple() SET_INSTANCE = set() GENERATOR_INSTANCE = generatorInstance() STR_FILLED_INSTANCE = 'str()' BOOLEAN_FILLED_INSTANCE = True INTEGER_FILLED_INSTANCE = 2 FLOAT_FILLED_INSTANCE = 3.3 DICTIONARY_FILLED_INSTANCE = {'dict()': dict()} LIST_FILLED_INSTANCE = [list(), list()] TUPLE_FILLED_INSTANCE = (tuple(), tuple()) SET_FILLED_INSTANCE = {'set()', 2} # act # assert assert ObjectHelper.isNotNone(STR_INSTANCE) assert ObjectHelper.isNotNone(BOOLEAN_INSTANCE) assert ObjectHelper.isNotNone(INTEGER_INSTANCE) assert ObjectHelper.isNotNone(FLOAT_INSTANCE) assert ObjectHelper.isNotNone(DICTIONARY_INSTANCE) assert ObjectHelper.isNotNone(LIST_INSTANCE) assert ObjectHelper.isNotNone(TUPLE_INSTANCE) assert ObjectHelper.isNotNone(SET_INSTANCE) assert ObjectHelper.isNotNone(GENERATOR_INSTANCE) assert ObjectHelper.isNotNone(STR_FILLED_INSTANCE) assert ObjectHelper.isNotNone(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotNone(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotNone(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotNone(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotNone(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotNone(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotNone(SET_FILLED_INSTANCE) assert not ObjectHelper.isNone(STR_FILLED_INSTANCE) assert not ObjectHelper.isNone(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isNone(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isNone(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isNone(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isNone(LIST_FILLED_INSTANCE) assert not ObjectHelper.isNone(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isNone(SET_FILLED_INSTANCE) assert not ObjectHelper.isNone(GENERATOR_INSTANCE) assert not ObjectHelper.isList(STR_FILLED_INSTANCE) assert not ObjectHelper.isList(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isList(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isList(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isList(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isList(LIST_FILLED_INSTANCE) assert not ObjectHelper.isList(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isList(SET_FILLED_INSTANCE) assert not ObjectHelper.isList(GENERATOR_INSTANCE) assert ObjectHelper.isNotList(STR_FILLED_INSTANCE) assert ObjectHelper.isNotList(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotList(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotList(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotList(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isNotList(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotList(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotList(SET_FILLED_INSTANCE) assert ObjectHelper.isNotList(GENERATOR_INSTANCE) assert not ObjectHelper.isSet(STR_FILLED_INSTANCE) assert not ObjectHelper.isSet(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isSet(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isSet(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isSet(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isSet(LIST_FILLED_INSTANCE) assert not ObjectHelper.isSet(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isSet(SET_FILLED_INSTANCE) assert not ObjectHelper.isSet(GENERATOR_INSTANCE) assert ObjectHelper.isNotSet(STR_FILLED_INSTANCE) assert ObjectHelper.isNotSet(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotSet(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotSet(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotSet(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotSet(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotSet(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isNotSet(SET_FILLED_INSTANCE) assert ObjectHelper.isNotSet(GENERATOR_INSTANCE) assert not ObjectHelper.isTuple(STR_FILLED_INSTANCE) assert not ObjectHelper.isTuple(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isTuple(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isTuple(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isTuple(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isTuple(LIST_FILLED_INSTANCE) assert ObjectHelper.isTuple(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isTuple(SET_FILLED_INSTANCE) assert not ObjectHelper.isTuple(GENERATOR_INSTANCE) assert ObjectHelper.isNotTuple(STR_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(LIST_FILLED_INSTANCE) assert not ObjectHelper.isNotTuple(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(SET_FILLED_INSTANCE) assert ObjectHelper.isNotTuple(GENERATOR_INSTANCE) assert not ObjectHelper.isDictionary(STR_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isDictionary(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(LIST_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(SET_FILLED_INSTANCE) assert not ObjectHelper.isDictionary(GENERATOR_INSTANCE) assert ObjectHelper.isNotDictionary(STR_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isNotDictionary(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(SET_FILLED_INSTANCE) assert ObjectHelper.isNotDictionary(GENERATOR_INSTANCE) assert not ObjectHelper.isCollection(STR_FILLED_INSTANCE) assert not ObjectHelper.isCollection(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isCollection(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isCollection(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isCollection(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isCollection(LIST_FILLED_INSTANCE) assert ObjectHelper.isCollection(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isCollection(SET_FILLED_INSTANCE) assert not ObjectHelper.isCollection(GENERATOR_INSTANCE) assert ObjectHelper.isNotCollection(STR_FILLED_INSTANCE) assert ObjectHelper.isNotCollection(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotCollection(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotCollection(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isNotCollection(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isNotCollection(LIST_FILLED_INSTANCE) assert not ObjectHelper.isNotCollection(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isNotCollection(SET_FILLED_INSTANCE) assert ObjectHelper.isNotCollection(GENERATOR_INSTANCE) assert not ObjectHelper.isDictionaryClass(STR_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(LIST_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(SET_FILLED_INSTANCE) assert not ObjectHelper.isDictionaryClass(GENERATOR_INSTANCE) assert ObjectHelper.isNotDictionaryClass(STR_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(SET_FILLED_INSTANCE) assert ObjectHelper.isNotDictionaryClass(GENERATOR_INSTANCE) assert not ObjectHelper.isDictionaryClass(type(STR_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(BOOLEAN_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(INTEGER_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(FLOAT_FILLED_INSTANCE)) assert ObjectHelper.isDictionaryClass(type(DICTIONARY_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(LIST_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(TUPLE_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(SET_FILLED_INSTANCE)) assert not ObjectHelper.isDictionaryClass(type(GENERATOR_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(STR_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(BOOLEAN_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(INTEGER_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(FLOAT_FILLED_INSTANCE)) assert not ObjectHelper.isNotDictionaryClass( type(DICTIONARY_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(LIST_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(TUPLE_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(SET_FILLED_INSTANCE)) assert ObjectHelper.isNotDictionaryClass(type(GENERATOR_INSTANCE)) assert ObjectHelper.isNativeClass(type(STR_FILLED_INSTANCE)) assert ObjectHelper.isNativeClass(type(BOOLEAN_FILLED_INSTANCE)) assert ObjectHelper.isNativeClass(type(INTEGER_FILLED_INSTANCE)) assert ObjectHelper.isNativeClass(type(FLOAT_FILLED_INSTANCE)) assert not ObjectHelper.isNativeClass(type(DICTIONARY_FILLED_INSTANCE)) assert not ObjectHelper.isNativeClass(type(LIST_FILLED_INSTANCE)) assert not ObjectHelper.isNativeClass(type(TUPLE_FILLED_INSTANCE)) assert not ObjectHelper.isNativeClass(type(SET_FILLED_INSTANCE)) assert ObjectHelper.isNativeClass(type(GENERATOR_INSTANCE)) assert not ObjectHelper.isNotNativeClass(type(STR_FILLED_INSTANCE)) assert not ObjectHelper.isNotNativeClass(type(BOOLEAN_FILLED_INSTANCE)) assert not ObjectHelper.isNotNativeClass(type(INTEGER_FILLED_INSTANCE)) assert not ObjectHelper.isNotNativeClass(type(FLOAT_FILLED_INSTANCE)) assert ObjectHelper.isNotNativeClass(type(DICTIONARY_FILLED_INSTANCE)) assert ObjectHelper.isNotNativeClass(type(LIST_FILLED_INSTANCE)) assert ObjectHelper.isNotNativeClass(type(TUPLE_FILLED_INSTANCE)) assert ObjectHelper.isNotNativeClass(type(SET_FILLED_INSTANCE)) assert not ObjectHelper.isNotNativeClass(type(GENERATOR_INSTANCE)) assert ObjectHelper.isNativeClassInstance(STR_FILLED_INSTANCE) assert ObjectHelper.isNativeClassInstance(BOOLEAN_FILLED_INSTANCE) assert ObjectHelper.isNativeClassInstance(INTEGER_FILLED_INSTANCE) assert ObjectHelper.isNativeClassInstance(FLOAT_FILLED_INSTANCE) assert not ObjectHelper.isNativeClassInstance(DICTIONARY_FILLED_INSTANCE) assert not ObjectHelper.isNativeClassInstance(LIST_FILLED_INSTANCE) assert not ObjectHelper.isNativeClassInstance(TUPLE_FILLED_INSTANCE) assert not ObjectHelper.isNativeClassInstance(SET_FILLED_INSTANCE) assert ObjectHelper.isNativeClassInstance(GENERATOR_INSTANCE) assert not ObjectHelper.isNotNativeClassIsntance(STR_FILLED_INSTANCE) assert not ObjectHelper.isNotNativeClassIsntance(BOOLEAN_FILLED_INSTANCE) assert not ObjectHelper.isNotNativeClassIsntance(INTEGER_FILLED_INSTANCE) assert not ObjectHelper.isNotNativeClassIsntance(FLOAT_FILLED_INSTANCE) assert ObjectHelper.isNotNativeClassIsntance(DICTIONARY_FILLED_INSTANCE) assert ObjectHelper.isNotNativeClassIsntance(LIST_FILLED_INSTANCE) assert ObjectHelper.isNotNativeClassIsntance(TUPLE_FILLED_INSTANCE) assert ObjectHelper.isNotNativeClassIsntance(SET_FILLED_INSTANCE) assert not ObjectHelper.isNotNativeClassIsntance(GENERATOR_INSTANCE) assert ObjectHelper.isNone(None) assert not ObjectHelper.isNotNone(None) assert not ObjectHelper.isList(None) assert ObjectHelper.isNotList(None) assert not ObjectHelper.isSet(None) assert ObjectHelper.isNotSet(None) assert not ObjectHelper.isTuple(None) assert ObjectHelper.isNotTuple(None) assert not ObjectHelper.isDictionary(None) assert ObjectHelper.isNotDictionary(None) assert not ObjectHelper.isCollection(None) assert ObjectHelper.isNotCollection(None) assert not ObjectHelper.isDictionaryClass(None) assert ObjectHelper.isNotDictionaryClass(None) assert not ObjectHelper.isNativeClass(None) assert ObjectHelper.isNotNativeClass(None) assert not ObjectHelper.isNativeClassInstance(None) assert ObjectHelper.isNotNativeClassIsntance(None) assert not ObjectHelper.isNone(type(None)) assert ObjectHelper.isNotNone(type(None)) assert not ObjectHelper.isList(type(None)) assert ObjectHelper.isNotList(type(None)) assert not ObjectHelper.isDictionary(type(None)) assert ObjectHelper.isNotDictionary(type(None)) assert not ObjectHelper.isCollection(type(None)) assert ObjectHelper.isNotCollection(type(None)) assert not ObjectHelper.isDictionaryClass(type(None)) assert ObjectHelper.isNotDictionaryClass(type(None)) assert not ObjectHelper.isNativeClass(type(None)) assert ObjectHelper.isNotNativeClass(type(None)) assert not ObjectHelper.isNativeClassInstance(type(None)) assert ObjectHelper.isNotNativeClassIsntance(type(None))