WD26/WD27: HRecordToJSON / HJSONToRecord SOLVED! May 10, 2022 07:33AM |
Admin Registered: 5 years ago Posts: 501 |
Argus
Re: WD26/WD27: HRecordToJSON / HJSONToRecord May 11, 2022 07:03PM |
Re: WD26/WD27: HRecordToJSON / HJSONToRecord May 12, 2022 08:15AM |
Admin Registered: 5 years ago Posts: 501 |
Re: WD26/WD27: HRecordToJSON / HJSONToRecord SOLVED! May 14, 2022 08:15AM |
Admin Registered: 5 years ago Posts: 501 |
Re: WD26/WD27: HRecordToJSON / HJSONToRecord SOLVED! May 16, 2022 10:01AM |
Registered: 3 years ago Posts: 107 |
FUNCTION ObjSerialise(_MsgLst is wxMessageList, _IncludeTextMemos is boolean = True, _IncludeBinaryMemos is boolean = True)boolean,JSON) //Local variables Msg is wxMessage SerBuf is Buffer ResJSON is JSON // Clear all error messages in the list previously set by this function _MsgLst:ClearListForCaller(dbgInfo(dbgElement,dbgCurrentProcess)) //Do logic WHEN EXCEPTION IN // We first serialise the object without the data sources, since natively WX cannot serialise binary fields in a data source Serialize(object,SerBuf,psdJSON) IF NOT ErrorOccurred THEN //Assign the JSON ResJSON = SerBuf //Now add the data source into the json FieldList is string = HListItem(QRYRec, hLstDetail) //First we add regular fields (non-binary fields) FOR EACH STRING FieldDef OF FieldList SEPARATED BY CR FieldName is string = ExtractString(FieldDef,1,TA FieldType is string = ExtractString(FieldDef,3,TA //Do we have a binary element in the QrySelect member IF NOT FieldType IN (hItemBinary, hItemBinaryMemo, hItemBinaryMemo4, hItemPicture, hItemTextMemo, hItemUnicodeMemo ) THEN //Serialise a Regular Field {"ResJSON.QryRec."+FieldName} = {"QRYRec."+FieldName} END END //Last we add binary fields IF _IncludeTextMemos _OR_ _IncludeBinaryMemos THEN FOR EACH STRING FieldDef OF FieldList SEPARATED BY CR FieldName is string = ExtractString(FieldDef,1,TA FieldType is string = ExtractString(FieldDef,3,TA //Do we have a binary element in the QrySelect member IF FieldType IN (hItemBinary, hItemBinaryMemo, hItemBinaryMemo4, hItemPicture, hItemTextMemo, hItemUnicodeMemo ) THEN //Serialise the binary field SerBinB64 is string SerBinB64 = Encode({":QRYRec."+FieldName},encodeBASE64NoCR) {"ResJSON.QryRec.###BinaryData###."+FieldName+".Content"} = SerBinB64 IF FieldType IN (hItemTextMemo, hItemUnicodeMemo) AND _IncludeTextMemos THEN {"ResJSON.QryRec.###BinaryData###."+FieldName+".WXType"}="TextMemo" ELSE IF _IncludeBinaryMemos THEN IF FieldType = hItemPicture THEN {"ResJSON.QryRec.###BinaryData###."+FieldName+".WXType"}="ImageMemo" ELSE {"ResJSON.QryRec.###BinaryData###."+FieldName+".WXType"}="BinaryMemo" END END <COMPILE IF ConfigurationType<>Android> //Add also Binary Memo Metadata BinInfo is string = HInfoMemo({":QryRec."+FieldName}) {"ResJSON.QryRec.###BinaryData###."+FieldName+".Type"}=ExtractString(BinInfo,1,TA {"ResJSON.QryRec.###BinaryData###."+FieldName+".Name"}=ExtractString(BinInfo,2,TA {"ResJSON.QryRec.###BinaryData###."+FieldName+".Size"}=ExtractString(BinInfo,3,TA {"ResJSON.QryRec.###BinaryData###."+FieldName+".Date"}=ExtractString(BinInfo,4,TA {"ResJSON.QryRec.###BinaryData###."+FieldName+".Time"}=ExtractString(BinInfo,5,TA {"ResJSON.QryRec.###BinaryData###."+FieldName+".Info"}=ExtractString(BinInfo,6,TA <END> END END END END //Add Some Meta Data ResJSON.Metadata.SerialiseVersion = SerialiseVersion ResJSON.Metadata.DataSource = object..Class ResJSON.Metadata.PrimaryTable = DBRec..AssociatedFile RESULT (True, ResJSON) ELSE Msg.AddToList(_MsgLst,wxMessage.MSG_SYSERROR,ErrorInfo(errCode),"An error occurred while serialising the object.",ErrorInfo(errMessage)) RESULT (False, ResJSON) END DO Msg.AddToList(_MsgLst,wxMessage.MSG_SYSEXCEPTION,ExceptionInfo(errCode),"An exception was encountered.",ExceptionInfo(errMessage)) wxApplication.LogException(_MsgLst) <COMPILE IF ConfigurationType<>Android> ExceptionEnable() <END> RESULT (False, ResJSON) END
FUNCTION ObjDeserialise(_MsgLst is wxMessageList, _JSON is JSON):boolean //Local variables Msg is wxMessage SerBuf is Buffer <utile> // Clear all error messages in the list previously set by this function _MsgLst:ClearListForCaller(dbgInfo(dbgElement,dbgCurrentProcess)) //Do logic WHEN EXCEPTION IN //Allow only compatible deserialisations Trace(_JSON.ToString) IF _JSON.Metadata.DataSource ~= object..class //Now Deserialise all basic members <COMPILE IF ConfigurationType=Android> FOR EACH Mbr OF _JSON IF NOT Lower(Mbr..Name) IN ("qryrec","metadata") THEN {":"+Mbr..Name} = Mbr..Value END END <ELSE> //Parse all base type members from the JSON ObjDef is Definition = GetDefinition(object) FOR EACH Mbr OF ObjDef.Variable IF Mbr.Definition <> Null THEN IF Mbr.Definition.Type IN (wlInt, wlInt_1, wlInt_2, wlInt_4, wlInt_8, wlUnsignedInt_1, wlUnsignedInt_2, wlUnsignedInt_4, wlUnsignedInt_8, ... wlReal, wlReal_4, wlReal_8, wlNumeric, wlCurrency, ... wlUUID, wlUUID_128, wlUUID_256, wlString, wlAnsiString, wlFixedString, wlASCIIZString, wlCharacter, ... wlDate, wlDateTime, wlTime, wlDuration, ... wlBoolean, wlEmpty) IF {"_JSON."+Mbr..Name}..Exist THEN {":"+Mbr..Name} = {"_JSON."+Mbr..Name}..Value END END END END <END> //Now deserialise the QRYRec data from the json FieldList is string = HListItem(QRYRec, hLstDetail) FOR EACH STRING FieldDef OF FieldList SEPARATED BY CR FieldName is string = ExtractString(FieldDef,1,TA FieldType is string = ExtractString(FieldDef,3,TA //Do we have a binary element in the QrySelect member IF FieldType IN (hItemBinary, hItemBinaryMemo, hItemBinaryMemo4, hItemPicture, hItemTextMemo, hItemUnicodeMemo) //Serialise the binary field SerBinB64 is Buffer SerBinB64 = Decode({"_JSON.QryRec.###BinaryData###."+FieldName+".Content"}..Value,encodeBASE64NoCR) {":QRYRec."+FieldName} = SerBinB64 ELSE //Deserialise Field {":QRYRec."+FieldName} = {"_JSON.QryRec."+FieldName}..Value END END //Return OK RESULT True ELSE Msg.AddToList(_MsgLst,wxMessage.MSG_SYSERROR,ErrorInfo(errCode),"An error occurred while deserialising into the object.",... "Incompatible Data Source types: "+_JSON.Metadata.DataSource+"/"+object..class+" (Info: "+_JSON.Metadata.PrimaryTable+"/"+DBRec..AssociatedFile+")") RESULT False END DO Msg.AddToList(_MsgLst,wxMessage.MSG_SYSEXCEPTION,ExceptionInfo(errCode),"An exception was encountered.",ExceptionInfo(errMessage)) wxApplication.LogException(_MsgLst) <COMPILE IF ConfigurationType<>Android> ExceptionEnable() <END> RESULT False END
Re: WD26/WD27: HRecordToJSON / HJSONToRecord SOLVED! May 16, 2022 10:19AM |
Admin Registered: 5 years ago Posts: 501 |