Issues

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

Issue Type: Bug Created: 2011-12-08T10:17:42.000+0000 Last Updated: 2011-12-08T10:19:41.000+0000 Status: Open Fix version(s): Reporter: Eugenio Bonifacio (eugeniobonifacio) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Json

Related issues: Attachments:

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

<pre class="highlight">
    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

<pre class="highlight">
    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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts