3

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?

Zeke
  • 1,281
  • 1
  • 18
  • 26
jake9xx
  • 41
  • 3
  • 1
    Can you post a minimal code example of your problem? – Jeroen Heier Dec 22 '17 at 17:47
  • 1
    Since `"key_with_trailing_zeros":"12.200"` will also make the checksum fail, I think you're out of luck with `json_encode()` and you'll have to roll your own. – Álvaro González Dec 22 '17 at 17:47
  • 3
    if trailing zeroes like that are important then you should be encoding the data as a *string*, not a float. – Sammitch Dec 22 '17 at 18:08
  • There is an extra `}` right after the `time`. I don't think that's on purpose, so yeah... I know this doesn't solve the problem, but just in case. – Zeke Dec 22 '17 at 18:21

0 Answers0