485
edits
(Fixup text as things are never as clear cut as they appear to be.) |
|||
Line 150: | Line 150: | ||
We don't use that as we always first convert from struct so we know the element exists. If the element is not there then there's likely a case sensitivity issue, so getting a runtime error helps when debugging your code. | We don't use that as we always first convert from struct so we know the element exists. If the element is not there then there's likely a case sensitivity issue, so getting a runtime error helps when debugging your code. | ||
Of course a little while later I bump into a more complicated issue where the removed member is in an array. Lucky for me, in an single dimension array. This can be taken care of like this: | |||
<source lang="dataflex"> | |||
// | |||
// Used to strip longitude/latitude data from json in a CreateTransactionModel | |||
// You can use it to directly remove a JSON member at a lower level from the JSON | |||
// object passed via hoJSON. | |||
// The member to be removed uses the exact JSON member names separated by dots and | |||
// this is case sensitive. | |||
// | |||
// If the member does not exist a runtime error will be triggered. | |||
// Eg. | |||
// Send RemoveNamedMember hoJsonRequest "newTransaction.addresses[2].shipTo.latitude" | |||
// | |||
Procedure RemoveNamedMember Handle hoJson String sName | |||
Integer iDotPos | |||
Integer iLSBPos iIndex | |||
Integer eType | |||
Handle hoChild | |||
Handle hoArray | |||
String sMember sIndex | |||
Move (Pos(".",sName)) to iDotPos | |||
If (iDotPos>0) Begin | |||
Move (Left(sName,iDotPos-1)) To sMember | |||
Move (Replace(sMember+".",sName,"")) To sName | |||
Move (Pos("[",sMember)) To iLSBPos | |||
If (iLSBPos>0) Begin | |||
Move (Right(sMember,(length(sMember)+1)-iLSBPos)) To sIndex | |||
Move (Replace(sIndex,sMember,"")) To sMember | |||
Move (Replace("[",sIndex,"")) To sIndex | |||
Move (Replace("]",sIndex,"")) To sIndex | |||
Move (Cast(sIndex,Integer)) To iIndex | |||
End | |||
//Get HasMember of hoJson sMember to bHasMember <-- use this if you want to filter the runtime error (we currently do not) | |||
Get MemberJsonType of hoJson sMember to eType | |||
If (eType=jsonTypeObject) Begin | |||
Get Member of hoJson sMember to hoChild | |||
If (hoChild) Begin | |||
Send RemoveNamedMember hoChild sName | |||
Send Destroy Of hoChild | |||
End | |||
End | |||
Else If (eType=jsonTypeArray) Begin | |||
Get Member of hoJson sMember to hoArray | |||
If (hoArray) Begin | |||
Get MemberByIndex of hoArray iIndex to hoChild | |||
If (hoChild) Begin | |||
Send RemoveNamedMember hoChild sName | |||
Send Destroy Of hoChild | |||
End | |||
Send Destroy Of hoArray | |||
End | |||
End | |||
End | |||
Else Begin | |||
Send RemoveMember Of hoJson sName | |||
End | |||
End_Procedure | |||
</source> | |||
=== Harm's RemoveEmptyMembers method === | === Harm's RemoveEmptyMembers method === |