JSON and Nullable elements: Difference between revisions

no edit summary
No edit summary
Line 80: Line 80:
Yes it's readable, No it's not so easy to support once you get multiple levels deep for your address element as you now have to make sure you destroy the objects etc..
Yes it's readable, No it's not so easy to support once you get multiple levels deep for your address element as you now have to make sure you destroy the objects etc..


=== Harm's alternative ===
=== Harm's RemoveEmptyMembers method ===


Harm Wibier posted a solution where you can automatically remove elements which have a value of 0 or in the case of a string element where the string element is an empty string. (2)
Harm Wibier posted a solution where you can automatically remove elements which have a value of 0 or in the case of a string element where the string element is an empty string. See also (2)
<source lang="dataflex">
<source lang="dataflex">
//
//
Line 144: Line 144:


That works, but I'm personally a bit cautious about removing all elements that have value "0".  
That works, but I'm personally a bit cautious about removing all elements that have value "0".  
Sometimes we want pass that actual value. I'd rather remove the element I specify.
Sometimes we want pass that actual value, empty or zero does not equal null. There might be another element with the value 0 that should not be removed.


=== RemoveNamedMember ===
Personally I would rather remove a specified element.


So when looking at this line:
=== The RemoveNamedMember method ===
 
So when looking at the line with the actual condition on when to remove a specific element :
<source lang="dataflex">
<source lang="dataflex">
   If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
   If (data.addresses.shipto.latitude=0.0 and data.addresses.shipto.longitude=0.0) Begin
</source>
</source>
I figured that I wanted to have a method that could take care of this in one line as well.
it looked like it would be good to have a method that could take care of this in one line as well.


eg. this would be nice to have:
eg. this would be nice to have:
Line 162: Line 164:
</source>
</source>


it will then also work for a json structure where the address is a level deeper:
it will then also work for a json structure where the address is a level deeper without having to write new code.
 
eg.
<source lang="dataflex">
<source lang="dataflex">
     Get RemoveNamedMember hoJsonRequest "Returns.addresses.shipTo.longitude"
     Get RemoveNamedMember hoJsonRequest "Returns.addresses.shipTo.longitude"
Line 171: Line 175:
    
    
   //
   //
   // Used to strip data from json
   // Used to strip a named/value pair from json
   // You can use it to directly remove a JSON member at a lower level from the JSON
   // You can use this to directly remove a JSON member at a lower level from the JSON
   // object passed via hoJSON.
   // object passed via hoJSON.
   // The member to be removed uses the exact JSON member names separated by dots and
   // The member to be removed uses the exact JSON member names separated by dots.
   // this is case sensitive.
   // Beware that JSON member names are case sensitive!
   //
   //
   // If the member does not exist a runtime error will be triggered.
   // If the member does not exist a runtime error will be triggered.
Line 207: Line 211:
</source>
</source>


Note that if you don't want the runtime error that you can use the HasMember test. 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 debugging.
Note that if you don't want the runtime error that you can use the HasMember test.  
 
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.


=== External references ===
=== External references ===
Line 213: Line 219:
* (1) [https://support.dataaccess.com/Forums/showthread.php?64157-YAFR-DataTypeToJson-beyond-19-1 YAFR DataTypeToJson beyond 19.1] loong and mostly off topic debate at the forum about null support and JSON
* (1) [https://support.dataaccess.com/Forums/showthread.php?64157-YAFR-DataTypeToJson-beyond-19-1 YAFR DataTypeToJson beyond 19.1] loong and mostly off topic debate at the forum about null support and JSON
* (2) [https://support.dataaccess.com/Forums/showthread.php?62437-JsonToDataType&p=331876#post331876 JsonToDataType] Harm Wibier's solution
* (2) [https://support.dataaccess.com/Forums/showthread.php?62437-JsonToDataType&p=331876#post331876 JsonToDataType] Harm Wibier's solution
[[Category:JSON]]
[[Category:System Integration]]