r\DatumConverter::tryEncodeAsJson PHP Method

tryEncodeAsJson() public method

public tryEncodeAsJson ( $v )
    public function tryEncodeAsJson($v)
    {
        if ($this->canEncodeAsJson($v)) {
            // PHP by default loses some precision when encoding floats, so we temporarily
            // bump up the `precision` option to avoid this.
            // The 17 assumes IEEE-754 double precision numbers.
            // Source: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
            //         "The same argument applied to double precision shows that 17 decimal
            //          digits are required to recover a double precision number."
            $previousPrecision = ini_set("precision", 17);
            $json = json_encode($v);
            if ($previousPrecision !== false) {
                ini_set("precision", $previousPrecision);
            }
            if ($json === false) {
                throw new RqlDriverError("Failed to encode document as JSON: " . json_last_error());
            }
            return $json;
        } else {
            return false;
        }
    }