Issues

ZF-11928: Zend_Json_Encoder should always encode float values using decimal point

Description

I'm using JSON-RPC server in conjunction with a Java client. I noticed that when the data to be encoded contain float values with the decimal part as zero are encoded in json in a way that is ambiguous, because the encoded number appears as integer, not a float. So the client does not have the right infos to decoded it correctly.

EXAMPLE

float number 23.130 is encoded as 23.13 => THAT IS OK float number 2.0 is encoded as 2 => AMBIGUOUS, should be 2.0

I solved this issue changing _encodeDatum method in Zend_Json_Encoder

FROM THIS


    protected function _encodeDatum(&$value)
    {
        $result = 'null';

        if (is_int($value) || is_float($value)) {
            $result = (string) $value;
            $result = str_replace(",", ".", $result);
        } elseif (is_string($value)) {
            $result = $this->_encodeString($value);
        } elseif (is_bool($value)) {
            $result = $value ? 'true' : 'false';
        }

        return $result;
    }

TO THIS


    protected function _encodeDatum(&$value)
    {
        $result = 'null';

        if(is_int($value)) {
            $result = (string) $value;
        }
        elseif(is_float($value)) {            
            $result = $value;
            $result = str_replace(",", ".", $result);
            if(strpos($value, '.') === false) {
                $result .= '.0';
            }
        } elseif (is_string($value)) {
            $result = $this->_encodeString($value);
        } elseif (is_bool($value)) {
            $result = $value ? 'true' : 'false';
        }

        return $result;
    }

Comments

No comments to display