I have somewhat interesting case. We have embedded devices out there, pushing JSON data to our server. We use a checksum for data in sending, so device calculates the checksum over out payload and then sends a JSON data POST message.
When there are no decimals with trailing zeros, we are just fine. However, if we encounter a key with double
/float
value we are in trouble. We cannot use %g
formatter on the device (STM32) so we need to do stuff like %1.5i
to get the decimal points sent to server.
{
"key":"emXOlXEBv9",
"data":[
"key_with_trailing_zeros":12.200,
"time":"2017-12-22 19:35:41"}
],
"csum":"XXX"
}
So now device calculates checksum over the key
and data
properties. When we send it to server and dump it after json_encode
we get:
{
"key":"emXOlXEBv9",
"data":[
"key_with_trailing_zeros":12.2,
"time":"2017-12-22 19:35:41"}
],
"csum":"XXX"
}
Although the data value can be used by our system a-ok , the checksum fails as the string buffer used to calculate the checksum is somewhat different.
Without magic tricks I cannot use %f
or %g
on the device so, so what options I have, turn all fields to string and read the numeric value at server or is there some magic bullet I have not found yet?